@astrojs/markdoc 0.0.4 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +5 -5
- package/CHANGELOG.md +66 -0
- package/README.md +110 -146
- package/components/Renderer.astro +8 -11
- package/components/TreeNode.ts +6 -15
- package/dist/config.d.ts +2 -0
- package/dist/config.js +6 -0
- package/dist/default-config.d.ts +5 -0
- package/dist/default-config.js +13 -0
- package/dist/experimental-assets-config.d.ts +2 -0
- package/dist/experimental-assets-config.js +25 -0
- package/dist/index.d.ts +1 -2
- package/dist/index.js +74 -53
- package/dist/load-config.d.ts +14 -0
- package/dist/load-config.js +82 -0
- package/dist/utils.d.ts +1 -11
- package/dist/utils.js +7 -41
- package/package.json +11 -2
- package/src/config.ts +5 -0
- package/src/default-config.ts +18 -0
- package/src/experimental-assets-config.ts +29 -0
- package/src/index.ts +116 -59
- package/src/load-config.ts +102 -0
- package/src/utils.ts +5 -54
- package/template/content-module-types.d.ts +1 -3
- package/test/content-collections.test.js +24 -172
- package/test/fixtures/content-collections/package.json +0 -4
- package/test/fixtures/content-collections/src/content/blog/post-1.mdoc +7 -0
- package/test/fixtures/content-collections/src/content/blog/post-2.mdoc +7 -0
- package/test/fixtures/content-collections/src/content/blog/post-3.mdoc +7 -0
- package/test/fixtures/content-collections/src/pages/entry.json.js +1 -1
- package/test/fixtures/image-assets/astro.config.mjs +10 -0
- package/test/fixtures/image-assets/node_modules/.bin/astro +17 -0
- package/test/fixtures/image-assets/package.json +9 -0
- package/test/fixtures/image-assets/src/assets/alias/cityscape.jpg +0 -0
- package/test/fixtures/image-assets/src/assets/relative/oar.jpg +0 -0
- package/test/fixtures/image-assets/src/content/docs/intro.mdoc +7 -0
- package/test/fixtures/image-assets/src/pages/index.astro +19 -0
- package/test/fixtures/image-assets/src/public/favicon.svg +9 -0
- package/test/fixtures/render-simple/astro.config.mjs +7 -0
- package/test/fixtures/render-simple/node_modules/.bin/astro +17 -0
- package/test/fixtures/render-simple/package.json +9 -0
- package/test/fixtures/{content-collections/src/pages/content-simple.astro → render-simple/src/pages/index.astro} +2 -1
- package/test/fixtures/render-with-components/astro.config.mjs +7 -0
- package/test/fixtures/render-with-components/markdoc.config.mjs +28 -0
- package/test/fixtures/render-with-components/node_modules/.bin/astro +17 -0
- package/test/fixtures/render-with-components/package.json +12 -0
- package/test/fixtures/{content-collections/src/pages/content-with-components.astro → render-with-components/src/pages/index.astro} +2 -6
- package/test/fixtures/render-with-config/astro.config.mjs +7 -0
- package/test/fixtures/render-with-config/markdoc.config.mjs +15 -0
- package/test/fixtures/render-with-config/node_modules/.bin/astro +17 -0
- package/test/fixtures/render-with-config/package.json +9 -0
- package/test/fixtures/{content-collections → render-with-config}/src/content/blog/with-config.mdoc +4 -0
- package/test/fixtures/{content-collections/src/pages/content-with-config.astro → render-with-config/src/pages/index.astro} +2 -2
- package/test/image-assets.test.js +76 -0
- package/test/render.test.js +124 -0
- /package/test/fixtures/{content-collections → render-simple}/src/content/blog/simple.mdoc +0 -0
- /package/test/fixtures/{content-collections → render-with-components}/src/components/Code.astro +0 -0
- /package/test/fixtures/{content-collections → render-with-components}/src/components/CustomMarquee.astro +0 -0
- /package/test/fixtures/{content-collections → render-with-components}/src/content/blog/with-components.mdoc +0 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
[
|
|
2
|
-
[
|
|
3
|
-
[
|
|
4
|
-
[
|
|
5
|
-
[
|
|
1
|
+
[36m@astrojs/markdoc:build: [0mcache hit, replaying output [2m821991cc611aa46c[0m
|
|
2
|
+
[36m@astrojs/markdoc:build: [0m
|
|
3
|
+
[36m@astrojs/markdoc:build: [0m> @astrojs/markdoc@0.1.0 build /home/runner/work/astro/astro/packages/integrations/markdoc
|
|
4
|
+
[36m@astrojs/markdoc:build: [0m> astro-scripts build "src/**/*.ts" && tsc
|
|
5
|
+
[36m@astrojs/markdoc:build: [0m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,71 @@
|
|
|
1
1
|
# @astrojs/markdoc
|
|
2
2
|
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#6653](https://github.com/withastro/astro/pull/6653) [`7c439868a`](https://github.com/withastro/astro/commit/7c439868a3bc7d466418da9af669966014f3d9fe) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Simplify Markdoc configuration with a new `markdoc.config.mjs` file. This lets you import Astro components directly to render as Markdoc tags and nodes, without the need for the previous `components` property. This new configuration also unlocks passing variables to your Markdoc from the `Content` component ([see the new docs](https://docs.astro.build/en/guides/integrations-guide/markdoc/#pass-markdoc-variables)).
|
|
8
|
+
|
|
9
|
+
## Migration
|
|
10
|
+
|
|
11
|
+
Move any existing Markdoc config from your `astro.config` to a new `markdoc.config.mjs` file at the root of your project. This should be applied as a default export, with the optional `defineMarkdocConfig()` helper for autocomplete in your editor.
|
|
12
|
+
|
|
13
|
+
This example configures an `aside` Markdoc tag. Note that components should be imported and applied to the `render` attribute _directly,_ instead of passing the name as a string:
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
// markdoc.config.mjs
|
|
17
|
+
import { defineMarkdocConfig } from '@astrojs/markdoc/config';
|
|
18
|
+
import Aside from './src/components/Aside.astro';
|
|
19
|
+
|
|
20
|
+
export default defineMarkdocConfig({
|
|
21
|
+
tags: {
|
|
22
|
+
aside: {
|
|
23
|
+
render: Aside,
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
You should also remove the `components` prop from your `Content` components. Since components are imported into your config directly, this is no longer needed.
|
|
30
|
+
|
|
31
|
+
```diff
|
|
32
|
+
---
|
|
33
|
+
- import Aside from '../components/Aside.astro';
|
|
34
|
+
import { getEntryBySlug } from 'astro:content';
|
|
35
|
+
|
|
36
|
+
const entry = await getEntryBySlug('docs', 'why-markdoc');
|
|
37
|
+
const { Content } = await entry.render();
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
<Content
|
|
41
|
+
- components={{ Aside }}
|
|
42
|
+
/>
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Patch Changes
|
|
46
|
+
|
|
47
|
+
- Updated dependencies [[`1f783e320`](https://github.com/withastro/astro/commit/1f783e32075c20b13063599696644f5d47b75d8d), [`2e92e9aa9`](https://github.com/withastro/astro/commit/2e92e9aa976735c3ddb647152bb9c4850136e386), [`adecda7d6`](https://github.com/withastro/astro/commit/adecda7d6009793c5d20519a997e3b7afb08ad57), [`386336441`](https://github.com/withastro/astro/commit/386336441ad70017eea22db0683591126131db21), [`7c439868a`](https://github.com/withastro/astro/commit/7c439868a3bc7d466418da9af669966014f3d9fe), [`25cd3e574`](https://github.com/withastro/astro/commit/25cd3e574999c1c7294a089ad8c39df27ccdbf17), [`4bf87c64f`](https://github.com/withastro/astro/commit/4bf87c64ff7e9ca49e0f5c27e06bd49faaf60542), [`fc0ed9c53`](https://github.com/withastro/astro/commit/fc0ed9c53cd374860bbdb2503318a55ca09a2662)]:
|
|
48
|
+
- astro@2.1.8
|
|
49
|
+
|
|
50
|
+
## 0.0.5
|
|
51
|
+
|
|
52
|
+
### Patch Changes
|
|
53
|
+
|
|
54
|
+
- [#6630](https://github.com/withastro/astro/pull/6630) [`cfcf2e2ff`](https://github.com/withastro/astro/commit/cfcf2e2ffdaa68ace5c84329c05b83559a29d638) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Support automatic image optimization for Markdoc images when using `experimental.assets`. You can [follow our Assets guide](https://docs.astro.build/en/guides/assets/#enabling-assets-in-your-project) to enable this feature in your project. Then, start using relative or aliased image sources in your Markdoc files for automatic optimization:
|
|
55
|
+
|
|
56
|
+
```md
|
|
57
|
+
<!--Relative paths-->
|
|
58
|
+
|
|
59
|
+

|
|
60
|
+
|
|
61
|
+
<!--Or configured aliases-->
|
|
62
|
+
|
|
63
|
+

|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
- Updated dependencies [[`b7194103e`](https://github.com/withastro/astro/commit/b7194103e39267bf59dcd6ba00f522e424219d16), [`cfcf2e2ff`](https://github.com/withastro/astro/commit/cfcf2e2ffdaa68ace5c84329c05b83559a29d638), [`45da39a86`](https://github.com/withastro/astro/commit/45da39a8642d64eb318840b18dfc2b5ccc6561bc), [`7daef9a29`](https://github.com/withastro/astro/commit/7daef9a2993b5d457f3d243a1ebfd1dd383b3327)]:
|
|
67
|
+
- astro@2.1.7
|
|
68
|
+
|
|
3
69
|
## 0.0.4
|
|
4
70
|
|
|
5
71
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -99,53 +99,46 @@ const { Content } = await entry.render();
|
|
|
99
99
|
|
|
100
100
|
### Using components
|
|
101
101
|
|
|
102
|
-
You can add Astro
|
|
102
|
+
You can add Astro components to your Markdoc using both [Markdoc tags][markdoc-tags] and HTML element [nodes][markdoc-nodes].
|
|
103
103
|
|
|
104
104
|
#### Render Markdoc tags as Astro components
|
|
105
105
|
|
|
106
|
-
You may configure [Markdoc tags][markdoc-tags] that map to components. You can configure a new tag
|
|
106
|
+
You may configure [Markdoc tags][markdoc-tags] that map to components. You can configure a new tag by creating a `markdoc.config.mjs|ts` file at the root of your project and configuring the `tag` attribute.
|
|
107
107
|
|
|
108
|
+
This example renders an `Aside` component, and allows a `type` prop to be passed as a string:
|
|
108
109
|
|
|
109
110
|
```js
|
|
110
|
-
//
|
|
111
|
-
import {
|
|
112
|
-
import
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}),
|
|
130
|
-
],
|
|
131
|
-
});
|
|
111
|
+
// markdoc.config.mjs
|
|
112
|
+
import { defineMarkdocConfig } from '@astrojs/markdoc/config';
|
|
113
|
+
import Aside from './src/components/Aside.astro';
|
|
114
|
+
|
|
115
|
+
export default defineMarkdocConfig({
|
|
116
|
+
tags: {
|
|
117
|
+
aside: {
|
|
118
|
+
render: Aside,
|
|
119
|
+
attributes: {
|
|
120
|
+
// Markdoc requires type defs for each attribute.
|
|
121
|
+
// These should mirror the `Props` type of the component
|
|
122
|
+
// you are rendering.
|
|
123
|
+
// See Markdoc's documentation on defining attributes
|
|
124
|
+
// https://markdoc.dev/docs/attributes#defining-attributes
|
|
125
|
+
type: { type: String },
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
})
|
|
132
130
|
```
|
|
133
131
|
|
|
134
|
-
|
|
132
|
+
This component can now be used in your Markdoc files with the `{% aside %}` tag. Children will be passed to your component's default slot:
|
|
135
133
|
|
|
134
|
+
```md
|
|
135
|
+
# Welcome to Markdoc 👋
|
|
136
136
|
|
|
137
|
-
|
|
138
|
-
---
|
|
139
|
-
import { getEntryBySlug } from 'astro:content';
|
|
140
|
-
import Aside from '../components/Aside.astro';
|
|
137
|
+
{% aside type="tip" %}
|
|
141
138
|
|
|
142
|
-
|
|
143
|
-
const { Content } = await entry.render();
|
|
144
|
-
---
|
|
139
|
+
Use tags like this fancy "aside" to add some *flair* to your docs.
|
|
145
140
|
|
|
146
|
-
|
|
147
|
-
components={{ Aside }}
|
|
148
|
-
/>
|
|
141
|
+
{% /aside %}
|
|
149
142
|
```
|
|
150
143
|
|
|
151
144
|
#### Render Markdoc nodes / HTML elements as Astro components
|
|
@@ -153,46 +146,22 @@ const { Content } = await entry.render();
|
|
|
153
146
|
You may also want to map standard HTML elements like headings and paragraphs to components. For this, you can configure a custom [Markdoc node][markdoc-nodes]. This example overrides Markdoc's `heading` node to render a `Heading` component, passing the built-in `level` attribute as a prop:
|
|
154
147
|
|
|
155
148
|
```js
|
|
156
|
-
//
|
|
157
|
-
import {
|
|
158
|
-
import
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
heading
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
attributes: {
|
|
173
|
-
level: { type: String },
|
|
174
|
-
}
|
|
175
|
-
},
|
|
176
|
-
},
|
|
177
|
-
}),
|
|
178
|
-
],
|
|
179
|
-
});
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
Now, you can map the string passed to render (`'Heading'` in this example) to a component import. This is configured from the `<Content />` component used to render your Markdoc using the `components` prop:
|
|
183
|
-
|
|
184
|
-
```astro
|
|
185
|
-
---
|
|
186
|
-
import { getEntryBySlug } from 'astro:content';
|
|
187
|
-
import Heading from '../components/Heading.astro';
|
|
188
|
-
|
|
189
|
-
const entry = await getEntryBySlug('docs', 'why-markdoc');
|
|
190
|
-
const { Content } = await entry.render();
|
|
191
|
-
---
|
|
192
|
-
|
|
193
|
-
<Content
|
|
194
|
-
components={{ Heading }}
|
|
195
|
-
/>
|
|
149
|
+
// markdoc.config.mjs
|
|
150
|
+
import { defineMarkdocConfig } from '@astrojs/markdoc/config';
|
|
151
|
+
import Heading from './src/components/Heading.astro';
|
|
152
|
+
|
|
153
|
+
export default defineMarkdocConfig({
|
|
154
|
+
nodes: {
|
|
155
|
+
heading: {
|
|
156
|
+
render: Heading,
|
|
157
|
+
attributes: {
|
|
158
|
+
// Pass the attributes from Markdoc's default heading node
|
|
159
|
+
// as component props.
|
|
160
|
+
level: { type: String },
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
})
|
|
196
165
|
```
|
|
197
166
|
|
|
198
167
|
Now, all Markdown headings will render with the `Heading.astro` component. This example uses a level 3 heading, automatically passing `level: 3` as the component prop:
|
|
@@ -215,26 +184,26 @@ This example wraps a `Aside.tsx` component with a `ClientAside.astro` wrapper:
|
|
|
215
184
|
import Aside from './Aside';
|
|
216
185
|
---
|
|
217
186
|
|
|
218
|
-
<Aside client:load />
|
|
187
|
+
<Aside {...Astro.props} client:load />
|
|
219
188
|
```
|
|
220
189
|
|
|
221
|
-
This component
|
|
222
|
-
|
|
223
|
-
```astro
|
|
224
|
-
---
|
|
225
|
-
// src/pages/why-markdoc.astro
|
|
226
|
-
import { getEntryBySlug } from 'astro:content';
|
|
227
|
-
import ClientAside from '../components/ClientAside.astro';
|
|
228
|
-
|
|
229
|
-
const entry = await getEntryBySlug('docs', 'why-markdoc');
|
|
230
|
-
const { Content } = await entry.render();
|
|
231
|
-
---
|
|
190
|
+
This component can be passed to the `render` prop for any [tag][markdoc-tags] or [node][markdoc-nodes] in your config:
|
|
232
191
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
192
|
+
```js
|
|
193
|
+
// markdoc.config.mjs
|
|
194
|
+
import { defineMarkdocConfig } from '@astrojs/markdoc/config';
|
|
195
|
+
import Aside from './src/components/Aside.astro';
|
|
196
|
+
|
|
197
|
+
export default defineMarkdocConfig({
|
|
198
|
+
tags: {
|
|
199
|
+
aside: {
|
|
200
|
+
render: Aside,
|
|
201
|
+
attributes: {
|
|
202
|
+
type: { type: String },
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
})
|
|
238
207
|
```
|
|
239
208
|
|
|
240
209
|
### Access frontmatter and content collection information from your templates
|
|
@@ -253,35 +222,29 @@ The `$entry` object matches [the `CollectionEntry` type](https://docs.astro.buil
|
|
|
253
222
|
|
|
254
223
|
### Markdoc config
|
|
255
224
|
|
|
256
|
-
The
|
|
225
|
+
The `markdoc.config.mjs|ts` file accepts [all Markdoc configuration options](https://markdoc.dev/docs/config), including [tags](https://markdoc.dev/docs/tags) and [functions](https://markdoc.dev/docs/functions).
|
|
257
226
|
|
|
258
|
-
You can pass these options from the
|
|
227
|
+
You can pass these options from the default export in your `markdoc.config.mjs|ts` file:
|
|
259
228
|
|
|
260
229
|
```js
|
|
261
|
-
//
|
|
262
|
-
import {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
spain: '🇪🇸',
|
|
276
|
-
france: '🇫🇷',
|
|
277
|
-
}
|
|
278
|
-
return countryToEmojiMap[country] ?? '🏳'
|
|
279
|
-
},
|
|
280
|
-
},
|
|
230
|
+
// markdoc.config.mjs
|
|
231
|
+
import { defineMarkdocConfig } from '@astrojs/markdoc/config';
|
|
232
|
+
|
|
233
|
+
export default defineMarkdocConfig({
|
|
234
|
+
functions: {
|
|
235
|
+
getCountryEmoji: {
|
|
236
|
+
transform(parameters) {
|
|
237
|
+
const [country] = Object.values(parameters);
|
|
238
|
+
const countryToEmojiMap = {
|
|
239
|
+
japan: '🇯🇵',
|
|
240
|
+
spain: '🇪🇸',
|
|
241
|
+
france: '🇫🇷',
|
|
242
|
+
}
|
|
243
|
+
return countryToEmojiMap[country] ?? '🏳'
|
|
281
244
|
},
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
})
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
})
|
|
285
248
|
```
|
|
286
249
|
|
|
287
250
|
Now, you can call this function from any Markdoc content entry:
|
|
@@ -290,47 +253,46 @@ Now, you can call this function from any Markdoc content entry:
|
|
|
290
253
|
¡Hola {% getCountryEmoji("spain") %}!
|
|
291
254
|
```
|
|
292
255
|
|
|
293
|
-
:::note
|
|
294
|
-
These options will be applied during [the Markdoc "transform" phase](https://markdoc.dev/docs/render#transform). This is run **at build time** (rather than server request time) both for static and SSR Astro projects. If you need to define configuration at runtime (ex. SSR variables), [see the next section](#define-markdoc-configuration-at-runtime).
|
|
295
|
-
:::
|
|
296
|
-
|
|
297
256
|
📚 [See the Markdoc documentation](https://markdoc.dev/docs/functions#creating-a-custom-function) for more on using variables or functions in your content.
|
|
298
257
|
|
|
299
|
-
###
|
|
258
|
+
### Pass Markdoc variables
|
|
300
259
|
|
|
301
|
-
You may need to
|
|
260
|
+
You may need to pass [variables][markdoc-variables] to your content. This is useful when passing SSR parameters like A/B tests.
|
|
302
261
|
|
|
303
|
-
|
|
262
|
+
Variables can be passed as props via the `Content` component:
|
|
304
263
|
|
|
305
|
-
|
|
264
|
+
```astro
|
|
265
|
+
---
|
|
266
|
+
import { getEntryBySlug } from 'astro:content';
|
|
306
267
|
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
pnpm add @markdoc/markdoc
|
|
268
|
+
const entry = await getEntryBySlug('docs', 'why-markdoc');
|
|
269
|
+
const { Content } = await entry.render();
|
|
270
|
+
---
|
|
271
|
+
|
|
272
|
+
<!--Pass the `abTest` param as a variable-->
|
|
273
|
+
<Content abTestGroup={Astro.params.abTestGroup} />
|
|
314
274
|
```
|
|
315
275
|
|
|
316
|
-
Now,
|
|
276
|
+
Now, `abTestGroup` is available as a variable in `docs/why-markdoc.mdoc`:
|
|
317
277
|
|
|
318
|
-
|
|
278
|
+
```md
|
|
279
|
+
{% if $abTestGroup === 'image-optimization-lover' %}
|
|
319
280
|
|
|
320
|
-
|
|
321
|
-
---
|
|
322
|
-
import Markdoc from '@markdoc/markdoc';
|
|
323
|
-
import { Renderer } from '@astrojs/markdoc/components';
|
|
324
|
-
import { getEntryBySlug } from 'astro:content';
|
|
281
|
+
Let me tell you about image optimization...
|
|
325
282
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
const content = Markdoc.transform({
|
|
329
|
-
variables: { abTestGroup: Astro.params.abTestGroup },
|
|
330
|
-
}, ast);
|
|
331
|
-
---
|
|
283
|
+
{% /if %}
|
|
284
|
+
```
|
|
332
285
|
|
|
333
|
-
|
|
286
|
+
To make a variable global to all Markdoc files, you can use the `variables` attribute from your `markdoc.config.mjs|ts`:
|
|
287
|
+
|
|
288
|
+
```js
|
|
289
|
+
import { defineMarkdocConfig } from '@astrojs/markdoc/config';
|
|
290
|
+
|
|
291
|
+
export default defineMarkdocConfig({
|
|
292
|
+
variables: {
|
|
293
|
+
environment: process.env.IS_PROD ? 'prod' : 'dev',
|
|
294
|
+
}
|
|
295
|
+
})
|
|
334
296
|
```
|
|
335
297
|
|
|
336
298
|
## Examples
|
|
@@ -360,3 +322,5 @@ See [CHANGELOG.md](https://github.com/withastro/astro/tree/main/packages/integra
|
|
|
360
322
|
[markdoc-tags]: https://markdoc.dev/docs/tags
|
|
361
323
|
|
|
362
324
|
[markdoc-nodes]: https://markdoc.dev/docs/nodes
|
|
325
|
+
|
|
326
|
+
[markdoc-variables]: https://markdoc.dev/docs/variables
|
|
@@ -1,20 +1,17 @@
|
|
|
1
1
|
---
|
|
2
|
-
import type {
|
|
3
|
-
import
|
|
4
|
-
import { validateComponentsProp } from '../dist/utils.js';
|
|
2
|
+
import type { Config } from '@markdoc/markdoc';
|
|
3
|
+
import Markdoc from '@markdoc/markdoc';
|
|
5
4
|
import { ComponentNode, createTreeNode } from './TreeNode.js';
|
|
6
5
|
|
|
7
6
|
type Props = {
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
config: Config;
|
|
8
|
+
stringifiedAst: string;
|
|
10
9
|
};
|
|
11
10
|
|
|
12
|
-
const {
|
|
11
|
+
const { stringifiedAst, config } = Astro.props as Props;
|
|
13
12
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
validateComponentsProp(components);
|
|
17
|
-
}
|
|
13
|
+
const ast = Markdoc.Ast.fromJSON(stringifiedAst);
|
|
14
|
+
const content = Markdoc.transform(ast, config);
|
|
18
15
|
---
|
|
19
16
|
|
|
20
|
-
<ComponentNode treeNode={createTreeNode(content
|
|
17
|
+
<ComponentNode treeNode={createTreeNode(content)} />
|
package/components/TreeNode.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import type { AstroInstance } from 'astro';
|
|
2
2
|
import type { RenderableTreeNode } from '@markdoc/markdoc';
|
|
3
|
-
import { createComponent, renderComponent, render } from 'astro/runtime/server/index.js';
|
|
4
3
|
import Markdoc from '@markdoc/markdoc';
|
|
5
|
-
import {
|
|
4
|
+
import { createComponent, renderComponent, render } from 'astro/runtime/server/index.js';
|
|
6
5
|
|
|
7
6
|
export type TreeNode =
|
|
8
7
|
| {
|
|
@@ -45,20 +44,17 @@ export const ComponentNode = createComponent({
|
|
|
45
44
|
propagation: 'none',
|
|
46
45
|
});
|
|
47
46
|
|
|
48
|
-
export function createTreeNode(
|
|
49
|
-
node: RenderableTreeNode,
|
|
50
|
-
components: Record<string, AstroInstance['default']> = {}
|
|
51
|
-
): TreeNode {
|
|
47
|
+
export function createTreeNode(node: RenderableTreeNode): TreeNode {
|
|
52
48
|
if (typeof node === 'string' || typeof node === 'number') {
|
|
53
49
|
return { type: 'text', content: String(node) };
|
|
54
50
|
} else if (node === null || typeof node !== 'object' || !Markdoc.Tag.isTag(node)) {
|
|
55
51
|
return { type: 'text', content: '' };
|
|
56
52
|
}
|
|
57
53
|
|
|
58
|
-
if (node.name
|
|
59
|
-
const component =
|
|
54
|
+
if (typeof node.name === 'function') {
|
|
55
|
+
const component = node.name;
|
|
60
56
|
const props = node.attributes;
|
|
61
|
-
const children = node.children.map((child) => createTreeNode(child
|
|
57
|
+
const children = node.children.map((child) => createTreeNode(child));
|
|
62
58
|
|
|
63
59
|
return {
|
|
64
60
|
type: 'component',
|
|
@@ -66,17 +62,12 @@ export function createTreeNode(
|
|
|
66
62
|
props,
|
|
67
63
|
children,
|
|
68
64
|
};
|
|
69
|
-
} else if (isCapitalized(node.name)) {
|
|
70
|
-
throw new MarkdocError({
|
|
71
|
-
message: `Unable to render ${JSON.stringify(node.name)}.`,
|
|
72
|
-
hint: 'Did you add this to the "components" prop on your <Content /> component?',
|
|
73
|
-
});
|
|
74
65
|
} else {
|
|
75
66
|
return {
|
|
76
67
|
type: 'element',
|
|
77
68
|
tag: node.name,
|
|
78
69
|
attributes: node.attributes,
|
|
79
|
-
children: node.children.map((child) => createTreeNode(child
|
|
70
|
+
children: node.children.map((child) => createTreeNode(child)),
|
|
80
71
|
};
|
|
81
72
|
}
|
|
82
73
|
}
|
package/dist/config.d.ts
ADDED
package/dist/config.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import Markdoc from "@markdoc/markdoc";
|
|
2
|
+
import { Image } from "astro:assets";
|
|
3
|
+
const experimentalAssetsConfig = {
|
|
4
|
+
nodes: {
|
|
5
|
+
image: {
|
|
6
|
+
attributes: {
|
|
7
|
+
...Markdoc.nodes.image.attributes,
|
|
8
|
+
__optimizedSrc: { type: "Object" }
|
|
9
|
+
},
|
|
10
|
+
transform(node, config) {
|
|
11
|
+
const attributes = node.transformAttributes(config);
|
|
12
|
+
const children = node.transformChildren(config);
|
|
13
|
+
if (node.type === "image" && "__optimizedSrc" in node.attributes) {
|
|
14
|
+
const { __optimizedSrc, ...rest } = node.attributes;
|
|
15
|
+
return new Markdoc.Tag(Image, { ...rest, src: __optimizedSrc }, children);
|
|
16
|
+
} else {
|
|
17
|
+
return new Markdoc.Tag("img", attributes, children);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
export {
|
|
24
|
+
experimentalAssetsConfig
|
|
25
|
+
};
|
package/dist/index.d.ts
CHANGED