@astrojs/markdoc 0.3.0 → 0.3.2
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/README.md +3 -5
- package/components/Renderer.astro +8 -1
- package/components/TreeNode.ts +91 -18
- package/dist/config.d.ts +34 -29
- package/dist/heading-ids.d.ts +6 -5
- package/dist/heading-ids.js +1 -1
- package/dist/index.js +96 -22
- package/dist/load-config.d.ts +2 -2
- package/dist/runtime.d.ts +1 -2
- package/dist/runtime.js +2 -5
- package/dist/utils.d.ts +14 -0
- package/dist/utils.js +20 -0
- package/package.json +28 -6
package/README.md
CHANGED
|
@@ -151,9 +151,8 @@ import Heading from './src/components/Heading.astro';
|
|
|
151
151
|
export default defineMarkdocConfig({
|
|
152
152
|
nodes: {
|
|
153
153
|
heading: {
|
|
154
|
+
...nodes.heading, // Preserve default anchor link generation
|
|
154
155
|
render: Heading,
|
|
155
|
-
// Preserve default anchor link generation
|
|
156
|
-
...nodes.heading,
|
|
157
156
|
},
|
|
158
157
|
},
|
|
159
158
|
})
|
|
@@ -225,8 +224,8 @@ import { defineMarkdocConfig, nodes } from '@astrojs/markdoc/config';
|
|
|
225
224
|
export default defineMarkdocConfig({
|
|
226
225
|
nodes: {
|
|
227
226
|
document: {
|
|
228
|
-
render: null, // default 'article'
|
|
229
227
|
...nodes.document, // Apply defaults for other options
|
|
228
|
+
render: null, // default 'article'
|
|
230
229
|
},
|
|
231
230
|
},
|
|
232
231
|
})
|
|
@@ -246,9 +245,8 @@ import Quote from './src/components/Quote.astro';
|
|
|
246
245
|
export default defineMarkdocConfig({
|
|
247
246
|
nodes: {
|
|
248
247
|
blockquote: {
|
|
248
|
+
...nodes.blockquote, // Apply Markdoc's defaults for other options
|
|
249
249
|
render: Quote,
|
|
250
|
-
// Apply Markdoc's defaults for other options
|
|
251
|
-
...nodes.blockquote,
|
|
252
250
|
},
|
|
253
251
|
},
|
|
254
252
|
})
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
---
|
|
2
|
+
//! astro-head-inject
|
|
2
3
|
import type { Config } from '@markdoc/markdoc';
|
|
3
4
|
import Markdoc from '@markdoc/markdoc';
|
|
4
5
|
import { ComponentNode, createTreeNode } from './TreeNode.js';
|
|
@@ -14,4 +15,10 @@ const ast = Markdoc.Ast.fromJSON(stringifiedAst);
|
|
|
14
15
|
const content = Markdoc.transform(ast, config);
|
|
15
16
|
---
|
|
16
17
|
|
|
17
|
-
|
|
18
|
+
{
|
|
19
|
+
Array.isArray(content) ? (
|
|
20
|
+
content.map(async (c) => <ComponentNode treeNode={await createTreeNode(c)} />)
|
|
21
|
+
) : (
|
|
22
|
+
<ComponentNode treeNode={await createTreeNode(content)} />
|
|
23
|
+
)
|
|
24
|
+
}
|
package/components/TreeNode.ts
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import type { AstroInstance } from 'astro';
|
|
2
|
-
import { Fragment } from 'astro/jsx-runtime';
|
|
3
2
|
import type { RenderableTreeNode } from '@markdoc/markdoc';
|
|
4
3
|
import Markdoc from '@markdoc/markdoc';
|
|
5
4
|
import {
|
|
6
5
|
createComponent,
|
|
7
6
|
renderComponent,
|
|
8
7
|
render,
|
|
8
|
+
renderScriptElement,
|
|
9
|
+
renderUniqueStylesheet,
|
|
10
|
+
createHeadAndContent,
|
|
11
|
+
unescapeHTML,
|
|
12
|
+
renderTemplate,
|
|
9
13
|
HTMLString,
|
|
10
14
|
isHTMLString,
|
|
11
15
|
} from 'astro/runtime/server/index.js';
|
|
@@ -18,6 +22,9 @@ export type TreeNode =
|
|
|
18
22
|
| {
|
|
19
23
|
type: 'component';
|
|
20
24
|
component: AstroInstance['default'];
|
|
25
|
+
collectedLinks?: string[];
|
|
26
|
+
collectedStyles?: string[];
|
|
27
|
+
collectedScripts?: string[];
|
|
21
28
|
props: Record<string, any>;
|
|
22
29
|
children: TreeNode[];
|
|
23
30
|
}
|
|
@@ -39,39 +46,79 @@ export const ComponentNode = createComponent({
|
|
|
39
46
|
)}`,
|
|
40
47
|
};
|
|
41
48
|
if (treeNode.type === 'component') {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
treeNode.
|
|
47
|
-
|
|
49
|
+
let styles = '',
|
|
50
|
+
links = '',
|
|
51
|
+
scripts = '';
|
|
52
|
+
if (Array.isArray(treeNode.collectedStyles)) {
|
|
53
|
+
styles = treeNode.collectedStyles
|
|
54
|
+
.map((style: any) =>
|
|
55
|
+
renderUniqueStylesheet(result, {
|
|
56
|
+
type: 'inline',
|
|
57
|
+
content: style,
|
|
58
|
+
})
|
|
59
|
+
)
|
|
60
|
+
.join('');
|
|
61
|
+
}
|
|
62
|
+
if (Array.isArray(treeNode.collectedLinks)) {
|
|
63
|
+
links = treeNode.collectedLinks
|
|
64
|
+
.map((link: any) => {
|
|
65
|
+
return renderUniqueStylesheet(result, {
|
|
66
|
+
type: 'external',
|
|
67
|
+
src: link[0] === '/' ? link : '/' + link,
|
|
68
|
+
});
|
|
69
|
+
})
|
|
70
|
+
.join('');
|
|
71
|
+
}
|
|
72
|
+
if (Array.isArray(treeNode.collectedScripts)) {
|
|
73
|
+
scripts = treeNode.collectedScripts
|
|
74
|
+
.map((script: any) => renderScriptElement(script))
|
|
75
|
+
.join('');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const head = unescapeHTML(styles + links + scripts);
|
|
79
|
+
|
|
80
|
+
let headAndContent = createHeadAndContent(
|
|
81
|
+
head,
|
|
82
|
+
renderTemplate`${renderComponent(
|
|
83
|
+
result,
|
|
84
|
+
treeNode.component.name,
|
|
85
|
+
treeNode.component,
|
|
86
|
+
treeNode.props,
|
|
87
|
+
slots
|
|
88
|
+
)}`
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
// Let the runtime know that this component is being used.
|
|
92
|
+
result.propagators.set(
|
|
93
|
+
{},
|
|
94
|
+
{
|
|
95
|
+
init() {
|
|
96
|
+
return headAndContent;
|
|
97
|
+
},
|
|
98
|
+
}
|
|
48
99
|
);
|
|
100
|
+
|
|
101
|
+
return headAndContent;
|
|
49
102
|
}
|
|
50
103
|
return renderComponent(result, treeNode.tag, treeNode.tag, treeNode.attributes, slots);
|
|
51
104
|
},
|
|
52
|
-
propagation: '
|
|
105
|
+
propagation: 'self',
|
|
53
106
|
});
|
|
54
107
|
|
|
55
|
-
export function createTreeNode(node: RenderableTreeNode
|
|
108
|
+
export async function createTreeNode(node: RenderableTreeNode): Promise<TreeNode> {
|
|
56
109
|
if (isHTMLString(node)) {
|
|
57
110
|
return { type: 'text', content: node as HTMLString };
|
|
58
111
|
} else if (typeof node === 'string' || typeof node === 'number') {
|
|
59
112
|
return { type: 'text', content: String(node) };
|
|
60
|
-
} else if (Array.isArray(node)) {
|
|
61
|
-
return {
|
|
62
|
-
type: 'component',
|
|
63
|
-
component: Fragment,
|
|
64
|
-
props: {},
|
|
65
|
-
children: node.map((child) => createTreeNode(child)),
|
|
66
|
-
};
|
|
67
113
|
} else if (node === null || typeof node !== 'object' || !Markdoc.Tag.isTag(node)) {
|
|
68
114
|
return { type: 'text', content: '' };
|
|
69
115
|
}
|
|
70
116
|
|
|
117
|
+
const children = await Promise.all(node.children.map((child) => createTreeNode(child)));
|
|
118
|
+
|
|
71
119
|
if (typeof node.name === 'function') {
|
|
72
120
|
const component = node.name;
|
|
73
121
|
const props = node.attributes;
|
|
74
|
-
const children = node.children.map((child) => createTreeNode(child));
|
|
75
122
|
|
|
76
123
|
return {
|
|
77
124
|
type: 'component',
|
|
@@ -79,12 +126,38 @@ export function createTreeNode(node: RenderableTreeNode | RenderableTreeNode[]):
|
|
|
79
126
|
props,
|
|
80
127
|
children,
|
|
81
128
|
};
|
|
129
|
+
} else if (isPropagatedAssetsModule(node.name)) {
|
|
130
|
+
const { collectedStyles, collectedLinks, collectedScripts } = node.name;
|
|
131
|
+
const component = (await node.name.getMod()).default;
|
|
132
|
+
const props = node.attributes;
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
type: 'component',
|
|
136
|
+
component,
|
|
137
|
+
collectedStyles,
|
|
138
|
+
collectedLinks,
|
|
139
|
+
collectedScripts,
|
|
140
|
+
props,
|
|
141
|
+
children,
|
|
142
|
+
};
|
|
82
143
|
} else {
|
|
83
144
|
return {
|
|
84
145
|
type: 'element',
|
|
85
146
|
tag: node.name,
|
|
86
147
|
attributes: node.attributes,
|
|
87
|
-
children
|
|
148
|
+
children,
|
|
88
149
|
};
|
|
89
150
|
}
|
|
90
151
|
}
|
|
152
|
+
|
|
153
|
+
type PropagatedAssetsModule = {
|
|
154
|
+
__astroPropagation: true;
|
|
155
|
+
getMod: () => Promise<AstroInstance>;
|
|
156
|
+
collectedStyles: string[];
|
|
157
|
+
collectedLinks: string[];
|
|
158
|
+
collectedScripts: string[];
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
function isPropagatedAssetsModule(module: any): module is PropagatedAssetsModule {
|
|
162
|
+
return typeof module === 'object' && module != null && '__astroPropagation' in module;
|
|
163
|
+
}
|
package/dist/config.d.ts
CHANGED
|
@@ -1,36 +1,40 @@
|
|
|
1
|
-
import type { ConfigType as MarkdocConfig } from '@markdoc/markdoc';
|
|
1
|
+
import type { Config, ConfigType as MarkdocConfig, MaybePromise, NodeType, Schema } from '@markdoc/markdoc';
|
|
2
2
|
import _Markdoc from '@markdoc/markdoc';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
import type { AstroInstance } from 'astro';
|
|
4
|
+
type Render = AstroInstance['default'] | string;
|
|
5
|
+
export type AstroMarkdocConfig<C extends Record<string, any> = Record<string, any>> = Omit<MarkdocConfig, 'tags' | 'nodes'> & Partial<{
|
|
6
|
+
tags: Record<string, Schema<Config, Render>>;
|
|
7
|
+
nodes: Partial<Record<NodeType, Schema<Config, Render>>>;
|
|
8
|
+
ctx: C;
|
|
9
|
+
extends: MaybePromise<ResolvedAstroMarkdocConfig>[];
|
|
10
|
+
}>;
|
|
7
11
|
export type ResolvedAstroMarkdocConfig = Omit<AstroMarkdocConfig, 'extends'>;
|
|
8
12
|
export declare const Markdoc: typeof _Markdoc;
|
|
9
13
|
export declare const nodes: {
|
|
10
|
-
heading:
|
|
11
|
-
document:
|
|
12
|
-
paragraph:
|
|
13
|
-
image:
|
|
14
|
-
fence:
|
|
15
|
-
blockquote:
|
|
16
|
-
item:
|
|
17
|
-
list:
|
|
18
|
-
hr:
|
|
19
|
-
table:
|
|
20
|
-
td:
|
|
21
|
-
th:
|
|
22
|
-
tr:
|
|
23
|
-
tbody:
|
|
24
|
-
thead:
|
|
25
|
-
strong:
|
|
26
|
-
em:
|
|
27
|
-
s:
|
|
28
|
-
inline:
|
|
29
|
-
link:
|
|
30
|
-
code:
|
|
31
|
-
text:
|
|
32
|
-
hardbreak:
|
|
33
|
-
softbreak:
|
|
14
|
+
heading: Schema;
|
|
15
|
+
document: Schema;
|
|
16
|
+
paragraph: Schema;
|
|
17
|
+
image: Schema;
|
|
18
|
+
fence: Schema;
|
|
19
|
+
blockquote: Schema;
|
|
20
|
+
item: Schema;
|
|
21
|
+
list: Schema;
|
|
22
|
+
hr: Schema;
|
|
23
|
+
table: Schema;
|
|
24
|
+
td: Schema;
|
|
25
|
+
th: Schema;
|
|
26
|
+
tr: Schema;
|
|
27
|
+
tbody: Schema;
|
|
28
|
+
thead: Schema;
|
|
29
|
+
strong: Schema;
|
|
30
|
+
em: Schema;
|
|
31
|
+
s: Schema;
|
|
32
|
+
inline: Schema;
|
|
33
|
+
link: Schema;
|
|
34
|
+
code: Schema;
|
|
35
|
+
text: Schema;
|
|
36
|
+
hardbreak: Schema;
|
|
37
|
+
softbreak: Schema;
|
|
34
38
|
comment: {
|
|
35
39
|
attributes: {
|
|
36
40
|
content: {
|
|
@@ -43,3 +47,4 @@ export declare const nodes: {
|
|
|
43
47
|
node: {};
|
|
44
48
|
};
|
|
45
49
|
export declare function defineMarkdocConfig(config: AstroMarkdocConfig): AstroMarkdocConfig;
|
|
50
|
+
export {};
|
package/dist/heading-ids.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { type Schema } from '@markdoc/markdoc';
|
|
1
|
+
import { type Config as MarkdocConfig, type Schema } from '@markdoc/markdoc';
|
|
2
2
|
import Slugger from 'github-slugger';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
3
|
+
type HeadingIdConfig = MarkdocConfig & {
|
|
4
|
+
ctx: {
|
|
5
|
+
headingSlugger: Slugger;
|
|
6
|
+
};
|
|
7
|
+
};
|
|
7
8
|
export declare const heading: Schema;
|
|
8
9
|
export declare function setupHeadingConfig(): HeadingIdConfig;
|
|
9
10
|
export {};
|
package/dist/heading-ids.js
CHANGED
|
@@ -33,7 +33,7 @@ const heading = {
|
|
|
33
33
|
// For components, pass down `level` as a prop,
|
|
34
34
|
// alongside `__collectHeading` for our `headings` collector.
|
|
35
35
|
// Avoid accidentally rendering `level` as an HTML attribute otherwise!
|
|
36
|
-
typeof render === "
|
|
36
|
+
typeof render === "string" ? { ...attributes, id: slug } : { ...attributes, id: slug, __collectHeading: true, level }
|
|
37
37
|
);
|
|
38
38
|
return new Markdoc.Tag(render, tagProps, children);
|
|
39
39
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,12 +1,26 @@
|
|
|
1
1
|
import Markdoc from "@markdoc/markdoc";
|
|
2
2
|
import fs from "node:fs";
|
|
3
3
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
createNameHash,
|
|
6
|
+
hasContentFlag,
|
|
7
|
+
isValidUrl,
|
|
8
|
+
MarkdocError,
|
|
9
|
+
parseFrontmatter,
|
|
10
|
+
prependForwardSlash,
|
|
11
|
+
PROPAGATED_ASSET_FLAG
|
|
12
|
+
} from "./utils.js";
|
|
5
13
|
import { emitESMImage } from "astro/assets";
|
|
6
14
|
import { bold, red, yellow } from "kleur/colors";
|
|
7
15
|
import path from "node:path";
|
|
16
|
+
import { normalizePath } from "vite";
|
|
8
17
|
import { loadMarkdocConfig } from "./load-config.js";
|
|
9
18
|
import { setupConfig } from "./runtime.js";
|
|
19
|
+
const markdocTokenizer = new Markdoc.Tokenizer({
|
|
20
|
+
// Strip <!-- comments --> from rendered output
|
|
21
|
+
// Without this, they're rendered as strings!
|
|
22
|
+
allowComments: true
|
|
23
|
+
});
|
|
10
24
|
function markdocIntegration(legacyConfig) {
|
|
11
25
|
if (legacyConfig) {
|
|
12
26
|
console.log(
|
|
@@ -17,6 +31,7 @@ function markdocIntegration(legacyConfig) {
|
|
|
17
31
|
process.exit(0);
|
|
18
32
|
}
|
|
19
33
|
let markdocConfigResult;
|
|
34
|
+
let markdocConfigResultId = "";
|
|
20
35
|
return {
|
|
21
36
|
name: "@astrojs/markdoc",
|
|
22
37
|
hooks: {
|
|
@@ -26,14 +41,10 @@ function markdocIntegration(legacyConfig) {
|
|
|
26
41
|
updateConfig,
|
|
27
42
|
addContentEntryType
|
|
28
43
|
} = params;
|
|
29
|
-
updateConfig({
|
|
30
|
-
vite: {
|
|
31
|
-
ssr: {
|
|
32
|
-
external: ["@astrojs/markdoc/prism", "@astrojs/markdoc/shiki"]
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
44
|
markdocConfigResult = await loadMarkdocConfig(astroConfig);
|
|
45
|
+
if (markdocConfigResult) {
|
|
46
|
+
markdocConfigResultId = normalizePath(fileURLToPath(markdocConfigResult.fileUrl));
|
|
47
|
+
}
|
|
37
48
|
const userMarkdocConfig = (markdocConfigResult == null ? void 0 : markdocConfigResult.config) ?? {};
|
|
38
49
|
function getEntryInfo({ fileUrl, contents }) {
|
|
39
50
|
const parsed = parseFrontmatter(contents, fileURLToPath(fileUrl));
|
|
@@ -47,13 +58,21 @@ function markdocIntegration(legacyConfig) {
|
|
|
47
58
|
addContentEntryType({
|
|
48
59
|
extensions: [".mdoc"],
|
|
49
60
|
getEntryInfo,
|
|
61
|
+
// Markdoc handles script / style propagation
|
|
62
|
+
// for Astro components internally
|
|
63
|
+
handlePropagation: false,
|
|
50
64
|
async getRenderModule({ contents, fileUrl, viteId }) {
|
|
51
65
|
const entry = getEntryInfo({ contents, fileUrl });
|
|
52
|
-
const
|
|
66
|
+
const tokens = markdocTokenizer.tokenize(entry.body);
|
|
67
|
+
const ast = Markdoc.parse(tokens);
|
|
53
68
|
const pluginContext = this;
|
|
54
69
|
const markdocConfig = await setupConfig(userMarkdocConfig);
|
|
55
70
|
const filePath = fileURLToPath(fileUrl);
|
|
56
|
-
const validationErrors = Markdoc.validate(
|
|
71
|
+
const validationErrors = Markdoc.validate(
|
|
72
|
+
ast,
|
|
73
|
+
/* Raised generics issue with Markdoc core https://github.com/markdoc/markdoc/discussions/400 */
|
|
74
|
+
markdocConfig
|
|
75
|
+
).filter((e) => {
|
|
57
76
|
return (
|
|
58
77
|
// Ignore `variable-undefined` errors.
|
|
59
78
|
// Variables can be configured at runtime,
|
|
@@ -84,12 +103,14 @@ function markdocIntegration(legacyConfig) {
|
|
|
84
103
|
filePath
|
|
85
104
|
});
|
|
86
105
|
}
|
|
87
|
-
const res = `import {
|
|
106
|
+
const res = `import {
|
|
107
|
+
createComponent,
|
|
108
|
+
renderComponent,
|
|
109
|
+
} from 'astro/runtime/server/index.js';
|
|
88
110
|
import { Renderer } from '@astrojs/markdoc/components';
|
|
89
111
|
import { collectHeadings, setupConfig, setupConfigSync, Markdoc } from '@astrojs/markdoc/runtime';
|
|
90
|
-
import * as entry from ${JSON.stringify(viteId + "?astroContentCollectionEntry")};
|
|
91
112
|
${markdocConfigResult ? `import _userConfig from ${JSON.stringify(
|
|
92
|
-
|
|
113
|
+
markdocConfigResultId
|
|
93
114
|
)};
|
|
94
115
|
const userConfig = _userConfig ?? {};` : "const userConfig = {};"}${astroConfig.experimental.assets ? `
|
|
95
116
|
import { experimentalAssetsConfig } from '@astrojs/markdoc/experimental-assets-config';
|
|
@@ -104,19 +125,29 @@ export function getHeadings() {
|
|
|
104
125
|
instead of the Content component. Would remove double-transform and unlock variable resolution in heading slugs. */
|
|
105
126
|
""}
|
|
106
127
|
const headingConfig = userConfig.nodes?.heading;
|
|
107
|
-
const config = setupConfigSync(headingConfig ? { nodes: { heading: headingConfig } } : {}
|
|
128
|
+
const config = setupConfigSync(headingConfig ? { nodes: { heading: headingConfig } } : {});
|
|
108
129
|
const ast = Markdoc.Ast.fromJSON(stringifiedAst);
|
|
109
130
|
const content = Markdoc.transform(ast, config);
|
|
110
131
|
return collectHeadings(Array.isArray(content) ? content : content.children);
|
|
111
132
|
}
|
|
112
|
-
export async function Content (props) {
|
|
113
|
-
const config = await setupConfig({
|
|
114
|
-
...userConfig,
|
|
115
|
-
variables: { ...userConfig.variables, ...props },
|
|
116
|
-
}, entry);
|
|
117
133
|
|
|
118
|
-
|
|
119
|
-
|
|
134
|
+
export const Content = createComponent({
|
|
135
|
+
async factory(result, props) {
|
|
136
|
+
const config = await setupConfig({
|
|
137
|
+
...userConfig,
|
|
138
|
+
variables: { ...userConfig.variables, ...props },
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
return renderComponent(
|
|
142
|
+
result,
|
|
143
|
+
Renderer.name,
|
|
144
|
+
Renderer,
|
|
145
|
+
{ stringifiedAst, config },
|
|
146
|
+
{}
|
|
147
|
+
);
|
|
148
|
+
},
|
|
149
|
+
propagation: 'self',
|
|
150
|
+
});`;
|
|
120
151
|
return { code: res };
|
|
121
152
|
},
|
|
122
153
|
contentModuleTypes: await fs.promises.readFile(
|
|
@@ -124,10 +155,53 @@ export async function Content (props) {
|
|
|
124
155
|
"utf-8"
|
|
125
156
|
)
|
|
126
157
|
});
|
|
158
|
+
let rollupOptions = {};
|
|
159
|
+
if (markdocConfigResult) {
|
|
160
|
+
rollupOptions = {
|
|
161
|
+
output: {
|
|
162
|
+
// Split Astro components from your `markdoc.config`
|
|
163
|
+
// to only inject component styles and scripts at runtime.
|
|
164
|
+
manualChunks(id, { getModuleInfo }) {
|
|
165
|
+
var _a, _b;
|
|
166
|
+
if (markdocConfigResult && hasContentFlag(id, PROPAGATED_ASSET_FLAG) && ((_b = (_a = getModuleInfo(id)) == null ? void 0 : _a.importers) == null ? void 0 : _b.includes(markdocConfigResultId))) {
|
|
167
|
+
return createNameHash(id, [id]);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
updateConfig({
|
|
174
|
+
vite: {
|
|
175
|
+
vite: {
|
|
176
|
+
ssr: {
|
|
177
|
+
external: ["@astrojs/markdoc/prism", "@astrojs/markdoc/shiki"]
|
|
178
|
+
}
|
|
179
|
+
},
|
|
180
|
+
build: {
|
|
181
|
+
rollupOptions
|
|
182
|
+
},
|
|
183
|
+
plugins: [
|
|
184
|
+
{
|
|
185
|
+
name: "@astrojs/markdoc:astro-propagated-assets",
|
|
186
|
+
enforce: "pre",
|
|
187
|
+
// Astro component styles and scripts should only be injected
|
|
188
|
+
// When a given Markdoc file actually uses that component.
|
|
189
|
+
// Add the `astroPropagatedAssets` flag to inject only when rendered.
|
|
190
|
+
resolveId(id, importer) {
|
|
191
|
+
if (importer === markdocConfigResultId && id.endsWith(".astro")) {
|
|
192
|
+
return this.resolve(id + "?astroPropagatedAssets", importer, {
|
|
193
|
+
skipSelf: true
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
]
|
|
199
|
+
}
|
|
200
|
+
});
|
|
127
201
|
},
|
|
128
202
|
"astro:server:setup": async ({ server }) => {
|
|
129
203
|
server.watcher.on("all", (event, entry) => {
|
|
130
|
-
if (pathToFileURL(entry).pathname ===
|
|
204
|
+
if (prependForwardSlash(pathToFileURL(entry).pathname) === markdocConfigResultId) {
|
|
131
205
|
console.log(
|
|
132
206
|
yellow(
|
|
133
207
|
`${bold("[Markdoc]")} Restart the dev server for config changes to take effect.`
|
package/dist/load-config.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { Config as MarkdocConfig } from '@markdoc/markdoc';
|
|
2
1
|
import type { AstroConfig } from 'astro';
|
|
2
|
+
import type { AstroMarkdocConfig } from './config.js';
|
|
3
3
|
export type MarkdocConfigResult = {
|
|
4
|
-
config:
|
|
4
|
+
config: AstroMarkdocConfig;
|
|
5
5
|
fileUrl: URL;
|
|
6
6
|
};
|
|
7
7
|
export declare function loadMarkdocConfig(astroConfig: Pick<AstroConfig, 'root'>): Promise<MarkdocConfigResult | undefined>;
|
package/dist/runtime.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { MarkdownHeading } from '@astrojs/markdown-remark';
|
|
2
2
|
import { type RenderableTreeNode } from '@markdoc/markdoc';
|
|
3
|
-
import type { ContentEntryModule } from 'astro';
|
|
4
3
|
import type { AstroMarkdocConfig } from './config.js';
|
|
5
4
|
/** Used to call `Markdoc.transform()` and `Markdoc.Ast` in runtime modules */
|
|
6
5
|
export { default as Markdoc } from '@markdoc/markdoc';
|
|
@@ -11,7 +10,7 @@ export { default as Markdoc } from '@markdoc/markdoc';
|
|
|
11
10
|
*/
|
|
12
11
|
export declare function setupConfig(userConfig: AstroMarkdocConfig): Promise<Omit<AstroMarkdocConfig, 'extends'>>;
|
|
13
12
|
/** Used for synchronous `getHeadings()` function */
|
|
14
|
-
export declare function setupConfigSync(userConfig: AstroMarkdocConfig
|
|
13
|
+
export declare function setupConfigSync(userConfig: AstroMarkdocConfig): Omit<AstroMarkdocConfig, 'extends'>;
|
|
15
14
|
/**
|
|
16
15
|
* Get text content as a string from a Markdoc transform AST
|
|
17
16
|
*/
|
package/dist/runtime.js
CHANGED
|
@@ -13,11 +13,8 @@ async function setupConfig(userConfig) {
|
|
|
13
13
|
}
|
|
14
14
|
return mergeConfig(defaultConfig, userConfig);
|
|
15
15
|
}
|
|
16
|
-
function setupConfigSync(userConfig
|
|
17
|
-
|
|
18
|
-
...setupHeadingConfig(),
|
|
19
|
-
variables: { entry }
|
|
20
|
-
};
|
|
16
|
+
function setupConfigSync(userConfig) {
|
|
17
|
+
const defaultConfig = setupHeadingConfig();
|
|
21
18
|
return mergeConfig(defaultConfig, userConfig);
|
|
22
19
|
}
|
|
23
20
|
function mergeConfig(configA, configB) {
|
package/dist/utils.d.ts
CHANGED
|
@@ -37,4 +37,18 @@ interface ErrorProperties {
|
|
|
37
37
|
*/
|
|
38
38
|
export declare function prependForwardSlash(str: string): string;
|
|
39
39
|
export declare function isValidUrl(str: string): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Identifies Astro components with propagated assets
|
|
42
|
+
* @see 'packages/astro/src/content/consts.ts'
|
|
43
|
+
*/
|
|
44
|
+
export declare const PROPAGATED_ASSET_FLAG = "astroPropagatedAssets";
|
|
45
|
+
/**
|
|
46
|
+
* @see 'packages/astro/src/content/utils.ts'
|
|
47
|
+
*/
|
|
48
|
+
export declare function hasContentFlag(viteId: string, flag: string): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Create build hash for manual Rollup chunks.
|
|
51
|
+
* @see 'packages/astro/src/core/build/plugins/plugin-css.ts'
|
|
52
|
+
*/
|
|
53
|
+
export declare function createNameHash(baseId: string, hashIds: string[]): string;
|
|
40
54
|
export {};
|
package/dist/utils.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import matter from "gray-matter";
|
|
2
|
+
import crypto from "node:crypto";
|
|
3
|
+
import path from "node:path";
|
|
2
4
|
function parseFrontmatter(fileContents, filePath) {
|
|
3
5
|
try {
|
|
4
6
|
matter.clearCache();
|
|
@@ -52,8 +54,26 @@ function isValidUrl(str) {
|
|
|
52
54
|
return false;
|
|
53
55
|
}
|
|
54
56
|
}
|
|
57
|
+
const PROPAGATED_ASSET_FLAG = "astroPropagatedAssets";
|
|
58
|
+
function hasContentFlag(viteId, flag) {
|
|
59
|
+
const flags = new URLSearchParams(viteId.split("?")[1] ?? "");
|
|
60
|
+
return flags.has(flag);
|
|
61
|
+
}
|
|
62
|
+
function createNameHash(baseId, hashIds) {
|
|
63
|
+
const baseName = baseId ? path.parse(baseId).name : "index";
|
|
64
|
+
const hash = crypto.createHash("sha256");
|
|
65
|
+
for (const id of hashIds) {
|
|
66
|
+
hash.update(id, "utf-8");
|
|
67
|
+
}
|
|
68
|
+
const h = hash.digest("hex").slice(0, 8);
|
|
69
|
+
const proposedName = baseName + "." + h;
|
|
70
|
+
return proposedName;
|
|
71
|
+
}
|
|
55
72
|
export {
|
|
56
73
|
MarkdocError,
|
|
74
|
+
PROPAGATED_ASSET_FLAG,
|
|
75
|
+
createNameHash,
|
|
76
|
+
hasContentFlag,
|
|
57
77
|
isValidUrl,
|
|
58
78
|
parseFrontmatter,
|
|
59
79
|
prependForwardSlash
|
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.3.
|
|
4
|
+
"version": "0.3.2",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"author": "withastro",
|
|
@@ -19,15 +19,37 @@
|
|
|
19
19
|
"bugs": "https://github.com/withastro/astro/issues",
|
|
20
20
|
"homepage": "https://docs.astro.build/en/guides/integrations-guide/markdoc/",
|
|
21
21
|
"exports": {
|
|
22
|
-
"./prism":
|
|
23
|
-
|
|
22
|
+
"./prism": {
|
|
23
|
+
"types": "./dist/extensions/prism.d.ts",
|
|
24
|
+
"node": "./dist/extensions/prism.js"
|
|
25
|
+
},
|
|
26
|
+
"./shiki": {
|
|
27
|
+
"types": "./dist/extensions/shiki.d.ts",
|
|
28
|
+
"node": "./dist/extensions/shiki.js"
|
|
29
|
+
},
|
|
30
|
+
"./config": {
|
|
31
|
+
"types": "./dist/config.d.ts",
|
|
32
|
+
"node": "./dist/config.js"
|
|
33
|
+
},
|
|
24
34
|
".": "./dist/index.js",
|
|
25
35
|
"./components": "./components/index.ts",
|
|
26
36
|
"./runtime": "./dist/runtime.js",
|
|
27
|
-
"./config": "./dist/config.js",
|
|
28
37
|
"./experimental-assets-config": "./dist/experimental-assets-config.js",
|
|
29
38
|
"./package.json": "./package.json"
|
|
30
39
|
},
|
|
40
|
+
"typesVersions": {
|
|
41
|
+
"*": {
|
|
42
|
+
"config": [
|
|
43
|
+
"./dist/config.d.ts"
|
|
44
|
+
],
|
|
45
|
+
"prism": [
|
|
46
|
+
"./dist/extensions/prism.d.ts"
|
|
47
|
+
],
|
|
48
|
+
"shiki": [
|
|
49
|
+
"./dist/extensions/shiki.d.ts"
|
|
50
|
+
]
|
|
51
|
+
}
|
|
52
|
+
},
|
|
31
53
|
"files": [
|
|
32
54
|
"components",
|
|
33
55
|
"dist",
|
|
@@ -44,7 +66,7 @@
|
|
|
44
66
|
"zod": "^3.17.3"
|
|
45
67
|
},
|
|
46
68
|
"peerDependencies": {
|
|
47
|
-
"astro": "^2.
|
|
69
|
+
"astro": "^2.6.1"
|
|
48
70
|
},
|
|
49
71
|
"devDependencies": {
|
|
50
72
|
"@astrojs/markdown-remark": "^2.2.1",
|
|
@@ -57,7 +79,7 @@
|
|
|
57
79
|
"mocha": "^9.2.2",
|
|
58
80
|
"rollup": "^3.20.1",
|
|
59
81
|
"vite": "^4.3.1",
|
|
60
|
-
"astro": "2.
|
|
82
|
+
"astro": "2.6.1",
|
|
61
83
|
"astro-scripts": "0.0.14"
|
|
62
84
|
},
|
|
63
85
|
"engines": {
|