@astrojs/markdoc 0.0.0-head-prop-20230323183456
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 -0
- package/CHANGELOG.md +27 -0
- package/LICENSE +61 -0
- package/README.md +348 -0
- package/components/RenderNode.astro +30 -0
- package/components/Renderer.astro +21 -0
- package/components/astroNode.ts +51 -0
- package/components/index.ts +2 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +104 -0
- package/dist/utils.d.ts +50 -0
- package/dist/utils.js +92 -0
- package/package.json +54 -0
- package/src/index.ts +122 -0
- package/src/utils.ts +147 -0
- package/template/content-module-types.d.ts +9 -0
- package/test/content-collections.test.js +258 -0
- package/test/fixtures/content-collections/astro.config.mjs +7 -0
- package/test/fixtures/content-collections/node_modules/.bin/astro +17 -0
- package/test/fixtures/content-collections/package.json +13 -0
- package/test/fixtures/content-collections/src/components/Code.astro +12 -0
- package/test/fixtures/content-collections/src/components/CustomMarquee.astro +1 -0
- package/test/fixtures/content-collections/src/content/blog/simple.mdoc +7 -0
- package/test/fixtures/content-collections/src/content/blog/with-components.mdoc +17 -0
- package/test/fixtures/content-collections/src/content/blog/with-config.mdoc +9 -0
- package/test/fixtures/content-collections/src/content/config.ts +12 -0
- package/test/fixtures/content-collections/src/pages/collection.json.js +10 -0
- package/test/fixtures/content-collections/src/pages/content-simple.astro +18 -0
- package/test/fixtures/content-collections/src/pages/content-with-components.astro +23 -0
- package/test/fixtures/content-collections/src/pages/content-with-config.astro +19 -0
- package/test/fixtures/content-collections/src/pages/entry.json.js +10 -0
- package/test/fixtures/content-collections/utils.js +8 -0
- package/tsconfig.json +10 -0
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
[35m@astrojs/markdoc:build: [0mcache hit, replaying output [2m9d5f2e18e3f37f97[0m
|
|
2
|
+
[35m@astrojs/markdoc:build: [0m
|
|
3
|
+
[35m@astrojs/markdoc:build: [0m> @astrojs/markdoc@0.0.0-head-prop-20230323183456 build /home/runner/work/astro/astro/packages/integrations/markdoc
|
|
4
|
+
[35m@astrojs/markdoc:build: [0m> astro-scripts build "src/**/*.ts" && tsc
|
|
5
|
+
[35m@astrojs/markdoc:build: [0m
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# @astrojs/markdoc
|
|
2
|
+
|
|
3
|
+
## 0.0.0-head-prop-20230323183456
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#6552](https://github.com/withastro/astro/pull/6552) [`392ba3e4d`](https://github.com/withastro/astro/commit/392ba3e4d55f73ce9194bd94a2243f1aa62af079) Thanks [@bluwy](https://github.com/bluwy)! - Fix integration return type
|
|
8
|
+
|
|
9
|
+
## 0.0.2
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#6494](https://github.com/withastro/astro/pull/6494) [`a13e9d7e3`](https://github.com/withastro/astro/commit/a13e9d7e33baccf51e7d4815f99b481ad174bc57) Thanks [@Yan-Thomas](https://github.com/Yan-Thomas)! - Consistency improvements to several package descriptions
|
|
14
|
+
|
|
15
|
+
## 0.0.1
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- [#6209](https://github.com/withastro/astro/pull/6209) [`fec583909`](https://github.com/withastro/astro/commit/fec583909ab62829dc0c1600e2387979365f2b94) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Introduce the (experimental) `@astrojs/markdoc` integration. This unlocks Markdoc inside your Content Collections, bringing support for Astro and UI components in your content. This also improves Astro core internals to make Content Collections extensible to more file types in the future.
|
|
20
|
+
|
|
21
|
+
You can install this integration using the `astro add` command:
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
astro add markdoc
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
[Read the `@astrojs/markdoc` documentation](https://docs.astro.build/en/guides/integrations-guide/markdoc/) for usage instructions, and browse the [new `with-markdoc` starter](https://astro.new/with-markdoc) to try for yourself.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Fred K. Schott
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
"""
|
|
25
|
+
This license applies to parts of the `packages/create-astro` and `packages/astro` subdirectories originating from the https://github.com/sveltejs/kit repository:
|
|
26
|
+
|
|
27
|
+
Copyright (c) 2020 [these people](https://github.com/sveltejs/kit/graphs/contributors)
|
|
28
|
+
|
|
29
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
30
|
+
|
|
31
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
32
|
+
|
|
33
|
+
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
|
+
"""
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
"""
|
|
38
|
+
This license applies to parts of the `packages/create-astro` and `packages/astro` subdirectories originating from the https://github.com/vitejs/vite repository:
|
|
39
|
+
|
|
40
|
+
MIT License
|
|
41
|
+
|
|
42
|
+
Copyright (c) 2019-present, Yuxi (Evan) You and Vite contributors
|
|
43
|
+
|
|
44
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
45
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
46
|
+
in the Software without restriction, including without limitation the rights
|
|
47
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
48
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
49
|
+
furnished to do so, subject to the following conditions:
|
|
50
|
+
|
|
51
|
+
The above copyright notice and this permission notice shall be included in all
|
|
52
|
+
copies or substantial portions of the Software.
|
|
53
|
+
|
|
54
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
55
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
56
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
57
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
58
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
59
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
60
|
+
SOFTWARE.
|
|
61
|
+
"""
|
package/README.md
ADDED
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
# @astrojs/markdoc (experimental) 📝
|
|
2
|
+
|
|
3
|
+
This **[Astro integration][astro-integration]** enables the usage of [Markdoc](https://markdoc.dev/) to create components, pages, and content collection entries.
|
|
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>
|
|
13
|
+
|
|
14
|
+
## Why Markdoc?
|
|
15
|
+
|
|
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.
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
### Quick Install
|
|
21
|
+
|
|
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.
|
|
23
|
+
|
|
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
|
+
__`astro.config.mjs`__
|
|
46
|
+
|
|
47
|
+
```js ins={2} "markdoc()"
|
|
48
|
+
import { defineConfig } from 'astro/config';
|
|
49
|
+
import markdoc from '@astrojs/markdoc';
|
|
50
|
+
|
|
51
|
+
export default defineConfig({
|
|
52
|
+
// ...
|
|
53
|
+
integrations: [markdoc()],
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
### Editor Integration
|
|
59
|
+
|
|
60
|
+
[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.
|
|
61
|
+
|
|
62
|
+
```json title=".vscode/settings.json"
|
|
63
|
+
"files.associations": {
|
|
64
|
+
"*.mdoc": "markdown"
|
|
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
|
+
## Configuration
|
|
97
|
+
|
|
98
|
+
`@astrojs/markdoc` offers configuration options to use all of Markdoc's features and connect UI components to your content.
|
|
99
|
+
|
|
100
|
+
### Using components
|
|
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].
|
|
103
|
+
|
|
104
|
+
#### Render Markdoc tags as Astro components
|
|
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.
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
```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
|
+
});
|
|
132
|
+
```
|
|
133
|
+
|
|
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:
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
```astro
|
|
138
|
+
---
|
|
139
|
+
import { getEntryBySlug } from 'astro:content';
|
|
140
|
+
import Aside from '../components/Aside.astro';
|
|
141
|
+
|
|
142
|
+
const entry = await getEntryBySlug('docs', 'why-markdoc');
|
|
143
|
+
const { Content } = await entry.render();
|
|
144
|
+
---
|
|
145
|
+
|
|
146
|
+
<Content
|
|
147
|
+
components={{ Aside }}
|
|
148
|
+
/>
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
#### Render Markdoc nodes / HTML elements as Astro components
|
|
152
|
+
|
|
153
|
+
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
|
+
|
|
155
|
+
```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
|
+
/>
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
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:
|
|
199
|
+
|
|
200
|
+
```md
|
|
201
|
+
### I'm a level 3 heading!
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
📚 [Find all of Markdoc's built-in nodes and node attributes on their documentation.](https://markdoc.dev/docs/nodes#built-in-nodes)
|
|
205
|
+
|
|
206
|
+
#### Use client-side UI components
|
|
207
|
+
|
|
208
|
+
Today, the `components` prop does not support the `client:` directive for hydrating components. To embed client-side components, create a wrapper `.astro` file to import your component and apply a `client:` directive manually.
|
|
209
|
+
|
|
210
|
+
This example wraps a `Aside.tsx` component with a `ClientAside.astro` wrapper:
|
|
211
|
+
|
|
212
|
+
```astro
|
|
213
|
+
---
|
|
214
|
+
// src/components/ClientAside.astro
|
|
215
|
+
import Aside from './Aside';
|
|
216
|
+
---
|
|
217
|
+
|
|
218
|
+
<Aside client:load />
|
|
219
|
+
```
|
|
220
|
+
|
|
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
|
+
---
|
|
232
|
+
|
|
233
|
+
<Content
|
|
234
|
+
components={{
|
|
235
|
+
Aside: ClientAside,
|
|
236
|
+
}}
|
|
237
|
+
/>
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
### Markdoc config
|
|
241
|
+
|
|
242
|
+
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).
|
|
243
|
+
|
|
244
|
+
You can pass these options from the `markdoc()` integration in your `astro.config`. This example adds a global `getCountryEmoji` function:
|
|
245
|
+
|
|
246
|
+
```js
|
|
247
|
+
// astro.config.mjs
|
|
248
|
+
import { defineConfig } from 'astro/config';
|
|
249
|
+
import markdoc from '@astrojs/markdoc';
|
|
250
|
+
|
|
251
|
+
// https://astro.build/config
|
|
252
|
+
export default defineConfig({
|
|
253
|
+
integrations: [
|
|
254
|
+
markdoc({
|
|
255
|
+
functions: {
|
|
256
|
+
getCountryEmoji: {
|
|
257
|
+
transform(parameters) {
|
|
258
|
+
const [country] = Object.values(parameters);
|
|
259
|
+
const countryToEmojiMap = {
|
|
260
|
+
japan: '🇯🇵',
|
|
261
|
+
spain: '🇪🇸',
|
|
262
|
+
france: '🇫🇷',
|
|
263
|
+
}
|
|
264
|
+
return countryToEmojiMap[country] ?? '🏳'
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
},
|
|
268
|
+
}),
|
|
269
|
+
],
|
|
270
|
+
});
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
Now, you can call this function from any Markdoc content entry:
|
|
274
|
+
|
|
275
|
+
```md
|
|
276
|
+
¡Hola {% getCountryEmoji("spain") %}!
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
:::note
|
|
280
|
+
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).
|
|
281
|
+
:::
|
|
282
|
+
|
|
283
|
+
📚 [See the Markdoc documentation](https://markdoc.dev/docs/functions#creating-a-custom-function) for more on using variables or functions in your content.
|
|
284
|
+
|
|
285
|
+
### Define Markdoc configuration at runtime
|
|
286
|
+
|
|
287
|
+
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).
|
|
288
|
+
|
|
289
|
+
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`.
|
|
290
|
+
|
|
291
|
+
You will need to install the `@markdoc/markdoc` package into your project first:
|
|
292
|
+
|
|
293
|
+
```sh
|
|
294
|
+
# Using NPM
|
|
295
|
+
npx astro add @markdoc/markdoc
|
|
296
|
+
# Using Yarn
|
|
297
|
+
yarn astro add @markdoc/markdoc
|
|
298
|
+
# Using PNPM
|
|
299
|
+
pnpm astro add @markdoc/markdoc
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
Now, you can define Markdoc configuration options using `Markdock.transform()`.
|
|
303
|
+
|
|
304
|
+
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`:
|
|
305
|
+
|
|
306
|
+
```astro
|
|
307
|
+
---
|
|
308
|
+
import Markdoc from '@markdoc/markdoc';
|
|
309
|
+
import { Renderer } from '@astrojs/markdoc/components';
|
|
310
|
+
import { getEntryBySlug } from 'astro:content';
|
|
311
|
+
|
|
312
|
+
const { body } = await getEntryBySlug('docs', 'with-ab-test');
|
|
313
|
+
const ast = Markdoc.parse(body);
|
|
314
|
+
const content = Markdoc.transform({
|
|
315
|
+
variables: { abTestGroup: Astro.params.abTestGroup },
|
|
316
|
+
}, ast);
|
|
317
|
+
---
|
|
318
|
+
|
|
319
|
+
<Renderer {content} components={{ /* same `components` prop used by the `Content` component */ }} />
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
## Examples
|
|
323
|
+
|
|
324
|
+
* 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.
|
|
325
|
+
|
|
326
|
+
## Troubleshooting
|
|
327
|
+
|
|
328
|
+
For help, check out the `#support` channel on [Discord](https://astro.build/chat). Our friendly Support Squad members are here to help!
|
|
329
|
+
|
|
330
|
+
You can also check our [Astro Integration Documentation][astro-integration] for more on integrations.
|
|
331
|
+
|
|
332
|
+
## Contributing
|
|
333
|
+
|
|
334
|
+
This package is maintained by Astro's Core team. You're welcome to submit an issue or PR!
|
|
335
|
+
|
|
336
|
+
## Changelog
|
|
337
|
+
|
|
338
|
+
See [CHANGELOG.md](https://github.com/withastro/astro/tree/main/packages/integrations/markdoc/CHANGELOG.md) for a history of changes to this integration.
|
|
339
|
+
|
|
340
|
+
[astro-integration]: https://docs.astro.build/en/guides/integrations-guide/
|
|
341
|
+
|
|
342
|
+
[astro-components]: https://docs.astro.build/en/core-concepts/astro-components/
|
|
343
|
+
|
|
344
|
+
[astro-content-collections]: https://docs.astro.build/en/guides/content-collections/
|
|
345
|
+
|
|
346
|
+
[markdoc-tags]: https://markdoc.dev/docs/tags
|
|
347
|
+
|
|
348
|
+
[markdoc-nodes]: https://markdoc.dev/docs/nodes
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
---
|
|
2
|
+
import stringifyAttributes from 'stringify-attributes';
|
|
3
|
+
import type { AstroNode } from './astroNode';
|
|
4
|
+
|
|
5
|
+
type Props = {
|
|
6
|
+
node: AstroNode;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const Node = (Astro.props as Props).node;
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
{
|
|
13
|
+
typeof Node === 'string' ? (
|
|
14
|
+
<Fragment set:text={Node} />
|
|
15
|
+
) : 'component' in Node ? (
|
|
16
|
+
<Node.component {...Node.props}>
|
|
17
|
+
{Node.children.map((child) => (
|
|
18
|
+
<Astro.self node={child} />
|
|
19
|
+
))}
|
|
20
|
+
</Node.component>
|
|
21
|
+
) : (
|
|
22
|
+
<Fragment>
|
|
23
|
+
<Fragment set:html={`<${Node.tag} ${stringifyAttributes(Node.attributes)}>`} />
|
|
24
|
+
{Node.children.map((child) => (
|
|
25
|
+
<Astro.self node={child} />
|
|
26
|
+
))}
|
|
27
|
+
<Fragment set:html={`</${Node.tag}>`} />
|
|
28
|
+
</Fragment>
|
|
29
|
+
)
|
|
30
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
---
|
|
2
|
+
import type { RenderableTreeNode } from '@markdoc/markdoc';
|
|
3
|
+
import type { AstroInstance } from 'astro';
|
|
4
|
+
import { validateComponentsProp } from '../dist/utils.js';
|
|
5
|
+
import { createAstroNode } from './astroNode';
|
|
6
|
+
import RenderNode from './RenderNode.astro';
|
|
7
|
+
|
|
8
|
+
type Props = {
|
|
9
|
+
content: RenderableTreeNode;
|
|
10
|
+
components?: Record<string, AstroInstance['default']>;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const { content, components } = Astro.props as Props;
|
|
14
|
+
|
|
15
|
+
// Will throw if components is invalid
|
|
16
|
+
if (components) {
|
|
17
|
+
validateComponentsProp(components);
|
|
18
|
+
}
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
<RenderNode node={createAstroNode(content, components)} />
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { AstroInstance } from 'astro';
|
|
2
|
+
import type { RenderableTreeNode } from '@markdoc/markdoc';
|
|
3
|
+
import Markdoc from '@markdoc/markdoc';
|
|
4
|
+
import { MarkdocError, isCapitalized } from '../dist/utils.js';
|
|
5
|
+
|
|
6
|
+
export type AstroNode =
|
|
7
|
+
| string
|
|
8
|
+
| {
|
|
9
|
+
component: AstroInstance['default'];
|
|
10
|
+
props: Record<string, any>;
|
|
11
|
+
children: AstroNode[];
|
|
12
|
+
}
|
|
13
|
+
| {
|
|
14
|
+
tag: string;
|
|
15
|
+
attributes: Record<string, any>;
|
|
16
|
+
children: AstroNode[];
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export function createAstroNode(
|
|
20
|
+
node: RenderableTreeNode,
|
|
21
|
+
components: Record<string, AstroInstance['default']> = {}
|
|
22
|
+
): AstroNode {
|
|
23
|
+
if (typeof node === 'string' || typeof node === 'number') {
|
|
24
|
+
return String(node);
|
|
25
|
+
} else if (node === null || typeof node !== 'object' || !Markdoc.Tag.isTag(node)) {
|
|
26
|
+
return '';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (node.name in components) {
|
|
30
|
+
const component = components[node.name];
|
|
31
|
+
const props = node.attributes;
|
|
32
|
+
const children = node.children.map((child) => createAstroNode(child, components));
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
component,
|
|
36
|
+
props,
|
|
37
|
+
children,
|
|
38
|
+
};
|
|
39
|
+
} else if (isCapitalized(node.name)) {
|
|
40
|
+
throw new MarkdocError({
|
|
41
|
+
message: `Unable to render ${JSON.stringify(node.name)}.`,
|
|
42
|
+
hint: 'Did you add this to the "components" prop on your <Content /> component?',
|
|
43
|
+
});
|
|
44
|
+
} else {
|
|
45
|
+
return {
|
|
46
|
+
tag: node.name,
|
|
47
|
+
attributes: node.attributes,
|
|
48
|
+
children: node.children.map((child) => createAstroNode(child, components)),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import Markdoc from "@markdoc/markdoc";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import {
|
|
5
|
+
getAstroConfigPath,
|
|
6
|
+
MarkdocError,
|
|
7
|
+
parseFrontmatter,
|
|
8
|
+
prependForwardSlash
|
|
9
|
+
} from "./utils.js";
|
|
10
|
+
function markdoc(markdocConfig = {}) {
|
|
11
|
+
return {
|
|
12
|
+
name: "@astrojs/markdoc",
|
|
13
|
+
hooks: {
|
|
14
|
+
"astro:config:setup": async (params) => {
|
|
15
|
+
const { updateConfig, config, addContentEntryType } = params;
|
|
16
|
+
function getEntryInfo({ fileUrl, contents }) {
|
|
17
|
+
const parsed = parseFrontmatter(contents, fileURLToPath(fileUrl));
|
|
18
|
+
return {
|
|
19
|
+
data: parsed.data,
|
|
20
|
+
body: parsed.content,
|
|
21
|
+
slug: parsed.data.slug,
|
|
22
|
+
rawData: parsed.matter
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
addContentEntryType({
|
|
26
|
+
extensions: [".mdoc"],
|
|
27
|
+
getEntryInfo,
|
|
28
|
+
contentModuleTypes: await fs.promises.readFile(
|
|
29
|
+
new URL("../template/content-module-types.d.ts", import.meta.url),
|
|
30
|
+
"utf-8"
|
|
31
|
+
)
|
|
32
|
+
});
|
|
33
|
+
const viteConfig = {
|
|
34
|
+
plugins: [
|
|
35
|
+
{
|
|
36
|
+
name: "@astrojs/markdoc",
|
|
37
|
+
async transform(code, id) {
|
|
38
|
+
if (!id.endsWith(".mdoc"))
|
|
39
|
+
return;
|
|
40
|
+
validateRenderProperties(markdocConfig, config);
|
|
41
|
+
const body = getEntryInfo({
|
|
42
|
+
fileUrl: new URL(prependForwardSlash(id), "file://"),
|
|
43
|
+
contents: code
|
|
44
|
+
}).body;
|
|
45
|
+
const ast = Markdoc.parse(body);
|
|
46
|
+
const content = Markdoc.transform(ast, markdocConfig);
|
|
47
|
+
return `import { jsx as h } from 'astro/jsx-runtime';
|
|
48
|
+
import { Renderer } from '@astrojs/markdoc/components';
|
|
49
|
+
const transformedContent = ${JSON.stringify(
|
|
50
|
+
content
|
|
51
|
+
)};
|
|
52
|
+
export async function Content ({ components }) { return h(Renderer, { content: transformedContent, components }); }
|
|
53
|
+
Content[Symbol.for('astro.needsHeadRendering')] = true;`;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
]
|
|
57
|
+
};
|
|
58
|
+
updateConfig({ vite: viteConfig });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function validateRenderProperties(markdocConfig, astroConfig) {
|
|
64
|
+
const tags = markdocConfig.tags ?? {};
|
|
65
|
+
const nodes = markdocConfig.nodes ?? {};
|
|
66
|
+
for (const [name, config] of Object.entries(tags)) {
|
|
67
|
+
validateRenderProperty({ type: "tag", name, config, astroConfig });
|
|
68
|
+
}
|
|
69
|
+
for (const [name, config] of Object.entries(nodes)) {
|
|
70
|
+
validateRenderProperty({ type: "node", name, config, astroConfig });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function validateRenderProperty({
|
|
74
|
+
name,
|
|
75
|
+
config,
|
|
76
|
+
type,
|
|
77
|
+
astroConfig
|
|
78
|
+
}) {
|
|
79
|
+
if (typeof config.render === "string" && config.render.length === 0) {
|
|
80
|
+
throw new Error(
|
|
81
|
+
`Invalid ${type} configuration: ${JSON.stringify(
|
|
82
|
+
name
|
|
83
|
+
)}. The "render" property cannot be an empty string.`
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
if (typeof config.render === "string" && !isCapitalized(config.render)) {
|
|
87
|
+
const astroConfigPath = getAstroConfigPath(fs, fileURLToPath(astroConfig.root));
|
|
88
|
+
throw new MarkdocError({
|
|
89
|
+
message: `Invalid ${type} configuration: ${JSON.stringify(
|
|
90
|
+
name
|
|
91
|
+
)}. The "render" property must reference a capitalized component name.`,
|
|
92
|
+
hint: "If you want to render to an HTML element, see our docs on rendering Markdoc manually: https://docs.astro.build/en/guides/integrations-guide/markdoc/#render-markdoc-nodes--html-elements-as-astro-components",
|
|
93
|
+
location: astroConfigPath ? {
|
|
94
|
+
file: astroConfigPath
|
|
95
|
+
} : void 0
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function isCapitalized(str) {
|
|
100
|
+
return str.length > 0 && str[0] === str[0].toUpperCase();
|
|
101
|
+
}
|
|
102
|
+
export {
|
|
103
|
+
markdoc as default
|
|
104
|
+
};
|