Skip to content

Commit 0ddeded

Browse files
committed
fix callouts
1 parent 513ed67 commit 0ddeded

File tree

10 files changed

+119
-39
lines changed

10 files changed

+119
-39
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+
}

package-lock.json

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"@astrojs/prism": "^1.0.2",
2929
"@astrojs/tailwind": "^3.1.3",
3030
"@nanostores/preact": "^0.3.1",
31+
"astro-auto-import": "^0.3.0",
3132
"astro-i18next": "^1.0.0-beta.21",
3233
"clipboard": "^2.0.11",
3334
"ethers": "^5.7.2",

src/components/PageContent/PageContent.astro

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import WhatsNext from "./WhatsNext.astro"
55
const { content, githubEditUrl, headings } = Astro.props
66
const title = content.title
77
const whatsNext = content.whatsnext
8-
console.log("whatsNext", whatsNext)
98
---
109

1110
<article id="article" class="content">

src/content/docs/en/getting-started/overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Some networks are not EVM-compatible and use languages other than Solidity for s
4848

4949
The structure of a smart contract is similar to that of a class in Javascript, with a few differences. For example, the following `HelloWorld` contract is a simple smart contract that stores a single variable and includes a function to update the value of that variable.
5050

51-
::solidity-remix[/samples/Tutorials/HelloWorld.sol]
51+
::solidity-remix[samples/Tutorials/HelloWorld.sol]
5252

5353
### Solidity versions
5454

src/content/docs/en/learn/index.mdx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,20 @@ import ClickToZoom from "../../../../components/ClickToZoom.astro"
99
import priceAggr from "../../../../assets/images/contract-devs/price-aggr.png"
1010
import ToggleElement from "../../../../components/ToggleElement.astro"
1111
import Aside from "../../../../components/Aside.astro"
12+
import CodeSample from "../../../../components/CodeSample/CodeSample.astro"
13+
14+
:::tip[Did you know?]
15+
Astro helps you build faster websites with “Islands Architecture”.
16+
:::
17+
18+
:::solidity-remix[samples/Tutorials/HelloWorld.sol]
19+
Astro helps you build faster websites with “Islands Architecture”.
20+
:::
1221

1322
Content for the Research landing page. for fancy stuff, see the infrastructure page.
1423

24+
<CodeSample lang="solidity" src="/samples/Tutorials/Test.sol" />
25+
1526
### Callouts
1627

1728
<Aside type="note" title="note">
@@ -31,12 +42,6 @@ Content for the Research landing page. for fancy stuff, see the infrastructure p
3142
For .md file you can use this code.
3243
</Aside>
3344

34-
```
35-
:::tip[Did you know?]
36-
Astro helps you build faster websites with “Islands Architecture”.
37-
:::
38-
```
39-
4045
### Images zoom
4146

4247
<ClickToZoom src={priceAggr} />

src/pages/[lang]/[...slug].astro

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ if (!page.notTranslated) {
4747
changeLanguage(params.lang)
4848
}
4949
50-
console.log("page", page)
51-
5250
const content = {
5351
...page.data,
5452
lang: params.lang,

0 commit comments

Comments
 (0)