@astrojs/markdoc 0.2.0 → 0.2.1
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/dist/index.js +5 -7
- package/dist/nodes/heading.d.ts +8 -2
- package/dist/nodes/heading.js +14 -4
- package/dist/nodes/index.d.ts +1 -1
- package/dist/nodes/index.js +3 -3
- package/dist/runtime.d.ts +6 -3
- package/dist/runtime.js +26 -12
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -5,7 +5,7 @@ import { isValidUrl, MarkdocError, parseFrontmatter, prependForwardSlash } from
|
|
|
5
5
|
import { emitESMImage } from "astro/assets";
|
|
6
6
|
import { bold, red, yellow } from "kleur/colors";
|
|
7
7
|
import { loadMarkdocConfig } from "./load-config.js";
|
|
8
|
-
import {
|
|
8
|
+
import { setupConfig } from "./runtime.js";
|
|
9
9
|
function markdocIntegration(legacyConfig) {
|
|
10
10
|
if (legacyConfig) {
|
|
11
11
|
console.log(
|
|
@@ -38,7 +38,7 @@ function markdocIntegration(legacyConfig) {
|
|
|
38
38
|
async getRenderModule({ entry, viteId }) {
|
|
39
39
|
const ast = Markdoc.parse(entry.body);
|
|
40
40
|
const pluginContext = this;
|
|
41
|
-
const markdocConfig =
|
|
41
|
+
const markdocConfig = setupConfig(userMarkdocConfig, entry);
|
|
42
42
|
const validationErrors = Markdoc.validate(ast, markdocConfig).filter((e) => {
|
|
43
43
|
return (
|
|
44
44
|
// Ignore `variable-undefined` errors.
|
|
@@ -71,7 +71,7 @@ function markdocIntegration(legacyConfig) {
|
|
|
71
71
|
}
|
|
72
72
|
const res = `import { jsx as h } from 'astro/jsx-runtime';
|
|
73
73
|
import { Renderer } from '@astrojs/markdoc/components';
|
|
74
|
-
import { collectHeadings,
|
|
74
|
+
import { collectHeadings, setupConfig, Markdoc } from '@astrojs/markdoc/runtime';
|
|
75
75
|
import * as entry from ${JSON.stringify(viteId + "?astroContentCollectionEntry")};
|
|
76
76
|
${markdocConfigResult ? `import _userConfig from ${JSON.stringify(
|
|
77
77
|
markdocConfigResult.fileUrl.pathname
|
|
@@ -88,16 +88,14 @@ export function getHeadings() {
|
|
|
88
88
|
TODO: propose new `render()` API to allow Markdoc variable passing to `render()` itself,
|
|
89
89
|
instead of the Content component. Would remove double-transform and unlock variable resolution in heading slugs. */
|
|
90
90
|
""}
|
|
91
|
-
headingSlugger.reset();
|
|
92
91
|
const headingConfig = userConfig.nodes?.heading;
|
|
93
|
-
const config =
|
|
92
|
+
const config = setupConfig(headingConfig ? { nodes: { heading: headingConfig } } : {}, entry);
|
|
94
93
|
const ast = Markdoc.Ast.fromJSON(stringifiedAst);
|
|
95
94
|
const content = Markdoc.transform(ast, config);
|
|
96
95
|
return collectHeadings(Array.isArray(content) ? content : content.children);
|
|
97
96
|
}
|
|
98
97
|
export async function Content (props) {
|
|
99
|
-
|
|
100
|
-
const config = applyDefaultConfig({
|
|
98
|
+
const config = setupConfig({
|
|
101
99
|
...userConfig,
|
|
102
100
|
variables: { ...userConfig.variables, ...props },
|
|
103
101
|
}, entry);
|
package/dist/nodes/heading.d.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import { type Schema } from '@markdoc/markdoc';
|
|
1
|
+
import { type ConfigType, type Schema } from '@markdoc/markdoc';
|
|
2
2
|
import Slugger from 'github-slugger';
|
|
3
|
-
|
|
3
|
+
type ConfigTypeWithCtx = ConfigType & {
|
|
4
|
+
ctx: {
|
|
5
|
+
headingSlugger: Slugger;
|
|
6
|
+
};
|
|
7
|
+
};
|
|
4
8
|
export declare const heading: Schema;
|
|
9
|
+
export declare function setupHeadingConfig(): ConfigTypeWithCtx;
|
|
10
|
+
export {};
|
package/dist/nodes/heading.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import Markdoc from "@markdoc/markdoc";
|
|
2
2
|
import Slugger from "github-slugger";
|
|
3
3
|
import { getTextContent } from "../runtime.js";
|
|
4
|
-
|
|
5
|
-
function getSlug(attributes, children) {
|
|
4
|
+
function getSlug(attributes, children, headingSlugger) {
|
|
6
5
|
if (attributes.id && typeof attributes.id === "string") {
|
|
7
6
|
return attributes.id;
|
|
8
7
|
}
|
|
@@ -22,7 +21,7 @@ const heading = {
|
|
|
22
21
|
var _a, _b;
|
|
23
22
|
const { level, ...attributes } = node.transformAttributes(config);
|
|
24
23
|
const children = node.transformChildren(config);
|
|
25
|
-
const slug = getSlug(attributes, children);
|
|
24
|
+
const slug = getSlug(attributes, children, config.ctx.headingSlugger);
|
|
26
25
|
const render = ((_b = (_a = config.nodes) == null ? void 0 : _a.heading) == null ? void 0 : _b.render) ?? `h${level}`;
|
|
27
26
|
const tagProps = (
|
|
28
27
|
// For components, pass down `level` as a prop,
|
|
@@ -33,7 +32,18 @@ const heading = {
|
|
|
33
32
|
return new Markdoc.Tag(render, tagProps, children);
|
|
34
33
|
}
|
|
35
34
|
};
|
|
35
|
+
function setupHeadingConfig() {
|
|
36
|
+
const headingSlugger = new Slugger();
|
|
37
|
+
return {
|
|
38
|
+
ctx: {
|
|
39
|
+
headingSlugger
|
|
40
|
+
},
|
|
41
|
+
nodes: {
|
|
42
|
+
heading
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
36
46
|
export {
|
|
37
47
|
heading,
|
|
38
|
-
|
|
48
|
+
setupHeadingConfig
|
|
39
49
|
};
|
package/dist/nodes/index.d.ts
CHANGED
package/dist/nodes/index.js
CHANGED
package/dist/runtime.d.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import type { MarkdownHeading } from '@astrojs/markdown-remark';
|
|
2
2
|
import { type ConfigType as MarkdocConfig, type RenderableTreeNode } from '@markdoc/markdoc';
|
|
3
3
|
import type { ContentEntryModule } from 'astro';
|
|
4
|
-
/** Used to
|
|
4
|
+
/** Used to call `Markdoc.transform()` and `Markdoc.Ast` in runtime modules */
|
|
5
5
|
export { default as Markdoc } from '@markdoc/markdoc';
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Merge user config with default config and set up context (ex. heading ID slugger)
|
|
8
|
+
* Called on each file's individual transform
|
|
9
|
+
*/
|
|
10
|
+
export declare function setupConfig(userConfig: MarkdocConfig, entry: ContentEntryModule): MarkdocConfig;
|
|
8
11
|
/**
|
|
9
12
|
* Get text content as a string from a Markdoc transform AST
|
|
10
13
|
*/
|
package/dist/runtime.js
CHANGED
|
@@ -1,19 +1,34 @@
|
|
|
1
1
|
import Markdoc from "@markdoc/markdoc";
|
|
2
|
-
import {
|
|
2
|
+
import { setupHeadingConfig } from "./nodes/index.js";
|
|
3
3
|
import { default as default2 } from "@markdoc/markdoc";
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
function setupConfig(userConfig, entry) {
|
|
5
|
+
const defaultConfig = {
|
|
6
|
+
// `setupXConfig()` could become a "plugin" convention as well?
|
|
7
|
+
...setupHeadingConfig(),
|
|
8
|
+
variables: { entry }
|
|
9
|
+
};
|
|
10
|
+
return mergeConfig(defaultConfig, userConfig);
|
|
11
|
+
}
|
|
12
|
+
function mergeConfig(configA, configB) {
|
|
6
13
|
return {
|
|
7
|
-
...
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
...
|
|
14
|
+
...configA,
|
|
15
|
+
...configB,
|
|
16
|
+
tags: {
|
|
17
|
+
...configA.tags,
|
|
18
|
+
...configB.tags
|
|
11
19
|
},
|
|
12
20
|
nodes: {
|
|
13
|
-
...
|
|
14
|
-
...
|
|
21
|
+
...configA.nodes,
|
|
22
|
+
...configB.nodes
|
|
23
|
+
},
|
|
24
|
+
functions: {
|
|
25
|
+
...configA.functions,
|
|
26
|
+
...configB.functions
|
|
27
|
+
},
|
|
28
|
+
variables: {
|
|
29
|
+
...configA.variables,
|
|
30
|
+
...configB.variables
|
|
15
31
|
}
|
|
16
|
-
// TODO: Syntax highlighting
|
|
17
32
|
};
|
|
18
33
|
}
|
|
19
34
|
function getTextContent(childNodes) {
|
|
@@ -56,8 +71,7 @@ function collectHeadings(children) {
|
|
|
56
71
|
}
|
|
57
72
|
export {
|
|
58
73
|
default2 as Markdoc,
|
|
59
|
-
applyDefaultConfig,
|
|
60
74
|
collectHeadings,
|
|
61
75
|
getTextContent,
|
|
62
|
-
|
|
76
|
+
setupConfig
|
|
63
77
|
};
|
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.1",
|
|
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.1"
|
|
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.1",
|
|
57
57
|
"astro-scripts": "0.0.14"
|
|
58
58
|
},
|
|
59
59
|
"engines": {
|