@astrojs/markdoc 0.2.2 → 0.2.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/components/Renderer.astro +1 -2
- package/components/TreeNode.ts +12 -98
- package/dist/index.js +9 -49
- package/dist/nodes/heading.js +1 -1
- package/package.json +3 -3
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
---
|
|
2
|
-
//! astro-head-inject
|
|
3
2
|
import type { Config } from '@markdoc/markdoc';
|
|
4
3
|
import Markdoc from '@markdoc/markdoc';
|
|
5
4
|
import { ComponentNode, createTreeNode } from './TreeNode.js';
|
|
@@ -15,4 +14,4 @@ const ast = Markdoc.Ast.fromJSON(stringifiedAst);
|
|
|
15
14
|
const content = Markdoc.transform(ast, config);
|
|
16
15
|
---
|
|
17
16
|
|
|
18
|
-
<ComponentNode treeNode={
|
|
17
|
+
<ComponentNode treeNode={createTreeNode(content)} />
|
package/components/TreeNode.ts
CHANGED
|
@@ -2,16 +2,7 @@ import type { AstroInstance } from 'astro';
|
|
|
2
2
|
import { Fragment } from 'astro/jsx-runtime';
|
|
3
3
|
import type { RenderableTreeNode } from '@markdoc/markdoc';
|
|
4
4
|
import Markdoc from '@markdoc/markdoc';
|
|
5
|
-
import {
|
|
6
|
-
createComponent,
|
|
7
|
-
renderComponent,
|
|
8
|
-
render,
|
|
9
|
-
renderScriptElement,
|
|
10
|
-
renderUniqueStylesheet,
|
|
11
|
-
createHeadAndContent,
|
|
12
|
-
unescapeHTML,
|
|
13
|
-
renderTemplate,
|
|
14
|
-
} from 'astro/runtime/server/index.js';
|
|
5
|
+
import { createComponent, renderComponent, render } from 'astro/runtime/server/index.js';
|
|
15
6
|
|
|
16
7
|
export type TreeNode =
|
|
17
8
|
| {
|
|
@@ -21,9 +12,6 @@ export type TreeNode =
|
|
|
21
12
|
| {
|
|
22
13
|
type: 'component';
|
|
23
14
|
component: AstroInstance['default'];
|
|
24
|
-
collectedLinks?: string[];
|
|
25
|
-
collectedStyles?: string[];
|
|
26
|
-
collectedScripts?: string[];
|
|
27
15
|
props: Record<string, any>;
|
|
28
16
|
children: TreeNode[];
|
|
29
17
|
}
|
|
@@ -44,67 +32,20 @@ export const ComponentNode = createComponent({
|
|
|
44
32
|
)}`,
|
|
45
33
|
};
|
|
46
34
|
if (treeNode.type === 'component') {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
renderUniqueStylesheet({
|
|
54
|
-
type: 'inline',
|
|
55
|
-
content: style,
|
|
56
|
-
})
|
|
57
|
-
)
|
|
58
|
-
.join('');
|
|
59
|
-
}
|
|
60
|
-
if (Array.isArray(treeNode.collectedLinks)) {
|
|
61
|
-
links = treeNode.collectedLinks
|
|
62
|
-
.map((link: any) => {
|
|
63
|
-
return renderUniqueStylesheet(result, {
|
|
64
|
-
href: link[0] === '/' ? link : '/' + link,
|
|
65
|
-
});
|
|
66
|
-
})
|
|
67
|
-
.join('');
|
|
68
|
-
}
|
|
69
|
-
if (Array.isArray(treeNode.collectedScripts)) {
|
|
70
|
-
scripts = treeNode.collectedScripts
|
|
71
|
-
.map((script: any) => renderScriptElement(script))
|
|
72
|
-
.join('');
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const head = unescapeHTML(styles + links + scripts);
|
|
76
|
-
|
|
77
|
-
let headAndContent = createHeadAndContent(
|
|
78
|
-
head,
|
|
79
|
-
renderTemplate`${renderComponent(
|
|
80
|
-
result,
|
|
81
|
-
treeNode.component.name,
|
|
82
|
-
treeNode.component,
|
|
83
|
-
treeNode.props,
|
|
84
|
-
slots
|
|
85
|
-
)}`
|
|
35
|
+
return renderComponent(
|
|
36
|
+
result,
|
|
37
|
+
treeNode.component.name,
|
|
38
|
+
treeNode.component,
|
|
39
|
+
treeNode.props,
|
|
40
|
+
slots
|
|
86
41
|
);
|
|
87
|
-
|
|
88
|
-
// Let the runtime know that this component is being used.
|
|
89
|
-
result.propagators.set(
|
|
90
|
-
{},
|
|
91
|
-
{
|
|
92
|
-
init() {
|
|
93
|
-
return headAndContent;
|
|
94
|
-
},
|
|
95
|
-
}
|
|
96
|
-
);
|
|
97
|
-
|
|
98
|
-
return headAndContent;
|
|
99
42
|
}
|
|
100
43
|
return renderComponent(result, treeNode.tag, treeNode.tag, treeNode.attributes, slots);
|
|
101
44
|
},
|
|
102
|
-
propagation: '
|
|
45
|
+
propagation: 'none',
|
|
103
46
|
});
|
|
104
47
|
|
|
105
|
-
export
|
|
106
|
-
node: RenderableTreeNode | RenderableTreeNode[]
|
|
107
|
-
): Promise<TreeNode> {
|
|
48
|
+
export function createTreeNode(node: RenderableTreeNode | RenderableTreeNode[]): TreeNode {
|
|
108
49
|
if (typeof node === 'string' || typeof node === 'number') {
|
|
109
50
|
return { type: 'text', content: String(node) };
|
|
110
51
|
} else if (Array.isArray(node)) {
|
|
@@ -112,17 +53,16 @@ export async function createTreeNode(
|
|
|
112
53
|
type: 'component',
|
|
113
54
|
component: Fragment,
|
|
114
55
|
props: {},
|
|
115
|
-
children:
|
|
56
|
+
children: node.map((child) => createTreeNode(child)),
|
|
116
57
|
};
|
|
117
58
|
} else if (node === null || typeof node !== 'object' || !Markdoc.Tag.isTag(node)) {
|
|
118
59
|
return { type: 'text', content: '' };
|
|
119
60
|
}
|
|
120
61
|
|
|
121
|
-
const children = await Promise.all(node.children.map((child) => createTreeNode(child)));
|
|
122
|
-
|
|
123
62
|
if (typeof node.name === 'function') {
|
|
124
63
|
const component = node.name;
|
|
125
64
|
const props = node.attributes;
|
|
65
|
+
const children = node.children.map((child) => createTreeNode(child));
|
|
126
66
|
|
|
127
67
|
return {
|
|
128
68
|
type: 'component',
|
|
@@ -130,38 +70,12 @@ export async function createTreeNode(
|
|
|
130
70
|
props,
|
|
131
71
|
children,
|
|
132
72
|
};
|
|
133
|
-
} else if (isPropagatedAssetsModule(node.name)) {
|
|
134
|
-
const { collectedStyles, collectedLinks, collectedScripts } = node.name;
|
|
135
|
-
const component = (await node.name.getMod())?.default ?? Fragment;
|
|
136
|
-
const props = node.attributes;
|
|
137
|
-
|
|
138
|
-
return {
|
|
139
|
-
type: 'component',
|
|
140
|
-
component,
|
|
141
|
-
collectedStyles,
|
|
142
|
-
collectedLinks,
|
|
143
|
-
collectedScripts,
|
|
144
|
-
props,
|
|
145
|
-
children,
|
|
146
|
-
};
|
|
147
73
|
} else {
|
|
148
74
|
return {
|
|
149
75
|
type: 'element',
|
|
150
76
|
tag: node.name,
|
|
151
77
|
attributes: node.attributes,
|
|
152
|
-
children,
|
|
78
|
+
children: node.children.map((child) => createTreeNode(child)),
|
|
153
79
|
};
|
|
154
80
|
}
|
|
155
81
|
}
|
|
156
|
-
|
|
157
|
-
type PropagatedAssetsModule = {
|
|
158
|
-
__astroPropagation: true;
|
|
159
|
-
getMod: () => Promise<AstroInstance['default']>;
|
|
160
|
-
collectedStyles: string[];
|
|
161
|
-
collectedLinks: string[];
|
|
162
|
-
collectedScripts: string[];
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
function isPropagatedAssetsModule(module: any): module is PropagatedAssetsModule {
|
|
166
|
-
return typeof module === 'object' && module != null && '__astroPropagation' in module;
|
|
167
|
-
}
|
package/dist/index.js
CHANGED
|
@@ -20,11 +20,7 @@ function markdocIntegration(legacyConfig) {
|
|
|
20
20
|
name: "@astrojs/markdoc",
|
|
21
21
|
hooks: {
|
|
22
22
|
"astro:config:setup": async (params) => {
|
|
23
|
-
const {
|
|
24
|
-
config: astroConfig,
|
|
25
|
-
updateConfig,
|
|
26
|
-
addContentEntryType
|
|
27
|
-
} = params;
|
|
23
|
+
const { config: astroConfig, addContentEntryType } = params;
|
|
28
24
|
markdocConfigResult = await loadMarkdocConfig(astroConfig);
|
|
29
25
|
const userMarkdocConfig = (markdocConfigResult == null ? void 0 : markdocConfigResult.config) ?? {};
|
|
30
26
|
function getEntryInfo({ fileUrl, contents }) {
|
|
@@ -39,9 +35,6 @@ function markdocIntegration(legacyConfig) {
|
|
|
39
35
|
addContentEntryType({
|
|
40
36
|
extensions: [".mdoc"],
|
|
41
37
|
getEntryInfo,
|
|
42
|
-
// Markdoc handles script / style propagation
|
|
43
|
-
// for Astro components internally
|
|
44
|
-
handlePropagation: false,
|
|
45
38
|
async getRenderModule({ entry, viteId }) {
|
|
46
39
|
const ast = Markdoc.parse(entry.body);
|
|
47
40
|
const pluginContext = this;
|
|
@@ -76,10 +69,7 @@ function markdocIntegration(legacyConfig) {
|
|
|
76
69
|
filePath: entry._internal.filePath
|
|
77
70
|
});
|
|
78
71
|
}
|
|
79
|
-
const res = `import {
|
|
80
|
-
createComponent,
|
|
81
|
-
renderComponent,
|
|
82
|
-
} from 'astro/runtime/server/index.js';
|
|
72
|
+
const res = `import { jsx as h } from 'astro/jsx-runtime';
|
|
83
73
|
import { Renderer } from '@astrojs/markdoc/components';
|
|
84
74
|
import { collectHeadings, setupConfig, Markdoc } from '@astrojs/markdoc/runtime';
|
|
85
75
|
import * as entry from ${JSON.stringify(viteId + "?astroContentCollectionEntry")};
|
|
@@ -104,24 +94,14 @@ export function getHeadings() {
|
|
|
104
94
|
const content = Markdoc.transform(ast, config);
|
|
105
95
|
return collectHeadings(Array.isArray(content) ? content : content.children);
|
|
106
96
|
}
|
|
97
|
+
export async function Content (props) {
|
|
98
|
+
const config = setupConfig({
|
|
99
|
+
...userConfig,
|
|
100
|
+
variables: { ...userConfig.variables, ...props },
|
|
101
|
+
}, entry);
|
|
107
102
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
const config = setupConfig({
|
|
111
|
-
...userConfig,
|
|
112
|
-
variables: { ...userConfig.variables, ...props },
|
|
113
|
-
}, entry);
|
|
114
|
-
|
|
115
|
-
return renderComponent(
|
|
116
|
-
result,
|
|
117
|
-
Renderer.name,
|
|
118
|
-
Renderer,
|
|
119
|
-
{ stringifiedAst, config },
|
|
120
|
-
{}
|
|
121
|
-
);
|
|
122
|
-
},
|
|
123
|
-
propagation: 'self',
|
|
124
|
-
});`;
|
|
103
|
+
return h(Renderer, { config, stringifiedAst });
|
|
104
|
+
}`;
|
|
125
105
|
return { code: res };
|
|
126
106
|
},
|
|
127
107
|
contentModuleTypes: await fs.promises.readFile(
|
|
@@ -129,26 +109,6 @@ export const Content = createComponent({
|
|
|
129
109
|
"utf-8"
|
|
130
110
|
)
|
|
131
111
|
});
|
|
132
|
-
updateConfig({
|
|
133
|
-
vite: {
|
|
134
|
-
plugins: [
|
|
135
|
-
{
|
|
136
|
-
name: "@astrojs/markdoc:astro-propagated-assets",
|
|
137
|
-
enforce: "pre",
|
|
138
|
-
// Astro component styles and scripts should only be injected
|
|
139
|
-
// When a given Markdoc file actually uses that component.
|
|
140
|
-
// Add the `astroPropagatedAssets` flag to inject only when rendered.
|
|
141
|
-
resolveId(id, importer) {
|
|
142
|
-
if (importer === (markdocConfigResult == null ? void 0 : markdocConfigResult.fileUrl.pathname) && id.endsWith(".astro")) {
|
|
143
|
-
return this.resolve(id + "?astroPropagatedAssets", importer, {
|
|
144
|
-
skipSelf: true
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
]
|
|
150
|
-
}
|
|
151
|
-
});
|
|
152
112
|
},
|
|
153
113
|
"astro:server:setup": async ({ server }) => {
|
|
154
114
|
server.watcher.on("all", (event, entry) => {
|
package/dist/nodes/heading.js
CHANGED
|
@@ -27,7 +27,7 @@ const heading = {
|
|
|
27
27
|
// For components, pass down `level` as a prop,
|
|
28
28
|
// alongside `__collectHeading` for our `headings` collector.
|
|
29
29
|
// Avoid accidentally rendering `level` as an HTML attribute otherwise!
|
|
30
|
-
typeof render === "
|
|
30
|
+
typeof render === "function" ? { ...attributes, id: slug, __collectHeading: true, level } : { ...attributes, id: slug }
|
|
31
31
|
);
|
|
32
32
|
return new Markdoc.Tag(render, tagProps, children);
|
|
33
33
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astrojs/markdoc",
|
|
3
3
|
"description": "Add support for Markdoc pages in your Astro site",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.3",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"author": "withastro",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"zod": "^3.17.3"
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
|
43
|
-
"astro": "^2.5.
|
|
43
|
+
"astro": "^2.5.5"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@astrojs/markdown-remark": "^2.2.1",
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"mocha": "^9.2.2",
|
|
54
54
|
"rollup": "^3.20.1",
|
|
55
55
|
"vite": "^4.3.1",
|
|
56
|
-
"astro": "2.5.
|
|
56
|
+
"astro": "2.5.5",
|
|
57
57
|
"astro-scripts": "0.0.14"
|
|
58
58
|
},
|
|
59
59
|
"engines": {
|