@astrojs/markdoc 0.8.1 β†’ 0.8.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.
Files changed (2) hide show
  1. package/README.md +21 -501
  2. package/package.json +3 -4
package/README.md CHANGED
@@ -2,517 +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
- ```diff lang="js" "markdoc()"
46
- // astro.config.mjs
47
- import { defineConfig } from 'astro/config';
48
- + import markdoc from '@astrojs/markdoc';
49
- export default defineConfig({
50
- // ...
51
- integrations: [markdoc()],
52
- // ^^^^^^^^^
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](https://docs.astro.build/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
- ### Markdoc Language Server
348
-
349
- If you are using VS Code, there is an official [Markdoc language extension](https://marketplace.visualstudio.com/items?itemName=Stripe.markdoc-language-support) that includes syntax highlighting and autocomplete for configured tags. [See the language server on GitHub](https://github.com/markdoc/language-server.git) for more information.
350
-
351
- To set up the extension, create a `markdoc.config.json` file into the project root with following content:
352
-
353
- ```json
354
- [
355
- {
356
- "id": "my-site",
357
- "path": "src/content",
358
- "schema": {
359
- "path": "markdoc.config.mjs",
360
- "type": "esm",
361
- "property": "default",
362
- "watch": true
363
- }
364
- }
365
- ]
366
- ```
367
-
368
- The `schema` property contains all information to configure the language server for Astro content collections. It accepts following properties:
369
-
370
- - `path`: The path to the configuration file.
371
- - `type`: The type of module your configuration file uses (`esm` allows `import` syntax).
372
- - `property`: The exported property name that contains the configuration object.
373
- - `watch`: Tell the server to watch for changes in the configuration.
374
-
375
- The top-level `path` property tells the server where content is located. Since Markdoc is specific to content collections, you can use `src/content`.
376
-
377
- ### Pass Markdoc variables
378
-
379
- You may need to pass [variables][markdoc-variables] to your content. This is useful when passing SSR parameters like A/B tests.
380
-
381
- Variables can be passed as props via the `Content` component:
382
-
383
- ```astro
384
- ---
385
- import { getEntryBySlug } from 'astro:content';
386
-
387
- const entry = await getEntryBySlug('docs', 'why-markdoc');
388
- const { Content } = await entry.render();
389
- ---
390
-
391
- <!--Pass the `abTest` param as a variable-->
392
- <Content abTestGroup={Astro.params.abTestGroup} />
393
- ```
394
-
395
- Now, `abTestGroup` is available as a variable in `docs/why-markdoc.mdoc`:
396
-
397
- ```md
398
- {% if $abTestGroup === 'image-optimization-lover' %}
399
-
400
- Let me tell you about image optimization...
401
-
402
- {% /if %}
403
- ```
404
-
405
- To make a variable global to all Markdoc files, you can use the `variables` attribute from your `markdoc.config.mjs|ts`:
406
-
407
- ```js
408
- import { defineMarkdocConfig } from '@astrojs/markdoc/config';
409
-
410
- export default defineMarkdocConfig({
411
- variables: {
412
- environment: process.env.IS_PROD ? 'prod' : 'dev',
413
- },
414
- });
415
- ```
416
-
417
- ### Access frontmatter from your Markdoc content
418
-
419
- To access frontmatter, you can pass the entry `data` property [as a variable](#pass-markdoc-variables) where you render your content:
420
-
421
- ```astro
422
- ---
423
- import { getEntry } from 'astro:content';
424
-
425
- const entry = await getEntry('docs', 'why-markdoc');
426
- const { Content } = await entry.render();
427
- ---
428
-
429
- <Content frontmatter={entry.data} />
430
- ```
431
-
432
- This can now be accessed as `$frontmatter` in your Markdoc.
433
-
434
- ## Integration config options
435
-
436
- The Astro Markdoc integration handles configuring Markdoc options and capabilities that are not available through the `markdoc.config.js` file.
437
-
438
- ### `allowHTML`
439
-
440
- Enables writing HTML markup alongside Markdoc tags and nodes.
441
-
442
- By default, Markdoc will not recognize HTML markup as semantic content.
443
-
444
- 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.
445
-
446
- ```diff lang="js" "allowHTML: true"
447
- // astro.config.mjs
448
- import { defineConfig } from 'astro/config';
449
- import markdoc from '@astrojs/markdoc';
450
-
451
- export default defineConfig({
452
- // ...
453
- + integrations: [markdoc({ allowHTML: true })],
454
- // ^^^^^^^^^^^^^^^
455
- });
456
- ```
457
-
458
- > **Warning**
459
- > 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.
460
-
461
- ### `ignoreIndentation`
462
-
463
- By default, any content that is indented by four spaces is treated as a code block. Unfortunately, this behavior makes it difficult to use arbitrary levels of indentation to improve the readability of documents with complex structure.
464
-
465
- When using nested tags in Markdoc, it can be helpful to indent the content inside of tags so that the level of depth is clear. To support arbitrary indentation, we have to disable the indent-based code blocks and modify several other markdown-it parsing rules that account for indent-based code blocks. These changes can be applied by enabling the ignoreIndentation option.
466
-
467
- ```diff lang="js" "ignoreIndentation: true"
468
- // astro.config.mjs
469
- import { defineConfig } from 'astro/config';
470
- import markdoc from '@astrojs/markdoc';
471
-
472
- export default defineConfig({
473
- // ...
474
- + integrations: [markdoc({ ignoreIndentation: true })],
475
- // ^^^^^^^^^^^^^^^^^^^^^^^
476
- });
477
- ```
478
-
479
- ```md
480
- # Welcome to Markdoc with indented tags πŸ‘‹
481
-
482
- # Note: Can use either spaces or tabs for indentation
483
-
484
- {% custom-tag %}
485
- {% custom-tag %} ### Tags can be indented for better readability
486
-
487
- {% another-custom-tag %}
488
- This is easier to follow when there is a lot of nesting
489
- {% /another-custom-tag %}
490
-
491
- {% /custom-tag %}
492
- {% /custom-tag %}
493
- ```
494
-
495
- ## Examples
496
-
497
- - 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.
498
-
499
- ## Troubleshooting
500
-
501
- For help, check out the `#support` channel on [Discord](https://astro.build/chat). Our friendly Support Squad members are here to help!
17
+ ## Contributing
502
18
 
