@astrojs/markdoc 0.0.0-markdoc-20230214163516
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 +620 -0
- package/LICENSE +61 -0
- package/README.md +160 -0
- package/components/RenderNode.astro +30 -0
- package/components/Renderer.astro +14 -0
- package/components/astroNode.ts +64 -0
- package/components/index.ts +2 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +57 -0
- package/dist/utils.d.ts +6 -0
- package/dist/utils.js +20 -0
- package/package.json +54 -0
- package/src/index.ts +50 -0
- package/src/utils.ts +25 -0
- package/template/content-module-types.d.ts +20 -0
- package/test/content-collections.test.js +181 -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/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 +14 -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 +50 -0
- package/test/fixtures/content-collections/src/pages/content-with-config.astro +38 -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
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,160 @@
|
|
|
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 [UI components][astro-ui-frameworks]. 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
|
+
## Usage
|
|
58
|
+
|
|
59
|
+
Markdoc files can be used within content collections. Add entries to any content collection using the `.mdoc` extension:
|
|
60
|
+
|
|
61
|
+
```sh
|
|
62
|
+
src/content/docs/
|
|
63
|
+
why-markdoc.mdoc
|
|
64
|
+
quick-start.mdoc
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Then, query for these files using the [Content Collection APIs](https://docs.astro.build/en/guides/content-collections/#querying-collections):
|
|
68
|
+
|
|
69
|
+
```astro
|
|
70
|
+
---
|
|
71
|
+
import { getEntryBySlug } from 'astro:content';
|
|
72
|
+
|
|
73
|
+
const entry = await getEntryBySlug('docs', 'why-markdoc');
|
|
74
|
+
const { Content } = await entry.render();
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
<!--Access frontmatter properties with `data`-->
|
|
78
|
+
<h1>{entry.data.title}</h1>
|
|
79
|
+
<!--Render Markdoc contents with the Content component-->
|
|
80
|
+
<Content />
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
📚 See the [Astro Content Collection docs][astro-content-collections] for more information.
|
|
84
|
+
|
|
85
|
+
## Configuration
|
|
86
|
+
|
|
87
|
+
You can configure how your Markdoc content is rendered using props via the `Content` component. This component is returned by [a content collection `render()` result](https://docs.astro.build/en/guides/content-collections/#rendering-content-to-html).
|
|
88
|
+
|
|
89
|
+
### `config` prop
|
|
90
|
+
|
|
91
|
+
The `config` prop accepts all [Markdoc configuration options](https://markdoc.dev/docs/config#full-example), including tags and variables.
|
|
92
|
+
|
|
93
|
+
This example defines a `version` variable to use within a `why-markdoc.mdoc` entry:
|
|
94
|
+
|
|
95
|
+
```astro
|
|
96
|
+
---
|
|
97
|
+
import { getEntryBySlug } from 'astro:content';
|
|
98
|
+
|
|
99
|
+
const entry = await getEntryBySlug('docs', 'why-markdoc');
|
|
100
|
+
const { Content } = await entry.render();
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
<Content
|
|
104
|
+
config={{
|
|
105
|
+
variables: {
|
|
106
|
+
version: '0.0.1',
|
|
107
|
+
}
|
|
108
|
+
}}
|
|
109
|
+
/>
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### `components` prop
|
|
113
|
+
|
|
114
|
+
The `components` prop defines mappings from an HTML element name to an Astro or UI framework component (React, Vue, Svelte, etc).
|
|
115
|
+
|
|
116
|
+
:::note
|
|
117
|
+
`components` 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.
|
|
118
|
+
:::
|
|
119
|
+
|
|
120
|
+
This example renders all `h1` headings using a `Title` component:
|
|
121
|
+
|
|
122
|
+
```astro
|
|
123
|
+
---
|
|
124
|
+
import { getEntryBySlug } from 'astro:content';
|
|
125
|
+
import Title from '../components/Title.astro';
|
|
126
|
+
|
|
127
|
+
const entry = await getEntryBySlug('docs', 'why-markdoc');
|
|
128
|
+
const { Content } = await entry.render();
|
|
129
|
+
---
|
|
130
|
+
|
|
131
|
+
<Content
|
|
132
|
+
components={{
|
|
133
|
+
h1: Title,
|
|
134
|
+
}}
|
|
135
|
+
/>
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## Examples
|
|
139
|
+
|
|
140
|
+
* The [Astro Markdoc starter template](https://github.com/withastro/astro/tree/latest/examples/with-mdx) shows how to use Markdoc files in your Astro project.
|
|
141
|
+
|
|
142
|
+
## Troubleshooting
|
|
143
|
+
|
|
144
|
+
For help, check out the `#support` channel on [Discord](https://astro.build/chat). Our friendly Support Squad members are here to help!
|
|
145
|
+
|
|
146
|
+
You can also check our [Astro Integration Documentation][astro-integration] for more on integrations.
|
|
147
|
+
|
|
148
|
+
## Contributing
|
|
149
|
+
|
|
150
|
+
This package is maintained by Astro's Core team. You're welcome to submit an issue or PR!
|
|
151
|
+
|
|
152
|
+
## Changelog
|
|
153
|
+
|
|
154
|
+
See [CHANGELOG.md](https://github.com/withastro/astro/tree/main/packages/integrations/markdoc/CHANGELOG.md) for a history of changes to this integration.
|
|
155
|
+
|
|
156
|
+
[astro-integration]: https://docs.astro.build/en/guides/integrations-guide/
|
|
157
|
+
|
|
158
|
+
[astro-ui-frameworks]: https://docs.astro.build/en/core-concepts/framework-components/#using-framework-components
|
|
159
|
+
|
|
160
|
+
[astro-content-collections]: https://docs.astro.build/en/guides/content-collections/
|
|
@@ -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,14 @@
|
|
|
1
|
+
---
|
|
2
|
+
import type { RenderableTreeNode } from '@markdoc/markdoc';
|
|
3
|
+
import { ComponentRenderer, createAstroNode } from './astroNode';
|
|
4
|
+
import RenderNode from './RenderNode.astro';
|
|
5
|
+
|
|
6
|
+
type Props = {
|
|
7
|
+
content: RenderableTreeNode;
|
|
8
|
+
components: Record<string, ComponentRenderer>;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const { content, components } = Astro.props as Props;
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
<RenderNode node={createAstroNode(content, components)} />
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { ComponentInstance } from 'astro';
|
|
2
|
+
import type { RenderableTreeNode, Tag } from '@markdoc/markdoc';
|
|
3
|
+
import Markdoc from '@markdoc/markdoc';
|
|
4
|
+
import { escape } from 'html-escaper';
|
|
5
|
+
|
|
6
|
+
export type ComponentRenderer =
|
|
7
|
+
| ComponentInstance['default']
|
|
8
|
+
| {
|
|
9
|
+
component: ComponentInstance['default'];
|
|
10
|
+
props?(params: { attributes: Record<string, any>; getTreeNode(): Tag }): Record<string, any>;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type AstroNode =
|
|
14
|
+
| string
|
|
15
|
+
| {
|
|
16
|
+
component: ComponentInstance['default'];
|
|
17
|
+
props: Record<string, any>;
|
|
18
|
+
children: AstroNode[];
|
|
19
|
+
}
|
|
20
|
+
| {
|
|
21
|
+
tag: string;
|
|
22
|
+
attributes: Record<string, any>;
|
|
23
|
+
children: AstroNode[];
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export function createAstroNode(
|
|
27
|
+
node: RenderableTreeNode,
|
|
28
|
+
components: Record<string, ComponentRenderer> = {}
|
|
29
|
+
): AstroNode {
|
|
30
|
+
if (typeof node === 'string' || typeof node === 'number') {
|
|
31
|
+
return escape(String(node));
|
|
32
|
+
} else if (node === null || typeof node !== 'object' || !Markdoc.Tag.isTag(node)) {
|
|
33
|
+
return '';
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (node.name in components) {
|
|
37
|
+
const componentRenderer = components[node.name];
|
|
38
|
+
const component =
|
|
39
|
+
'component' in componentRenderer ? componentRenderer.component : componentRenderer;
|
|
40
|
+
const props =
|
|
41
|
+
'props' in componentRenderer && typeof componentRenderer.props === 'function'
|
|
42
|
+
? componentRenderer.props({
|
|
43
|
+
attributes: node.attributes,
|
|
44
|
+
getTreeNode() {
|
|
45
|
+
return node;
|
|
46
|
+
},
|
|
47
|
+
})
|
|
48
|
+
: node.attributes;
|
|
49
|
+
|
|
50
|
+
const children = node.children.map((child) => createAstroNode(child, components));
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
component,
|
|
54
|
+
props,
|
|
55
|
+
children,
|
|
56
|
+
};
|
|
57
|
+
} else {
|
|
58
|
+
return {
|
|
59
|
+
tag: node.name,
|
|
60
|
+
attributes: node.attributes,
|
|
61
|
+
children: node.children.map((child) => createAstroNode(child, components)),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import _Markdoc from "@markdoc/markdoc";
|
|
2
|
+
import { parseFrontmatter } from "./utils.js";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import fs from "node:fs";
|
|
5
|
+
function markdoc() {
|
|
6
|
+
return {
|
|
7
|
+
name: "@astrojs/markdoc",
|
|
8
|
+
hooks: {
|
|
9
|
+
"astro:config:setup": async ({ updateConfig, config, addContentEntryType, command }) => {
|
|
10
|
+
const contentEntryType = {
|
|
11
|
+
extensions: [".mdoc"],
|
|
12
|
+
async getEntryInfo({ fileUrl, contents }) {
|
|
13
|
+
const parsed = parseFrontmatter(contents, fileURLToPath(fileUrl));
|
|
14
|
+
return {
|
|
15
|
+
data: parsed.data,
|
|
16
|
+
body: parsed.content,
|
|
17
|
+
slug: parsed.data.slug,
|
|
18
|
+
rawData: parsed.matter
|
|
19
|
+
};
|
|
20
|
+
},
|
|
21
|
+
contentModuleTypes: await fs.promises.readFile(
|
|
22
|
+
new URL("../template/content-module-types.d.ts", import.meta.url),
|
|
23
|
+
"utf-8"
|
|
24
|
+
)
|
|
25
|
+
};
|
|
26
|
+
addContentEntryType(contentEntryType);
|
|
27
|
+
const viteConfig = {
|
|
28
|
+
plugins: [
|
|
29
|
+
{
|
|
30
|
+
name: "@astrojs/markdoc",
|
|
31
|
+
async transform(code, id) {
|
|
32
|
+
if (!id.endsWith(".mdoc"))
|
|
33
|
+
return;
|
|
34
|
+
return `import { jsx as h } from 'astro/jsx-runtime';
|
|
35
|
+
import { Markdoc } from '@astrojs/markdoc';
|
|
36
|
+
import { Renderer } from '@astrojs/markdoc/components';
|
|
37
|
+
export const body = ${JSON.stringify(
|
|
38
|
+
code
|
|
39
|
+
)};
|
|
40
|
+
export function getParsed() { return Markdoc.parse(body); }
|
|
41
|
+
export function getTransformed(inlineConfig) { return Markdoc.transform(getParsed(), inlineConfig) }
|
|
42
|
+
export async function Content ({ config, components }) { return h(Renderer, { content: getTransformed(config), components }); }
|
|
43
|
+
Content[Symbol.for('astro.needsHeadRendering')] = true;`;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
]
|
|
47
|
+
};
|
|
48
|
+
updateConfig({ vite: viteConfig });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
const Markdoc = _Markdoc;
|
|
54
|
+
export {
|
|
55
|
+
Markdoc,
|
|
56
|
+
markdoc as default
|
|
57
|
+
};
|
package/dist/utils.d.ts
ADDED
package/dist/utils.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import matter from "gray-matter";
|
|
2
|
+
function parseFrontmatter(fileContents, filePath) {
|
|
3
|
+
try {
|
|
4
|
+
matter.clearCache();
|
|
5
|
+
return matter(fileContents);
|
|
6
|
+
} catch (e) {
|
|
7
|
+
if (e.name === "YAMLException") {
|
|
8
|
+
const err = e;
|
|
9
|
+
err.id = filePath;
|
|
10
|
+
err.loc = { file: e.id, line: e.mark.line + 1, column: e.mark.column };
|
|
11
|
+
err.message = e.reason;
|
|
12
|
+
throw err;
|
|
13
|
+
} else {
|
|
14
|
+
throw e;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export {
|
|
19
|
+
parseFrontmatter
|
|
20
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@astrojs/markdoc",
|
|
3
|
+
"description": "Use Markdoc within Astro",
|
|
4
|
+
"version": "0.0.0-markdoc-20230214163516",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"author": "withastro",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/withastro/astro.git",
|
|
12
|
+
"directory": "packages/integrations/markdoc"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"astro-integration",
|
|
16
|
+
"astro-component",
|
|
17
|
+
"markdoc"
|
|
18
|
+
],
|
|
19
|
+
"bugs": "https://github.com/withastro/astro/issues",
|
|
20
|
+
"homepage": "https://docs.astro.build/en/guides/integrations-guide/markdoc/",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": "./dist/index.js",
|
|
23
|
+
"./components": "./components/index.ts",
|
|
24
|
+
"./package.json": "./package.json"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@markdoc/markdoc": "^0.2.2",
|
|
28
|
+
"gray-matter": "^4.0.3",
|
|
29
|
+
"html-escaper": "^3.0.3",
|
|
30
|
+
"stringify-attributes": "^3.0.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/chai": "^4.3.1",
|
|
34
|
+
"@types/html-escaper": "^3.0.0",
|
|
35
|
+
"@types/mocha": "^9.1.1",
|
|
36
|
+
"astro": "0.0.0-markdoc-20230214163516",
|
|
37
|
+
"astro-scripts": "0.0.10",
|
|
38
|
+
"chai": "^4.3.6",
|
|
39
|
+
"devalue": "^4.2.0",
|
|
40
|
+
"linkedom": "^0.14.12",
|
|
41
|
+
"mocha": "^9.2.2",
|
|
42
|
+
"vite": "^4.0.3"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=16.12.0"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
|
|
49
|
+
"build:ci": "astro-scripts build \"src/**/*.ts\"",
|
|
50
|
+
"dev": "astro-scripts dev \"src/**/*.ts\"",
|
|
51
|
+
"test": "mocha --exit --timeout 20000",
|
|
52
|
+
"test:match": "mocha --timeout 20000 -g"
|
|
53
|
+
}
|
|
54
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { AstroIntegration } from 'astro';
|
|
2
|
+
import type { InlineConfig } from 'vite';
|
|
3
|
+
import _Markdoc from '@markdoc/markdoc';
|
|
4
|
+
import { parseFrontmatter } from './utils.js';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
|
|
8
|
+
export default function markdoc(): AstroIntegration {
|
|
9
|
+
return {
|
|
10
|
+
name: '@astrojs/markdoc',
|
|
11
|
+
hooks: {
|
|
12
|
+
'astro:config:setup': async ({ updateConfig, config, addContentEntryType, command }: any) => {
|
|
13
|
+
const contentEntryType = {
|
|
14
|
+
extensions: ['.mdoc'],
|
|
15
|
+
async getEntryInfo({ fileUrl, contents }: { fileUrl: URL; contents: string }) {
|
|
16
|
+
const parsed = parseFrontmatter(contents, fileURLToPath(fileUrl));
|
|
17
|
+
return {
|
|
18
|
+
data: parsed.data,
|
|
19
|
+
body: parsed.content,
|
|
20
|
+
slug: parsed.data.slug,
|
|
21
|
+
rawData: parsed.matter,
|
|
22
|
+
};
|
|
23
|
+
},
|
|
24
|
+
contentModuleTypes: await fs.promises.readFile(
|
|
25
|
+
new URL('../template/content-module-types.d.ts', import.meta.url),
|
|
26
|
+
'utf-8'
|
|
27
|
+
),
|
|
28
|
+
};
|
|
29
|
+
addContentEntryType(contentEntryType);
|
|
30
|
+
|
|
31
|
+
const viteConfig: InlineConfig = {
|
|
32
|
+
plugins: [
|
|
33
|
+
{
|
|
34
|
+
name: '@astrojs/markdoc',
|
|
35
|
+
async transform(code, id) {
|
|
36
|
+
if (!id.endsWith('.mdoc')) return;
|
|
37
|
+
return `import { jsx as h } from 'astro/jsx-runtime';\nimport { Markdoc } from '@astrojs/markdoc';\nimport { Renderer } from '@astrojs/markdoc/components';\nexport const body = ${JSON.stringify(
|
|
38
|
+
code
|
|
39
|
+
)};\nexport function getParsed() { return Markdoc.parse(body); }\nexport function getTransformed(inlineConfig) { return Markdoc.transform(getParsed(), inlineConfig) }\nexport async function Content ({ config, components }) { return h(Renderer, { content: getTransformed(config), components }); }\nContent[Symbol.for('astro.needsHeadRendering')] = true;`;
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
};
|
|
44
|
+
updateConfig({ vite: viteConfig });
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export const Markdoc = _Markdoc;
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import matter from 'gray-matter';
|
|
2
|
+
import type { ErrorPayload as ViteErrorPayload } from 'vite';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Match YAML exception handling from Astro core errors
|
|
6
|
+
* @see 'astro/src/core/errors.ts'
|
|
7
|
+
*/
|
|
8
|
+
export function parseFrontmatter(fileContents: string, filePath: string) {
|
|
9
|
+
try {
|
|
10
|
+
// `matter` is empty string on cache results
|
|
11
|
+
// clear cache to prevent this
|
|
12
|
+
(matter as any).clearCache();
|
|
13
|
+
return matter(fileContents);
|
|
14
|
+
} catch (e: any) {
|
|
15
|
+
if (e.name === 'YAMLException') {
|
|
16
|
+
const err: Error & ViteErrorPayload['err'] = e;
|
|
17
|
+
err.id = filePath;
|
|
18
|
+
err.loc = { file: e.id, line: e.mark.line + 1, column: e.mark.column };
|
|
19
|
+
err.message = e.reason;
|
|
20
|
+
throw err;
|
|
21
|
+
} else {
|
|
22
|
+
throw e;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
declare module 'astro:content' {
|
|
2
|
+
type ComponentRenderer =
|
|
3
|
+
| import('astro').ComponentInstance['default']
|
|
4
|
+
| {
|
|
5
|
+
component: import('astro').ComponentInstance['default'];
|
|
6
|
+
props?(params: {
|
|
7
|
+
attributes: Record<string, any>;
|
|
8
|
+
getTreeNode(): import('@astrojs/markdoc').Markdoc.Tag;
|
|
9
|
+
}): Record<string, any>;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
interface Render {
|
|
13
|
+
'.mdoc': Promise<{
|
|
14
|
+
Content(props: {
|
|
15
|
+
config?: import('@astrojs/markdoc').Markdoc.Config;
|
|
16
|
+
components?: Record<string, ComponentRenderer>;
|
|
17
|
+
}): import('astro').MarkdownInstance<{}>['Content'];
|
|
18
|
+
}>;
|
|
19
|
+
}
|
|
20
|
+
}
|