@astrojs/markdoc 1.0.0-beta.0 → 1.0.0-beta.3

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/LICENSE CHANGED
@@ -20,7 +20,6 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  SOFTWARE.
22
22
 
23
-
24
23
  """
25
24
  This license applies to parts of the `packages/create-astro` and `packages/astro` subdirectories originating from the https://github.com/sveltejs/kit repository:
26
25
 
@@ -33,7 +32,6 @@ The above copyright notice and this permission notice shall be included in all c
33
32
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34
33
  """
35
34
 
36
-
37
35
  """
38
36
  This license applies to parts of the `packages/create-astro` and `packages/astro` subdirectories originating from the https://github.com/vitejs/vite repository:
39
37
 
package/README.md CHANGED
@@ -2,452 +2,37 @@
2
2
 
3
3
  This **[Astro integration][astro-integration]** enables the usage of [Markdoc](https://markdoc.dev/) to create components, pages, and content collection entries.
4
4
 
5
- - <strong>[Why Markdoc?](#why-markdoc)</strong>
6
- - <strong>[Installation](#installation)</strong>
7
- - <strong>[Usage](#usage)</strong>
8
- - <strong>[Configuration](#configuration)</strong>
9
- - <strong>[Examples](#examples)</strong>
10
- - <strong>[Troubleshooting](#troubleshooting)</strong>
11
- - <strong>[Contributing](#contributing)</strong>
12
- - <strong>[Changelog](#changelog)</strong>
5
+ ## Documentation
13
6
 
14
- ## Why Markdoc?
7
+ Read the [`@astrojs/markdoc` docs][docs]
15
8
 
16
- Markdoc allows you to enhance your Markdown with [Astro components][astro-components]. If you have existing content authored in Markdoc, this integration allows you to bring those files to your Astro project using content collections.
9
+ ## Support
17
10
 
18
- ## Installation
11
+ - Get help in the [Astro Discord][discord]. Post questions in our `#support` forum, or visit our dedicated `#dev` channel to discuss current development and more!
19
12
 
20
- ### Quick Install
13
+ - Check our [Astro Integration Documentation][astro-integration] for more on integrations.
21
14
 
22
- The `astro add` command-line tool automates the installation for you. Run one of the following commands in a new terminal window. (If you aren't sure which package manager you're using, run the first command.) Then, follow the prompts, and type "y" in the terminal (meaning "yes") for each one.
15
+ - Submit bug reports and feature requests as [GitHub issues][issues].
23
16
 
24
- ```sh
25
- # Using NPM
26
- npx astro add markdoc
27
- # Using Yarn
28
- yarn astro add markdoc
29
- # Using PNPM
30
- pnpm astro add markdoc
31
- ```
32
-
33
- If you run into any issues, [feel free to report them to us on GitHub](https://github.com/withastro/astro/issues) and try the manual installation steps below.
34
-
35
- ### Manual Install
36
-
37
- First, install the `@astrojs/markdoc` package using your package manager. If you're using npm or aren't sure, run this in the terminal:
38
-
39
- ```sh
40
- npm install @astrojs/markdoc
41
- ```
42
-
43
- Then, apply this integration to your `astro.config.*` file using the `integrations` property:
44
-
45
- ```js ins={3} "markdoc()"
46
- // astro.config.mjs
47
- import { defineConfig } from 'astro/config';
48
- import markdoc from '@astrojs/markdoc';
49
-
50
- export default defineConfig({
51
- // ...
52
- integrations: [markdoc()],
53
- });
54
- ```
55
-
56
- ### Editor Integration
57
-
58
- [VS Code](https://code.visualstudio.com/) supports Markdown by default. However, for Markdoc editor support, you may wish to add the following setting in your VSCode config. This ensures authoring Markdoc files provides a Markdown-like editor experience.
59
-
60
- ```json title=".vscode/settings.json"
61
- {
62
- "files.associations": {
63
- "*.mdoc": "markdown"
64
- }
65
- }
66
- ```
67
-
68
- ## Usage
69
-
70
- Markdoc files can only be used within content collections. Add entries to any content collection using the `.mdoc` extension:
71
-
72
- ```sh
73
- src/content/docs/
74
- why-markdoc.mdoc
75
- quick-start.mdoc
76
- ```
77
-
78
- Then, query your collection using the [Content Collection APIs](https://docs.astro.build/en/guides/content-collections/#querying-collections):
79
-
80
- ```astro
81
- ---
82
- import { getEntryBySlug } from 'astro:content';
83
-
84
- const entry = await getEntryBySlug('docs', 'why-markdoc');
85
- const { Content } = await entry.render();
86
- ---
87
-
88
- <!--Access frontmatter properties with `data`-->
89
- <h1>{entry.data.title}</h1>
90
- <!--Render Markdoc contents with the Content component-->
91
- <Content />
92
- ```
93
-
94
- 📚 See the [Astro Content Collection docs][astro-content-collections] for more information.
95
-
96
- ## Markdoc config
97
-
98
- `@astrojs/markdoc` offers configuration options to use all of Markdoc's features and connect UI components to your content.
99
-
100
- ### Use Astro components as Markdoc tags
101
-
102
- You can configure [Markdoc tags][markdoc-tags] that map to `.astro` components. You can add a new tag by creating a `markdoc.config.mjs|ts` file at the root of your project and configuring the `tag` attribute.
103
-
104
- This example renders an `Aside` component, and allows a `type` prop to be passed as a string:
105
-
106
- ```js
107
- // markdoc.config.mjs
108
- import { defineMarkdocConfig, component } from '@astrojs/markdoc/config';
109
-
110
- export default defineMarkdocConfig({
111
- tags: {
112
- aside: {
113
- render: component('./src/components/Aside.astro'),
114
- attributes: {
115
- // Markdoc requires type defs for each attribute.
116
- // These should mirror the `Props` type of the component
117
- // you are rendering.
118
- // See Markdoc's documentation on defining attributes
119
- // https://markdoc.dev/docs/attributes#defining-attributes
120
- type: { type: String },
121
- },
122
- },
123
- },
124
- });
125
- ```
126
-
127
- This component can now be used in your Markdoc files with the `{% aside %}` tag. Children will be passed to your component's default slot:
128
-
129
- ```md
130
- # Welcome to Markdoc 👋
131
-
132
- {% aside type="tip" %}
133
-
134
- Use tags like this fancy "aside" to add some _flair_ to your docs.
135
-
136
- {% /aside %}
137
- ```
138
-
139
- ### Use Astro components from npm packages and TypeScript files
140
-
141
- You may need to use Astro components exposed as named exports from TypeScript or JavaScript files. This is common when using npm packages and design systems.
142
-
143
- You can pass the import name as the second argument to the `component()` function:
144
-
145
- ```js
146
- // markdoc.config.mjs
147
- import { defineMarkdocConfig, component } from '@astrojs/markdoc/config';
148
-
149
- export default defineMarkdocConfig({
150
- tags: {
151
- tabs: {
152
- render: component('@astrojs/starlight/components', 'Tabs'),
153
- },
154
- },
155
- });
156
- ```
157
-
158
- This generates the following import statement internally:
159
-
160
- ```ts
161
- import { Tabs } from '@astrojs/starlight/components';
162
- ```
163
-
164
- ### Custom headings
165
-
166
- `@astrojs/markdoc` automatically adds anchor links to your headings, and [generates a list of `headings` via the content collections API](https://docs.astro.build/en/guides/content-collections/#rendering-content-to-html). To further customize how headings are rendered, you can apply an Astro component [as a Markdoc node][markdoc-nodes].
167
-
168
- This example renders a `Heading.astro` component using the `render` property:
169
-
170
- ```js
171
- // markdoc.config.mjs
172
- import { defineMarkdocConfig, nodes, component } from '@astrojs/markdoc/config';
173
-
174
- export default defineMarkdocConfig({
175
- nodes: {
176
- heading: {
177
- ...nodes.heading, // Preserve default anchor link generation
178
- render: component('./src/components/Heading.astro'),
179
- },
180
- },
181
- });
182
- ```
183
-
184
- All Markdown headings will render the `Heading.astro` component and pass the following `attributes` as component props:
185
-
186
- - `level: number` The heading level 1 - 6
187
- - `id: string` An `id` generated from the heading's text contents. This corresponds to the `slug` generated by the [content `render()` function](https://docs.astro.build/en/guides/content-collections/#rendering-content-to-html).
188
-
189
- For example, the heading `### Level 3 heading!` will pass `level: 3` and `id: 'level-3-heading'` as component props.
190
-
191
- ### Syntax highlighting
192
-
193
- `@astrojs/markdoc` provides [Shiki](https://github.com/shikijs/shiki) and [Prism](https://github.com/PrismJS) extensions to highlight your code blocks.
194
-
195
- #### Shiki
196
-
197
- Apply the `shiki()` extension to your Markdoc config using the `extends` property. You can optionally pass a shiki configuration object:
198
-
199
- ```js
200
- // markdoc.config.mjs
201
- import { defineMarkdocConfig } from '@astrojs/markdoc/config';
202
- import shiki from '@astrojs/markdoc/shiki';
203
-
204
- export default defineMarkdocConfig({
205
- extends: [
206
- shiki({
207
- // Choose from Shiki's built-in themes (or add your own)
208
- // Default: 'github-dark'
209
- // https://github.com/shikijs/shiki/blob/main/docs/themes.md
210
- theme: 'dracula',
211
- // Enable word wrap to prevent horizontal scrolling
212
- // Default: false
213
- wrap: true,
214
- // Pass custom languages
215
- // Note: Shiki has countless langs built-in, including `.astro`!
216
- // https://github.com/shikijs/shiki/blob/main/docs/languages.md
217
- langs: [],
218
- }),
219
- ],
220
- });
221
- ```
222
-
223
- #### Prism
224
-
225
- Apply the `prism()` extension to your Markdoc config using the `extends` property.
226
-
227
- ```js
228
- // markdoc.config.mjs
229
- import { defineMarkdocConfig } from '@astrojs/markdoc/config';
230
- import prism from '@astrojs/markdoc/prism';
231
-
232
- export default defineMarkdocConfig({
233
- extends: [prism()],
234
- });
235
- ```
236
-
237
- 📚 To learn about configuring Prism stylesheets, [see our syntax highlighting guide](https://docs.astro.build/en/guides/markdown-content/#prism-configuration).
238
-
239
- ### Set the root HTML element
240
-
241
- Markdoc wraps documents with an `<article>` tag by default. This can be changed from the `document` Markdoc node. This accepts an HTML element name or `null` if you prefer to remove the wrapper element:
242
-
243
- ```js
244
- // markdoc.config.mjs
245
- import { defineMarkdocConfig, nodes } from '@astrojs/markdoc/config';
246
-
247
- export default defineMarkdocConfig({
248
- nodes: {
249
- document: {
250
- ...nodes.document, // Apply defaults for other options
251
- render: null, // default 'article'
252
- },
253
- },
254
- });
255
- ```
256
-
257
- ### Custom Markdoc nodes / elements
258
-
259
- You may want to render standard Markdown elements, such as paragraphs and bolded text, as Astro components. For this, you can configure a [Markdoc node][markdoc-nodes]. If a given node receives attributes, they will be available as component props.
260
-
261
- This example renders blockquotes with a custom `Quote.astro` component:
262
-
263
- ```js
264
- // markdoc.config.mjs
265
- import { defineMarkdocConfig, nodes, component } from '@astrojs/markdoc/config';
266
-
267
- export default defineMarkdocConfig({
268
- nodes: {
269
- blockquote: {
270
- ...nodes.blockquote, // Apply Markdoc's defaults for other options
271
- render: component('./src/components/Quote.astro'),
272
- },
273
- },
274
- });
275
- ```
276
-
277
- 📚 [Find all of Markdoc's built-in nodes and node attributes on their documentation.](https://markdoc.dev/docs/nodes#built-in-nodes)
278
-
279
- ### Use client-side UI components
280
-
281
- Tags and nodes are restricted to `.astro` files. To embed client-side UI components in Markdoc, [use a wrapper `.astro` component that renders a framework component](/en/core-concepts/framework-components/#nesting-framework-components) with your desired `client:` directive.
282
-
283
- This example wraps a React `Aside.tsx` component with a `ClientAside.astro` component:
284
-
285
- ```astro
286
- ---
287
- // src/components/ClientAside.astro
288
- import Aside from './Aside';
289
- ---
290
-
291
- <Aside {...Astro.props} client:load />
292
- ```
293
-
294
- This Astro component can now be passed to the `render` prop for any [tag][markdoc-tags] or [node][markdoc-nodes] in your config:
295
-
296
- ```js
297
- // markdoc.config.mjs
298
- import { defineMarkdocConfig, component } from '@astrojs/markdoc/config';
299
-
300
- export default defineMarkdocConfig({
301
- tags: {
302
- aside: {
303
- render: component('./src/components/ClientAside.astro'),
304
- attributes: {
305
- type: { type: String },
306
- },
307
- },
308
- },
309
- });
310
- ```
311
-
312
- ### Markdoc config
313
-
314
- 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).
315
-
316
- You can pass these options from the default export in your `markdoc.config.mjs|ts` file:
317
-
318
- ```js
319
- // markdoc.config.mjs
320
- import { defineMarkdocConfig } from '@astrojs/markdoc/config';
321
-
322
- export default defineMarkdocConfig({
323
- functions: {
324
- getCountryEmoji: {
325
- transform(parameters) {
326
- const [country] = Object.values(parameters);
327
- const countryToEmojiMap = {
328
- japan: '🇯🇵',
329
- spain: '🇪🇸',
330
- france: '🇫🇷',
331
- };
332
- return countryToEmojiMap[country] ?? '🏳';
333
- },
334
- },
335
- },
336
- });
337
- ```
338
-
339
- Now, you can call this function from any Markdoc content entry:
340
-
341
- ```md
342
- ¡Hola {% getCountryEmoji("spain") %}!
343
- ```
344
-
345
- 📚 [See the Markdoc documentation](https://markdoc.dev/docs/functions#creating-a-custom-function) for more on using variables or functions in your content.
346
-
347
- ### Pass Markdoc variables
348
-
349
- You may need to pass [variables][markdoc-variables] to your content. This is useful when passing SSR parameters like A/B tests.
350
-
351
- Variables can be passed as props via the `Content` component:
352
-
353
- ```astro
354
- ---
355
- import { getEntryBySlug } from 'astro:content';
356
-
357
- const entry = await getEntryBySlug('docs', 'why-markdoc');
358
- const { Content } = await entry.render();
359
- ---
360
-
361
- <!--Pass the `abTest` param as a variable-->
362
- <Content abTestGroup={Astro.params.abTestGroup} />
363
- ```
364
-
365
- Now, `abTestGroup` is available as a variable in `docs/why-markdoc.mdoc`:
366
-
367
- ```md
368
- {% if $abTestGroup === 'image-optimization-lover' %}
369
-
370
- Let me tell you about image optimization...
371
-
372
- {% /if %}
373
- ```
374
-
375
- To make a variable global to all Markdoc files, you can use the `variables` attribute from your `markdoc.config.mjs|ts`:
376
-
377
- ```js
378
- import { defineMarkdocConfig } from '@astrojs/markdoc/config';
379
-
380
- export default defineMarkdocConfig({
381
- variables: {
382
- environment: process.env.IS_PROD ? 'prod' : 'dev',
383
- },
384
- });
385
- ```
386
-
387
- ### Access frontmatter from your Markdoc content
388
-
389
- To access frontmatter, you can pass the entry `data` property [as a variable](#pass-markdoc-variables) where you render your content:
390
-
391
- ```astro
392
- ---
393
- import { getEntry } from 'astro:content';
394
-
395
- const entry = await getEntry('docs', 'why-markdoc');
396
- const { Content } = await entry.render();
397
- ---
398
-
399
- <Content frontmatter={entry.data} />
400
- ```
401
-
402
- This can now be accessed as `$frontmatter` in your Markdoc.
403
-
404
- ## Integration config options
405
-
406
- The Astro Markdoc integration handles configuring Markdoc options and capabilities that are not available through the `markdoc.config.js` file.
407
-
408
- ### `allowHTML`
409
-
410
- Enables writing HTML markup alongside Markdoc tags and nodes.
411
-
412
- By default, Markdoc will not recognize HTML markup as semantic content.
413
-
414
- To achieve a more Markdown-like experience, where HTML elements can be included alongside your content, set `allowHTML:true` as a `markdoc` integration option. This will enable HTML parsing in Markdoc markup.
415
-
416
- ```js {7} "allowHTML: true"
417
- // astro.config.mjs
418
- import { defineConfig } from 'astro/config';
419
- import markdoc from '@astrojs/markdoc';
420
-
421
- export default defineConfig({
422
- // ...
423
- integrations: [markdoc({ allowHTML: true })],
424
- });
425
- ```
426
-
427
- > **Warning**
428
- > When `allowHTML` is enabled, HTML markup inside Markdoc documents will be rendered as actual HTML elements (including `<script>`), making attack vectors like XSS possible. Ensure that any HTML markup comes from trusted sources.
429
-
430
- ## Examples
431
-
432
- - The [Astro Markdoc starter template](https://github.com/withastro/astro/tree/latest/examples/with-markdoc) shows how to use Markdoc files in your Astro project.
433
-
434
- ## Troubleshooting
435
-
436
- For help, check out the `#support` channel on [Discord](https://astro.build/chat). Our friendly Support Squad members are here to help!
17
+ ## Contributing
437
18
 
438
- You can also check our [Astro Integration Documentation][astro-integration] for more on integrations.
19
+ This package is maintained by Astro's Core team. You're welcome to submit an issue or PR! These links will help you get started:
439
20
 
440
- ## Contributing
21
+ - [Contributor Manual][contributing]
22
+ - [Code of Conduct][coc]
23
+ - [Community Guide][community]
441
24
 
442
- This package is maintained by Astro's Core team. You're welcome to submit an issue or PR!
25
+ ## License
443
26
 
444
- ## Changelog
27
+ MIT
445
28
 
446
- See [CHANGELOG.md](https://github.com/withastro/astro/tree/main/packages/integrations/markdoc/CHANGELOG.md) for a history of changes to this integration.
29
+ Copyright (c) 2023–present [Astro][astro]
447
30
 
31
+ [astro]: https://astro.build/
32
+ [docs]: https://docs.astro.build/en/guides/integrations-guide/markdoc/
33
+ [contributing]: https://github.com/withastro/astro/blob/main/CONTRIBUTING.md
34
+ [coc]: https://github.com/withastro/.github/blob/main/CODE_OF_CONDUCT.md
35
+ [community]: https://github.com/withastro/.github/blob/main/COMMUNITY_GUIDE.md
36
+ [discord]: https://astro.build/chat/
37
+ [issues]: https://github.com/withastro/astro/issues
448
38
  [astro-integration]: https://docs.astro.build/en/guides/integrations-guide/
449
- [astro-components]: https://docs.astro.build/en/core-concepts/astro-components/
450
- [astro-content-collections]: https://docs.astro.build/en/guides/content-collections/
451
- [markdoc-tags]: https://markdoc.dev/docs/tags
452
- [markdoc-nodes]: https://markdoc.dev/docs/nodes
453
- [markdoc-variables]: https://markdoc.dev/docs/variables
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  //! astro-head-inject
3
- import type { Config } from '@markdoc/markdoc';
3
+ import type { Config, RenderableTreeNodes } from '@markdoc/markdoc';
4
4
  import Markdoc from '@markdoc/markdoc';
5
5
  import { ComponentNode, createTreeNode } from './TreeNode.js';
6
6
 
@@ -12,13 +12,12 @@ type Props = {
12
12
  const { stringifiedAst, config } = Astro.props as Props;
13
13
 
14
14
  const ast = Markdoc.Ast.fromJSON(stringifiedAst);
15
- const content = Markdoc.transform(ast, config);
15
+ // The AST may be an array, and `transform` has overloads for arrays and non-array cases,
16
+ // However TypeScript seems to struggle to combine both overloads into a single signature.
17
+ // Also, `transform` returns a promise here but the types don't reflect that.
18
+ // @ts-expect-error
19
+ const content = (await Markdoc.transform(ast, config)) as RenderableTreeNodes;
20
+ const treeNode = await createTreeNode(content);
16
21
  ---
17
22
 
18
- {
19
- Array.isArray(content) ? (
20
- content.map(async (c) => <ComponentNode treeNode={await createTreeNode(c)} />)
21
- ) : (
22
- <ComponentNode treeNode={await createTreeNode(content)} />
23
- )
24
- }
23
+ <ComponentNode treeNode={treeNode} />