@astrojs/markdoc 0.0.5 → 0.1.1

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.
Files changed (57) hide show
  1. package/.turbo/turbo-build.log +2 -2
  2. package/CHANGELOG.md +56 -0
  3. package/README.md +110 -146
  4. package/components/Renderer.astro +8 -11
  5. package/components/TreeNode.ts +14 -23
  6. package/dist/config.d.ts +2 -0
  7. package/dist/config.js +6 -0
  8. package/dist/default-config.d.ts +5 -0
  9. package/dist/default-config.js +13 -0
  10. package/dist/experimental-assets-config.d.ts +2 -0
  11. package/dist/experimental-assets-config.js +25 -0
  12. package/dist/index.d.ts +1 -2
  13. package/dist/index.js +43 -112
  14. package/dist/load-config.d.ts +14 -0
  15. package/dist/load-config.js +82 -0
  16. package/dist/utils.d.ts +0 -11
  17. package/dist/utils.js +1 -44
  18. package/package.json +8 -3
  19. package/src/config.ts +5 -0
  20. package/src/default-config.ts +18 -0
  21. package/src/experimental-assets-config.ts +29 -0
  22. package/src/index.ts +65 -143
  23. package/src/load-config.ts +102 -0
  24. package/src/utils.ts +0 -58
  25. package/template/content-module-types.d.ts +1 -3
  26. package/test/content-collections.test.js +24 -172
  27. package/test/fixtures/content-collections/package.json +0 -4
  28. package/test/fixtures/content-collections/src/content/blog/post-1.mdoc +7 -0
  29. package/test/fixtures/content-collections/src/content/blog/post-2.mdoc +7 -0
  30. package/test/fixtures/content-collections/src/content/blog/post-3.mdoc +7 -0
  31. package/test/fixtures/content-collections/src/pages/entry.json.js +1 -1
  32. package/test/fixtures/render-null/astro.config.mjs +7 -0
  33. package/test/fixtures/render-null/markdoc.config.mjs +26 -0
  34. package/test/fixtures/render-null/node_modules/.bin/astro +17 -0
  35. package/test/fixtures/render-null/package.json +9 -0
  36. package/test/fixtures/render-null/src/content/blog/render-null.mdoc +7 -0
  37. package/test/fixtures/render-null/src/pages/index.astro +19 -0
  38. package/test/fixtures/render-simple/astro.config.mjs +7 -0
  39. package/test/fixtures/render-simple/node_modules/.bin/astro +17 -0
  40. package/test/fixtures/render-simple/package.json +9 -0
  41. package/test/fixtures/{content-collections/src/pages/content-simple.astro → render-simple/src/pages/index.astro} +2 -1
  42. package/test/fixtures/render-with-components/astro.config.mjs +7 -0
  43. package/test/fixtures/render-with-components/markdoc.config.mjs +28 -0
  44. package/test/fixtures/render-with-components/node_modules/.bin/astro +17 -0
  45. package/test/fixtures/render-with-components/package.json +12 -0
  46. package/test/fixtures/{content-collections/src/pages/content-with-components.astro → render-with-components/src/pages/index.astro} +2 -6
  47. package/test/fixtures/render-with-config/astro.config.mjs +7 -0
  48. package/test/fixtures/render-with-config/markdoc.config.mjs +15 -0
  49. package/test/fixtures/render-with-config/node_modules/.bin/astro +17 -0
  50. package/test/fixtures/render-with-config/package.json +9 -0
  51. package/test/fixtures/{content-collections → render-with-config}/src/content/blog/with-config.mdoc +4 -0
  52. package/test/fixtures/{content-collections/src/pages/content-with-config.astro → render-with-config/src/pages/index.astro} +2 -2
  53. package/test/render.test.js +150 -0
  54. /package/test/fixtures/{content-collections → render-simple}/src/content/blog/simple.mdoc +0 -0
  55. /package/test/fixtures/{content-collections → render-with-components}/src/components/Code.astro +0 -0
  56. /package/test/fixtures/{content-collections → render-with-components}/src/components/CustomMarquee.astro +0 -0
  57. /package/test/fixtures/{content-collections → render-with-components}/src/content/blog/with-components.mdoc +0 -0
@@ -1,5 +1,5 @@
1
- @astrojs/markdoc:build: cache hit, replaying output 0cf4b43fcaef6989
1
+ @astrojs/markdoc:build: cache hit, replaying output 92e3ff1c54a3d71a
2
2
  @astrojs/markdoc:build: 
