Skip to content

Commit 3297159

Browse files
authored
Merge pull request #12 from scroll-tech/ui-refactor
UI refactor
2 parents 274b301 + ebb3276 commit 3297159

File tree

120 files changed

+4462
-1087
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+4462
-1087
lines changed

astro.config.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { defineConfig } from "astro/config"
22
import preact from "@astrojs/preact"
33
import react from "@astrojs/react"
44
import astroI18next from "astro-i18next"
5-
import { astroCallouts } from "./integrations/astro-callouts"
6-
import { solidityRemixCode } from "./integrations/solidity-remix"
5+
import { astroCallouts, asideAutoImport } from "./integrations/astro-callouts"
6+
import { solidityRemixCode, codeSampleAutoImport } from "./integrations/solidity-remix"
77
import { youtubeEmbed } from "./integrations/youtube-embed"
88
import mdx from "@astrojs/mdx"
99
import rehypeSlug from "rehype-slug"
@@ -12,6 +12,7 @@ import rehypeKatex from "rehype-katex"
1212
import remarkGfm from "remark-gfm"
1313
import remarkMath from "remark-math"
1414
import image from "@astrojs/image"
15+
import AutoImport from "astro-auto-import"
1516

1617
import sitemap from "@astrojs/sitemap"
1718