503
- 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:
504
20
 
505
- ## Contributing
21
+ - [Contributor Manual][contributing]
22
+ - [Code of Conduct][coc]
23
+ - [Community Guide][community]
506
24
 
507
- This package is maintained by Astro's Core team. You're welcome to submit an issue or PR!
25
+ ## License
508
26
 
509
- ## Changelog
27
+ MIT
510
28
 
511
- 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]
512
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
513
38
  [astro-integration]: https://docs.astro.build/en/guides/integrations-guide/
514
- [astro-components]: https://docs.astro.build/en/core-concepts/astro-components/
515
- [astro-content-collections]: https://docs.astro.build/en/guides/content-collections/
516
- [markdoc-tags]: https://markdoc.dev/docs/tags
517
- [markdoc-nodes]: https://markdoc.dev/docs/nodes
518
- [markdoc-variables]: https://markdoc.dev/docs/variables
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@astrojs/markdoc",
3
3
  "description": "Add support for Markdoc in your Astro site",
4
- "version": "0.8.1",
4
+ "version": "0.8.3",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -62,7 +62,6 @@
62
62
  "gray-matter": "^4.0.3",
63
63
  "htmlparser2": "^9.0.0",
64
64
  "kleur": "^4.1.5",
65
- "shikiji": "^0.6.13",
66
65
  "zod": "^3.22.4",
67
66
  "@astrojs/internal-helpers": "0.2.1",
68
67
  "@astrojs/prism": "3.0.0"
@@ -80,8 +79,8 @@
80
79
  "linkedom": "^0.16.4",
81
80
  "mocha": "^10.2.0",
82
81
  "vite": "^5.0.10",
83
- "@astrojs/markdown-remark": "4.0.1",
84
- "astro": "4.0.7",
82
+ "@astrojs/markdown-remark": "4.1.0",
83
+ "astro": "4.2.0",
85
84
  "astro-scripts": "0.0.14"
86
85
  },
87
86
  "engines": {