3
- @astrojs/markdoc:build: > @astrojs/markdoc@0.0.5 build /home/runner/work/astro/astro/packages/integrations/markdoc
3
+ @astrojs/markdoc:build: > @astrojs/markdoc@0.1.1 build /home/runner/work/astro/astro/packages/integrations/markdoc
4
4
  @astrojs/markdoc:build: > astro-scripts build "src/**/*.ts" && tsc
5
5
  @astrojs/markdoc:build: 
package/CHANGELOG.md CHANGED
@@ -1,5 +1,61 @@
1
1
  # @astrojs/markdoc
2
2
 
3
+ ## 0.1.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#6723](https://github.com/withastro/astro/pull/6723) [`73fcc7627`](https://github.com/withastro/astro/commit/73fcc7627e27a001d3ed2f4d046999d91f1aef85) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Fix: when using `render: null` in your config, content is now rendered without a wrapper element.
8
+
9
+ - Updated dependencies [[`489dd8d69`](https://github.com/withastro/astro/commit/489dd8d69cdd9d7c243cf8bec96051a914984b9c), [`a1a4f45b5`](https://github.com/withastro/astro/commit/a1a4f45b51a80215fa7598da83bd0d9c5acd20d2), [`a1108e037`](https://github.com/withastro/astro/commit/a1108e037115cdb67d03505286c7d3a4fc2a1ff5), [`8b88e4cf1`](https://github.com/withastro/astro/commit/8b88e4cf15c8bea7942b3985380164e0edf7250b), [`d54cbe413`](https://github.com/withastro/astro/commit/d54cbe41349e55f8544212ad9320705f07325920), [`4c347ab51`](https://github.com/withastro/astro/commit/4c347ab51e46f2319d614f8577fe502e3dc816e2), [`ff0430786`](https://github.com/withastro/astro/commit/ff043078630e678348ae4f4757b3015b3b862c16), [`2f2e572e9`](https://github.com/withastro/astro/commit/2f2e572e937fd25451bbc78a05d55b7caa1ca3ec), [`7116c021a`](https://github.com/withastro/astro/commit/7116c021a39eac15a6e1264dfbd11bef0f5d618a)]:
10
+ - astro@2.2.0
11
+
12
+ ## 0.1.0
13
+
14
+ ### Minor Changes
15
+
16
+ - [#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)).
17
+
18
+ ## Migration
19
+
20
+ 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.
21
+
22
+ 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:
23
+
24
+ ```js
25
+ // markdoc.config.mjs
26
+ import { defineMarkdocConfig } from '@astrojs/markdoc/config';
27
+ import Aside from './src/components/Aside.astro';
28
+
29
+ export default defineMarkdocConfig({
30
+ tags: {
31
+ aside: {
32
+ render: Aside,
33
+ },
34
+ },
35
+ });
36
+ ```
37
+
38
+ You should also remove the `components` prop from your `Content` components. Since components are imported into your config directly, this is no longer needed.
39
+
40
+ ```diff
41
+ ---
42
+ - import Aside from '../components/Aside.astro';
43
+ import { getEntryBySlug } from 'astro:content';
44
+
45
+ const entry = await getEntryBySlug('docs', 'why-markdoc');
46
+ const { Content } = await entry.render();
47
+ ---
48
+
49
+ <Content
50
+ - components={{ Aside }}
51
+ />
52
+ ```
53
+
54
+ ### Patch Changes
55
+
56
+ - 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)]:
57
+ - astro@2.1.8
58
+
3
59
  ## 0.0.5
4
60
 
5
61
  ### 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 and UI framework components (React, Vue, Svelte, etc.) to your Markdoc using both [Markdoc tags][markdoc-tags] and HTML element [nodes][markdoc-nodes].
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 from your `astro.config` using the `tags` attribute.
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
- // astro.config.mjs
111
- import { defineConfig } from 'astro/config';
112
- import markdoc from '@astrojs/markdoc';
113
-
114
- // https://astro.build/config
115
- export default defineConfig({
116
- integrations: [
117
- markdoc({
118
- tags: {
119
- aside: {
120
- render: 'Aside',
121
- attributes: {
122
- // Component props as attribute definitions
123
- // See Markdoc's documentation on defining attributes
124
- // https://markdoc.dev/docs/attributes#defining-attributes
125
- type: { type: String },
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
- Then, you can wire this render name (`'Aside'`) to a component from the `components` prop via the `<Content />` component. Note the object key name (`Aside` in this case) should match the render name:
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
- ```astro
138
- ---
139
- import { getEntryBySlug } from 'astro:content';
140
- import Aside from '../components/Aside.astro';
137
+ {% aside type="tip" %}
141
138
 
142
- const entry = await getEntryBySlug('docs', 'why-markdoc');
143
- const { Content } = await entry.render();
144
- ---
139
+ Use tags like this fancy "aside" to add some *flair* to your docs.
145
140
 
146
- <Content
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
- // astro.config.mjs
157
- import { defineConfig } from 'astro/config';
158
- import markdoc from '@astrojs/markdoc';
159
-
160
- // https://astro.build/config
161
- export default defineConfig({
162
- integrations: [
163
- markdoc({
164
- nodes: {
165
- heading: {
166
- render: 'Heading',
167
- // Markdoc requires type defs for each attribute.
168
- // These should mirror the `Props` type of the component
169
- // you are rendering.
170
- // See Markdoc's documentation on defining attributes
171
- // https://markdoc.dev/docs/attributes#defining-attributes
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 [can be applied via the `components` prop](#render-markdoc-nodes--html-elements-as-astro-components):
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
- <Content
234
- components={{
235
- Aside: ClientAside,
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 Markdoc integration accepts [all Markdoc configuration options](https://markdoc.dev/docs/config), including [tags](https://markdoc.dev/docs/tags) and [functions](https://markdoc.dev/docs/functions).
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 `markdoc()` integration in your `astro.config`. This example adds a global `getCountryEmoji` function:
227
+ You can pass these options from the default export in your `markdoc.config.mjs|ts` file:
259
228
 
260
229
  ```js
261
- // astro.config.mjs
262
- import { defineConfig } from 'astro/config';
263
- import markdoc from '@astrojs/markdoc';
264
-
265
- // https://astro.build/config
266
- export default defineConfig({
267
- integrations: [
268
- markdoc({
269
- functions: {
270
- getCountryEmoji: {
271
- transform(parameters) {
272
- const [country] = Object.values(parameters);
273
- const countryToEmojiMap = {
274
- japan: '🇯🇵',
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
- ### Define Markdoc configuration at runtime
258
+ ### Pass Markdoc variables
300
259
 
301
- You may need to define Markdoc configuration at the component level, rather than the `astro.config.mjs` level. This is useful when mapping props and SSR parameters to [Markdoc variables](https://markdoc.dev/docs/variables).
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
- Astro recommends running the Markdoc transform step manually. This allows you to define your configuration and call Markdoc's rendering functions in a `.astro` file directly, ignoring any Markdoc config in your `astro.config.mjs`.
262
+ Variables can be passed as props via the `Content` component:
304
263
 
305
- You will need to install the `@markdoc/markdoc` package into your project first:
264
+ ```astro
265
+ ---
266
+ import { getEntryBySlug } from 'astro:content';
306
267
 
307
- ```sh
308
- # Using NPM
309
- npm install @markdoc/markdoc
310
- # Using Yarn
311
- yarn add @markdoc/markdoc
312
- # Using PNPM
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, you can define Markdoc configuration options using `Markdock.transform()`.
276
+ Now, `abTestGroup` is available as a variable in `docs/why-markdoc.mdoc`:
317
277
 
318
- This example defines an `abTestGroup` Markdoc variable based on an SSR param, transforming the raw entry `body`. The result is rendered using the `Renderer` component provided by `@astrojs/markdoc`:
278
+ ```md
279
+ {% if $abTestGroup === 'image-optimization-lover' %}
319
280
 
320
- ```astro
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
- const { body } = await getEntryBySlug('docs', 'with-ab-test');
327
- const ast = Markdoc.parse(body);
328
- const content = Markdoc.transform({
329
- variables: { abTestGroup: Astro.params.abTestGroup },
330
- }, ast);
331
- ---
283
+ {% /if %}
284
+ ```
332
285
 
333
- <Renderer {content} components={{ /* same `components` prop used by the `Content` component */ }} />
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 { RenderableTreeNode } from '@markdoc/markdoc';
3
- import type { AstroInstance } from 'astro';
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
- content: RenderableTreeNode;
9
- components?: Record<string, AstroInstance['default']>;
7
+ config: Config;
8
+ stringifiedAst: string;
10
9
  };
11
10
 
12
- const { content, components } = Astro.props as Props;
11
+ const { stringifiedAst, config } = Astro.props as Props;
13
12
 
14
- // Will throw if components is invalid
15
- if (components) {
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, components)} />
17
+ <ComponentNode treeNode={createTreeNode(content)} />
@@ -1,10 +1,8 @@
1
1
  import type { AstroInstance } from 'astro';
2
+ import { Fragment } from 'astro/jsx-runtime';
2
3
  import type { RenderableTreeNode } from '@markdoc/markdoc';
3
- import { createComponent, renderComponent, render } from 'astro/runtime/server/index.js';
4
- // @ts-expect-error Cannot find module 'astro:markdoc-assets' or its corresponding type declarations
5
- import { Image } from 'astro:markdoc-assets';
6
4
  import Markdoc from '@markdoc/markdoc';
7
- import { MarkdocError, isCapitalized } from '../dist/utils.js';
5
+ import { createComponent, renderComponent, render } from 'astro/runtime/server/index.js';
8
6
 
9
7
  export type TreeNode =
10
8
  | {
@@ -47,26 +45,24 @@ export const ComponentNode = createComponent({
47
45
  propagation: 'none',
48
46
  });
49
47
 
50
- const builtInComponents: Record<string, AstroInstance['default']> = {
51
- Image,
52
- };
53
-
54
- export function createTreeNode(
55
- node: RenderableTreeNode,
56
- userComponents: Record<string, AstroInstance['default']> = {}
57
- ): TreeNode {
58
- const components = { ...userComponents, ...builtInComponents };
59
-
48
+ export function createTreeNode(node: RenderableTreeNode | RenderableTreeNode[]): TreeNode {
60
49
  if (typeof node === 'string' || typeof node === 'number') {
61
50
  return { type: 'text', content: String(node) };
51
+ } else if (Array.isArray(node)) {
52
+ return {
53
+ type: 'component',
54
+ component: Fragment,
55
+ props: {},
56
+ children: node.map((child) => createTreeNode(child)),
57
+ };
62
58
  } else if (node === null || typeof node !== 'object' || !Markdoc.Tag.isTag(node)) {
63
59
  return { type: 'text', content: '' };
64
60
  }
65
61
 
66
- if (node.name in components) {
67
- const component = components[node.name];
62
+ if (typeof node.name === 'function') {
63
+ const component = node.name;
68
64
  const props = node.attributes;
69
- const children = node.children.map((child) => createTreeNode(child, components));
65
+ const children = node.children.map((child) => createTreeNode(child));
70
66
 
71
67
  return {
72
68
  type: 'component',
@@ -74,17 +70,12 @@ export function createTreeNode(
74
70
  props,
75
71
  children,
76
72
  };
77
- } else if (isCapitalized(node.name)) {
78
- throw new MarkdocError({
79
- message: `Unable to render ${JSON.stringify(node.name)}.`,
80
- hint: 'Did you add this to the "components" prop on your <Content /> component?',
81
- });
82
73
  } else {
83
74
  return {
84
75
  type: 'element',
85
76
  tag: node.name,
86
77
  attributes: node.attributes,
87
- children: node.children.map((child) => createTreeNode(child, components)),
78
+ children: node.children.map((child) => createTreeNode(child)),
88
79
  };
89
80
  }
90
81
  }
@@ -0,0 +1,2 @@
1
+ import type { ConfigType as MarkdocConfig } from '@markdoc/markdoc';
2
+ export declare function defineMarkdocConfig(config: MarkdocConfig): MarkdocConfig;
package/dist/config.js ADDED
@@ -0,0 +1,6 @@
1
+ function defineMarkdocConfig(config) {
2
+ return config;
3
+ }
4
+ export {
5
+ defineMarkdocConfig
6
+ };
@@ -0,0 +1,5 @@
1
+ import type { ConfigType as MarkdocConfig } from '@markdoc/markdoc';
2
+ import type { ContentEntryModule } from 'astro';
3
+ export declare function applyDefaultConfig(config: MarkdocConfig, ctx: {
4
+ entry: ContentEntryModule;
5
+ }): MarkdocConfig;
@@ -0,0 +1,13 @@
1
+ function applyDefaultConfig(config, ctx) {
2
+ return {
3
+ ...config,
4
+ variables: {
5
+ entry: ctx.entry,
6
+ ...config.variables
7
+ }
8
+ // TODO: heading ID calculation, Shiki syntax highlighting
9
+ };
10
+ }
11
+ export {
12
+ applyDefaultConfig
13
+ };
@@ -0,0 +1,2 @@
1
+ import type { Config as MarkdocConfig } from '@markdoc/markdoc';
2
+ export declare const experimentalAssetsConfig: MarkdocConfig;
@@ -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
@@ -1,3 +1,2 @@
1
- import type { Config as ReadonlyMarkdocConfig } from '@markdoc/markdoc';
2
1
  import type { AstroIntegration } from 'astro';
3
- export default function markdocIntegration(userMarkdocConfig?: ReadonlyMarkdocConfig): AstroIntegration;
2
+ export default function markdocIntegration(legacyConfig: any): AstroIntegration;