@@ -24,6 +25,9 @@ export default defineConfig({
2425
astroFlavoredMarkdown: true,
2526
},
2627
integrations: [
28+
AutoImport({
29+
imports: [asideAutoImport, codeSampleAutoImport],
30+
}),
2731
preact({
2832
compat: true,
2933
}),

integrations/astro-callouts.ts

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
/**
2-
* Astro's implementation of callouts
3-
* https://github.com/withastro/docs/blob/main/integrations/astro-asides.ts
4-
*/
5-
61
import type { AstroIntegration } from "astro"
72
import type * as mdast from "mdast"
8-
import type * as unified from "unified"
9-
import { h } from "hastscript"
103
import remarkDirective from "remark-directive"
11-
import { visit } from "unist-util-visit"
4+
import type * as unified from "unified"
125
import { remove } from "unist-util-remove"
6+
import { visit } from "unist-util-visit"
7+
import { makeComponentNode } from "./utils/makeComponentNode"
138

149
const AsideTagname = "AutoImportedAside"
1510

11+
export const asideAutoImport: Record<string, [string, string][]> = {
12+
"~/components/Aside.astro": [["default", AsideTagname]],
13+
}
1614
/**
1715
* remark plugin that converts blocks delimited with `:::` into instances of
1816
* the `<Aside>` component. Depends on the `remark-directive` module for the
@@ -38,8 +36,9 @@ function remarkAsides(): unified.Plugin<[], mdast.Root> {
3836
const variants = new Set(["note", "tip", "caution", "danger"])
3937

4038
const transformer: unified.Transformer<mdast.Root> = (tree) => {
41-
visit(tree, (node) => {
42-
if (node.type !== "containerDirective") return
39+
// @ts-expect-error Possibly infinite type instantiation we can’t do anything about.
40+
visit(tree, (node, index, parent) => {
41+
if (!parent || index === null || node.type !== "containerDirective") return
4342
const type = node.name
4443
if (!variants.has(type)) return
4544

@@ -57,9 +56,8 @@ function remarkAsides(): unified.Plugin<[], mdast.Root> {
5756
}
5857
})
5958

60-
const data = node.data || (node.data = {})
61-
data.hName = AsideTagname
62-
data.hProperties = h(AsideTagname, { type, title }).properties
59+
// Replace this node with the aside component it represents.
60+
parent.children[index] = makeComponentNode(AsideTagname, { attributes: { type, title } }, ...node.children)
6361
})
6462
}
6563

@@ -75,18 +73,12 @@ export function astroCallouts(): AstroIntegration {
7573
return {
7674
name: "@astrojs/callouts",
7775
hooks: {
78-
"astro:config:setup": ({ injectScript, updateConfig }) => {
76+
"astro:config:setup": ({ updateConfig }) => {
7977
updateConfig({
8078
markdown: {
8179
remarkPlugins: [remarkDirective, remarkAsides()],
8280
},
8381
})
84-
85-
// Auto-import the Aside component and attach it to the global scope
86-
injectScript(
87-
"page-ssr",
88-
`import ${AsideTagname} from "~/components/Aside.astro"; global.${AsideTagname} = ${AsideTagname};`
89-
)
9082
},
9183
},
9284
}

integrations/solidity-remix.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@ import { h } from "hastscript"
1010
import remarkDirective from "remark-directive"
1111
import { visit } from "unist-util-visit"
1212
import { remove } from "unist-util-remove"
13+
import { makeComponentNode } from "./utils/makeComponentNode"
1314

1415
const CodeSampleTagName = "AutoImportedCodeSample"
1516

17+
export const codeSampleAutoImport: Record<string, [string, string][]> = {
18+
"~/components/CodeSample/CodeSample.astro": [["default", CodeSampleTagName]],
19+
}
20+
1621
/**
1722
* remark plugin that converts blocks delimited with `:::` into instances of
1823
* the `<CodeSample>` component. Depends on the `remark-directive` module for the
@@ -52,6 +57,18 @@ function remarkSolidityRemix(): unified.Plugin<[], mdast.Root> {
5257

5358
if (!src) throw Error("No source was provided for the solidity example")
5459

60+
// const codeSnippetWrapper = makeComponentNode(
61+
// CodeSnippetTagname,
62+
// { mdx: isMDX, attributes },
63+
// code
64+
// );
65+
66+
// parent.children[index] = makeComponentNode(
67+
// AsideTagname,
68+
// { mdx: isMDXFile(file), attributes: { type, title } },
69+
// ...node.children
70+
// );
71+
5572
const data = node.data || (node.data = {})
5673
data.hName = CodeSampleTagName
5774
data.hProperties = h(CodeSampleTagName, {
@@ -73,18 +90,12 @@ export function solidityRemixCode(): AstroIntegration {
7390
return {
7491
name: "@smart-contract/solidity-remix",
7592
hooks: {
76-
"astro:config:setup": ({ injectScript, updateConfig }) => {
93+
"astro:config:setup": ({ updateConfig }) => {
7794
updateConfig({
7895
markdown: {
7996
remarkPlugins: [remarkDirective, remarkSolidityRemix()],
8097
},
8198
})
82-
83-
// Auto-import the Aside component and attach it to the global scope
84-
injectScript(
85-
"page-ssr",
86-
`import ${CodeSampleTagName} from "~/components/CodeSample/CodeSample.astro"; global.${CodeSampleTagName} = ${CodeSampleTagName};`
87-
)
8899
},
89100
},
90101
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type { BlockContent } from "mdast"
2+
import type { MdxJsxAttribute, MdxJsxFlowElement } from "mdast-util-mdx-jsx"
3+
4+
interface NodeProps {
5+
attributes?: Record<string, string | boolean | number | undefined | null>
6+
}
7+
8+
/**
9+
* Create AST node for a custom component injection.
10+
*
11+
* @example
12+
* makeComponentNode('MyComponent', { prop: 'val' }, h('p', 'Paragraph inside component'))
13+
*
14+
*/
15+
export function makeComponentNode(
16+
name: string,
17+
{ attributes = {} }: NodeProps = {},
18+
...children: BlockContent[]
19+
): MdxJsxFlowElement {
20+
return {
21+
type: "mdxJsxFlowElement",
22+
name,
23+
attributes: Object.entries(attributes)
24+
// Filter out non-truthy attributes to avoid empty attrs being parsed as `true`.
25+
.filter(([_k, v]) => v !== false && Boolean(v))
26+
.map(([name, value]) => ({
27+
type: "mdxJsxAttribute",
28+
name,
29+
value: value as MdxJsxAttribute["value"],
30+
})),
31+
children,
32+
}
33+
}

0 commit comments

Comments
 (0)