@astrojs/markdoc 0.1.2 → 0.2.0
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 +8 -11
- package/dist/config.d.ts +38 -0
- package/dist/config.js +7 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +68 -25
- package/dist/load-config.d.ts +5 -12
- package/dist/nodes/heading.d.ts +4 -0
- package/dist/nodes/heading.js +39 -0
- package/dist/nodes/index.d.ts +4 -0
- package/dist/nodes/index.js +7 -0
- package/dist/runtime.d.ts +16 -0
- package/dist/runtime.js +63 -0
- package/package.json +11 -4
- package/template/content-module-types.d.ts +1 -0
- package/.turbo/turbo-build.log +0 -4
- package/CHANGELOG.md +0 -129
- package/dist/default-config.d.ts +0 -5
- package/dist/default-config.js +0 -13
- package/src/config.ts +0 -5
- package/src/default-config.ts +0 -18
- package/src/experimental-assets-config.ts +0 -29
- package/src/index.ts +0 -164
- package/src/load-config.ts +0 -102
- package/src/utils.ts +0 -98
- package/test/content-collections.test.js +0 -110
- package/test/entry-prop.test.js +0 -58
- package/test/fixtures/content-collections/astro.config.mjs +0 -7
- package/test/fixtures/content-collections/node_modules/.bin/astro +0 -17
- package/test/fixtures/content-collections/package.json +0 -9
- package/test/fixtures/content-collections/src/content/blog/post-1.mdoc +0 -7
- package/test/fixtures/content-collections/src/content/blog/post-2.mdoc +0 -7
- package/test/fixtures/content-collections/src/content/blog/post-3.mdoc +0 -7
- package/test/fixtures/content-collections/src/content/config.ts +0 -12
- package/test/fixtures/content-collections/src/pages/collection.json.js +0 -10
- package/test/fixtures/content-collections/src/pages/entry.json.js +0 -10
- package/test/fixtures/content-collections/utils.js +0 -8
- package/test/fixtures/entry-prop/astro.config.mjs +0 -7
- package/test/fixtures/entry-prop/node_modules/.bin/astro +0 -17
- package/test/fixtures/entry-prop/package.json +0 -9
- package/test/fixtures/entry-prop/src/content/blog/entry.mdoc +0 -9
- package/test/fixtures/entry-prop/src/content/config.ts +0 -9
- package/test/fixtures/entry-prop/src/pages/index.astro +0 -19
- package/test/fixtures/image-assets/astro.config.mjs +0 -10
- package/test/fixtures/image-assets/node_modules/.bin/astro +0 -17
- package/test/fixtures/image-assets/package.json +0 -9
- package/test/fixtures/image-assets/src/assets/alias/cityscape.jpg +0 -0
- package/test/fixtures/image-assets/src/assets/relative/oar.jpg +0 -0
- package/test/fixtures/image-assets/src/content/docs/intro.mdoc +0 -7
- package/test/fixtures/image-assets/src/pages/index.astro +0 -19
- package/test/fixtures/image-assets/src/public/favicon.svg +0 -9
- package/test/fixtures/render-null/astro.config.mjs +0 -7
- package/test/fixtures/render-null/markdoc.config.mjs +0 -26
- package/test/fixtures/render-null/node_modules/.bin/astro +0 -17
- package/test/fixtures/render-null/package.json +0 -9
- package/test/fixtures/render-null/src/content/blog/render-null.mdoc +0 -7
- package/test/fixtures/render-null/src/pages/index.astro +0 -19
- package/test/fixtures/render-simple/astro.config.mjs +0 -7
- package/test/fixtures/render-simple/node_modules/.bin/astro +0 -17
- package/test/fixtures/render-simple/package.json +0 -9
- package/test/fixtures/render-simple/src/content/blog/simple.mdoc +0 -7
- package/test/fixtures/render-simple/src/pages/index.astro +0 -19
- package/test/fixtures/render-with-components/astro.config.mjs +0 -7
- package/test/fixtures/render-with-components/markdoc.config.mjs +0 -28
- package/test/fixtures/render-with-components/node_modules/.bin/astro +0 -17
- package/test/fixtures/render-with-components/package.json +0 -12
- package/test/fixtures/render-with-components/src/components/Code.astro +0 -12
- package/test/fixtures/render-with-components/src/components/CustomMarquee.astro +0 -1
- package/test/fixtures/render-with-components/src/content/blog/with-components.mdoc +0 -17
- package/test/fixtures/render-with-components/src/pages/index.astro +0 -19
- package/test/fixtures/render-with-config/astro.config.mjs +0 -7
- package/test/fixtures/render-with-config/markdoc.config.mjs +0 -15
- package/test/fixtures/render-with-config/node_modules/.bin/astro +0 -17
- package/test/fixtures/render-with-config/package.json +0 -9
- package/test/fixtures/render-with-config/src/content/blog/with-config.mdoc +0 -13
- package/test/fixtures/render-with-config/src/pages/index.astro +0 -19
- package/test/image-assets.test.js +0 -76
- package/test/render.test.js +0 -150
- package/tsconfig.json +0 -10
package/README.md
CHANGED
|
@@ -143,32 +143,29 @@ Use tags like this fancy "aside" to add some *flair* to your docs.
|
|
|
143
143
|
|
|
144
144
|
#### Render Markdoc nodes / HTML elements as Astro components
|
|
145
145
|
|
|
146
|
-
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,
|
|
146
|
+
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, and passes through Astro's default heading properties to define attributes and generate heading ids / slugs:
|
|
147
147
|
|
|
148
148
|
```js
|
|
149
149
|
// markdoc.config.mjs
|
|
150
|
-
import { defineMarkdocConfig } from '@astrojs/markdoc/config';
|
|
150
|
+
import { defineMarkdocConfig, nodes } from '@astrojs/markdoc/config';
|
|
151
151
|
import Heading from './src/components/Heading.astro';
|
|
152
152
|
|
|
153
153
|
export default defineMarkdocConfig({
|
|
154
154
|
nodes: {
|
|
155
155
|
heading: {
|
|
156
156
|
render: Heading,
|
|
157
|
-
|
|
158
|
-
// Pass the attributes from Markdoc's default heading node
|
|
159
|
-
// as component props.
|
|
160
|
-
level: { type: String },
|
|
161
|
-
}
|
|
157
|
+
...nodes.heading,
|
|
162
158
|
},
|
|
163
159
|
},
|
|
164
160
|
})
|
|
165
161
|
```
|
|
166
162
|
|
|
167
|
-
|
|
163
|
+
All Markdown headings will render the `Heading.astro` component and pass `attributes` as component props. For headings, Astro provides the following attributes by default:
|
|
168
164
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
165
|
+
- `level: number` The heading level 1 - 6
|
|
166
|
+
- `id: string` An `id` generated from the heading's text contents. This corresponds to the `slug` generated by the [content `render()` function](https://docs.astro.build/en/guides/content-collections/#rendering-content-to-html).
|
|
167
|
+
|
|
168
|
+
For example, the heading `### Level 3 heading!` will pass `level: 3` and `id: 'level-3-heading'` as component props.
|
|
172
169
|
|
|
173
170
|
📚 [Find all of Markdoc's built-in nodes and node attributes on their documentation.](https://markdoc.dev/docs/nodes#built-in-nodes)
|
|
174
171
|
|
package/dist/config.d.ts
CHANGED
|
@@ -1,2 +1,40 @@
|
|
|
1
1
|
import type { ConfigType as MarkdocConfig } from '@markdoc/markdoc';
|
|
2
|
+
import _Markdoc from '@markdoc/markdoc';
|
|
3
|
+
export declare const Markdoc: typeof _Markdoc;
|
|
4
|
+
export declare const nodes: {
|
|
5
|
+
heading: import("@markdoc/markdoc").Schema;
|
|
6
|
+
document: import("@markdoc/markdoc").Schema;
|
|
7
|
+
paragraph: import("@markdoc/markdoc").Schema;
|
|
8
|
+
image: import("@markdoc/markdoc").Schema;
|
|
9
|
+
fence: import("@markdoc/markdoc").Schema;
|
|
10
|
+
blockquote: import("@markdoc/markdoc").Schema;
|
|
11
|
+
item: import("@markdoc/markdoc").Schema;
|
|
12
|
+
list: import("@markdoc/markdoc").Schema;
|
|
13
|
+
hr: import("@markdoc/markdoc").Schema;
|
|
14
|
+
table: import("@markdoc/markdoc").Schema;
|
|
15
|
+
td: import("@markdoc/markdoc").Schema;
|
|
16
|
+
th: import("@markdoc/markdoc").Schema;
|
|
17
|
+
tr: import("@markdoc/markdoc").Schema;
|
|
18
|
+
tbody: import("@markdoc/markdoc").Schema;
|
|
19
|
+
thead: import("@markdoc/markdoc").Schema;
|
|
20
|
+
strong: import("@markdoc/markdoc").Schema;
|
|
21
|
+
em: import("@markdoc/markdoc").Schema;
|
|
22
|
+
s: import("@markdoc/markdoc").Schema;
|
|
23
|
+
inline: import("@markdoc/markdoc").Schema;
|
|
24
|
+
link: import("@markdoc/markdoc").Schema;
|
|
25
|
+
code: import("@markdoc/markdoc").Schema;
|
|
26
|
+
text: import("@markdoc/markdoc").Schema;
|
|
27
|
+
hardbreak: import("@markdoc/markdoc").Schema;
|
|
28
|
+
softbreak: import("@markdoc/markdoc").Schema;
|
|
29
|
+
comment: {
|
|
30
|
+
attributes: {
|
|
31
|
+
content: {
|
|
32
|
+
type: StringConstructor;
|
|
33
|
+
required: boolean;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
error: {};
|
|
38
|
+
node: {};
|
|
39
|
+
};
|
|
2
40
|
export declare function defineMarkdocConfig(config: MarkdocConfig): MarkdocConfig;
|
package/dist/config.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
|
+
import _Markdoc from "@markdoc/markdoc";
|
|
2
|
+
import { nodes as astroNodes } from "./nodes/index.js";
|
|
3
|
+
const Markdoc = _Markdoc;
|
|
4
|
+
const nodes = { ...Markdoc.nodes, ...astroNodes };
|
|
1
5
|
function defineMarkdocConfig(config) {
|
|
2
6
|
return config;
|
|
3
7
|
}
|
|
4
8
|
export {
|
|
5
|
-
|
|
9
|
+
Markdoc,
|
|
10
|
+
defineMarkdocConfig,
|
|
11
|
+
nodes
|
|
6
12
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { AstroIntegration } from 'astro';
|
|
2
|
-
export default function markdocIntegration(legacyConfig
|
|
2
|
+
export default function markdocIntegration(legacyConfig?: any): AstroIntegration;
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import Markdoc from "@markdoc/markdoc";
|
|
2
2
|
import fs from "node:fs";
|
|
3
|
-
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
4
4
|
import { isValidUrl, MarkdocError, parseFrontmatter, prependForwardSlash } from "./utils.js";
|
|
5
5
|
import { emitESMImage } from "astro/assets";
|
|
6
|
-
import { bold, red } from "kleur/colors";
|
|
7
|
-
import { applyDefaultConfig } from "./default-config.js";
|
|
6
|
+
import { bold, red, yellow } from "kleur/colors";
|
|
8
7
|
import { loadMarkdocConfig } from "./load-config.js";
|
|
8
|
+
import { applyDefaultConfig } from "./runtime.js";
|
|
9
9
|
function markdocIntegration(legacyConfig) {
|
|
10
10
|
if (legacyConfig) {
|
|
11
11
|
console.log(
|
|
@@ -15,13 +15,14 @@ function markdocIntegration(legacyConfig) {
|
|
|
15
15
|
);
|
|
16
16
|
process.exit(0);
|
|
17
17
|
}
|
|
18
|
+
let markdocConfigResult;
|
|
18
19
|
return {
|
|
19
20
|
name: "@astrojs/markdoc",
|
|
20
21
|
hooks: {
|
|
21
22
|
"astro:config:setup": async (params) => {
|
|
22
23
|
const { config: astroConfig, addContentEntryType } = params;
|
|
23
|
-
|
|
24
|
-
const userMarkdocConfig = (
|
|
24
|
+
markdocConfigResult = await loadMarkdocConfig(astroConfig);
|
|
25
|
+
const userMarkdocConfig = (markdocConfigResult == null ? void 0 : markdocConfigResult.config) ?? {};
|
|
25
26
|
function getEntryInfo({ fileUrl, contents }) {
|
|
26
27
|
const parsed = parseFrontmatter(contents, fileURLToPath(fileUrl));
|
|
27
28
|
return {
|
|
@@ -37,16 +38,28 @@ function markdocIntegration(legacyConfig) {
|
|
|
37
38
|
async getRenderModule({ entry, viteId }) {
|
|
38
39
|
const ast = Markdoc.parse(entry.body);
|
|
39
40
|
const pluginContext = this;
|
|
40
|
-
const markdocConfig = applyDefaultConfig(userMarkdocConfig,
|
|
41
|
+
const markdocConfig = applyDefaultConfig(userMarkdocConfig, entry);
|
|
41
42
|
const validationErrors = Markdoc.validate(ast, markdocConfig).filter((e) => {
|
|
42
|
-
return
|
|
43
|
+
return (
|
|
44
|
+
// Ignore `variable-undefined` errors.
|
|
45
|
+
// Variables can be configured at runtime,
|
|
46
|
+
// so we cannot validate them at build time.
|
|
47
|
+
e.error.id !== "variable-undefined" && (e.error.level === "error" || e.error.level === "critical")
|
|
48
|
+
);
|
|
43
49
|
});
|
|
44
50
|
if (validationErrors.length) {
|
|
51
|
+
const frontmatterBlockOffset = entry._internal.rawData.split("\n").length + 2;
|
|
45
52
|
throw new MarkdocError({
|
|
46
53
|
message: [
|
|
47
|
-
`**${String(entry.collection)} \u2192 ${String(entry.id)}**
|
|
48
|
-
...validationErrors.map((e) => e.error.
|
|
49
|
-
].join("\n")
|
|
54
|
+
`**${String(entry.collection)} \u2192 ${String(entry.id)}** contains invalid content:`,
|
|
55
|
+
...validationErrors.map((e) => `- ${e.error.message}`)
|
|
56
|
+
].join("\n"),
|
|
57
|
+
location: {
|
|
58
|
+
// Error overlay does not support multi-line or ranges.
|
|
59
|
+
// Just point to the first line.
|
|
60
|
+
line: frontmatterBlockOffset + validationErrors[0].lines[0],
|
|
61
|
+
file: viteId
|
|
62
|
+
}
|
|
50
63
|
});
|
|
51
64
|
}
|
|
52
65
|
if (astroConfig.experimental.assets) {
|
|
@@ -56,29 +69,59 @@ function markdocIntegration(legacyConfig) {
|
|
|
56
69
|
filePath: entry._internal.filePath
|
|
57
70
|
});
|
|
58
71
|
}
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
import { applyDefaultConfig } from '@astrojs/markdoc/
|
|
62
|
-
import
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
72
|
+
const res = `import { jsx as h } from 'astro/jsx-runtime';
|
|
73
|
+
import { Renderer } from '@astrojs/markdoc/components';
|
|
74
|
+
import { collectHeadings, applyDefaultConfig, Markdoc, headingSlugger } from '@astrojs/markdoc/runtime';
|
|
75
|
+
import * as entry from ${JSON.stringify(viteId + "?astroContentCollectionEntry")};
|
|
76
|
+
${markdocConfigResult ? `import _userConfig from ${JSON.stringify(
|
|
77
|
+
markdocConfigResult.fileUrl.pathname
|
|
78
|
+
)};
|
|
79
|
+
const userConfig = _userConfig ?? {};` : "const userConfig = {};"}${astroConfig.experimental.assets ? `
|
|
80
|
+
import { experimentalAssetsConfig } from '@astrojs/markdoc/experimental-assets-config';
|
|
81
|
+
userConfig.nodes = { ...experimentalAssetsConfig.nodes, ...userConfig.nodes };` : ""}
|
|
66
82
|
const stringifiedAst = ${JSON.stringify(
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
83
|
+
/* Double stringify to encode *as* stringified JSON */
|
|
84
|
+
JSON.stringify(ast)
|
|
85
|
+
)};
|
|
86
|
+
export function getHeadings() {
|
|
87
|
+
${/* Yes, we are transforming twice (once from `getHeadings()` and again from <Content /> in case of variables).
|
|
88
|
+
TODO: propose new `render()` API to allow Markdoc variable passing to `render()` itself,
|
|
89
|
+
instead of the Content component. Would remove double-transform and unlock variable resolution in heading slugs. */
|
|
90
|
+
""}
|
|
91
|
+
headingSlugger.reset();
|
|
92
|
+
const headingConfig = userConfig.nodes?.heading;
|
|
93
|
+
const config = applyDefaultConfig(headingConfig ? { nodes: { heading: headingConfig } } : {}, entry);
|
|
94
|
+
const ast = Markdoc.Ast.fromJSON(stringifiedAst);
|
|
95
|
+
const content = Markdoc.transform(ast, config);
|
|
96
|
+
return collectHeadings(Array.isArray(content) ? content : content.children);
|
|
97
|
+
}
|
|
70
98
|
export async function Content (props) {
|
|
71
|
-
|
|
72
|
-
config
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
99
|
+
headingSlugger.reset();
|
|
100
|
+
const config = applyDefaultConfig({
|
|
101
|
+
...userConfig,
|
|
102
|
+
variables: { ...userConfig.variables, ...props },
|
|
103
|
+
}, entry);
|
|
104
|
+
|
|
105
|
+
return h(Renderer, { config, stringifiedAst });
|
|
106
|
+
}`;
|
|
107
|
+
return { code: res };
|
|
76
108
|
},
|
|
77
109
|
contentModuleTypes: await fs.promises.readFile(
|
|
78
110
|
new URL("../template/content-module-types.d.ts", import.meta.url),
|
|
79
111
|
"utf-8"
|
|
80
112
|
)
|
|
81
113
|
});
|
|
114
|
+
},
|
|
115
|
+
"astro:server:setup": async ({ server }) => {
|
|
116
|
+
server.watcher.on("all", (event, entry) => {
|
|
117
|
+
if (pathToFileURL(entry).pathname === (markdocConfigResult == null ? void 0 : markdocConfigResult.fileUrl.pathname)) {
|
|
118
|
+
console.log(
|
|
119
|
+
yellow(
|
|
120
|
+
`${bold("[Markdoc]")} Restart the dev server for config changes to take effect.`
|
|
121
|
+
)
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
82
125
|
}
|
|
83
126
|
}
|
|
84
127
|
};
|
package/dist/load-config.d.ts
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
1
|
+
import type { Config as MarkdocConfig } from '@markdoc/markdoc';
|
|
1
2
|
import type { AstroConfig } from 'astro';
|
|
2
|
-
export
|
|
3
|
-
config:
|
|
4
|
-
nodes: Partial<Record<import("@markdoc/markdoc").NodeType, import("@markdoc/markdoc").Schema<Readonly<Partial<any>>, string>>>;
|
|
5
|
-
tags: Record<string, import("@markdoc/markdoc").Schema<Readonly<Partial<any>>, string>>;
|
|
6
|
-
variables: Record<string, any>;
|
|
7
|
-
functions: Record<string, import("@markdoc/markdoc").ConfigFunction>;
|
|
8
|
-
partials: Record<string, any>;
|
|
9
|
-
validation?: {
|
|
10
|
-
validateFunctions?: boolean | undefined;
|
|
11
|
-
} | undefined;
|
|
12
|
-
}>>;
|
|
3
|
+
export type MarkdocConfigResult = {
|
|
4
|
+
config: MarkdocConfig;
|
|
13
5
|
fileUrl: URL;
|
|
14
|
-
}
|
|
6
|
+
};
|
|
7
|
+
export declare function loadMarkdocConfig(astroConfig: Pick<AstroConfig, 'root'>): Promise<MarkdocConfigResult | undefined>;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import Markdoc from "@markdoc/markdoc";
|
|
2
|
+
import Slugger from "github-slugger";
|
|
3
|
+
import { getTextContent } from "../runtime.js";
|
|
4
|
+
const headingSlugger = new Slugger();
|
|
5
|
+
function getSlug(attributes, children) {
|
|
6
|
+
if (attributes.id && typeof attributes.id === "string") {
|
|
7
|
+
return attributes.id;
|
|
8
|
+
}
|
|
9
|
+
const textContent = attributes.content ?? getTextContent(children);
|
|
10
|
+
let slug = headingSlugger.slug(textContent);
|
|
11
|
+
if (slug.endsWith("-"))
|
|
12
|
+
slug = slug.slice(0, -1);
|
|
13
|
+
return slug;
|
|
14
|
+
}
|
|
15
|
+
const heading = {
|
|
16
|
+
children: ["inline"],
|
|
17
|
+
attributes: {
|
|
18
|
+
id: { type: String },
|
|
19
|
+
level: { type: Number, required: true, default: 1 }
|
|
20
|
+
},
|
|
21
|
+
transform(node, config) {
|
|
22
|
+
var _a, _b;
|
|
23
|
+
const { level, ...attributes } = node.transformAttributes(config);
|
|
24
|
+
const children = node.transformChildren(config);
|
|
25
|
+
const slug = getSlug(attributes, children);
|
|
26
|
+
const render = ((_b = (_a = config.nodes) == null ? void 0 : _a.heading) == null ? void 0 : _b.render) ?? `h${level}`;
|
|
27
|
+
const tagProps = (
|
|
28
|
+
// For components, pass down `level` as a prop,
|
|
29
|
+
// alongside `__collectHeading` for our `headings` collector.
|
|
30
|
+
// Avoid accidentally rendering `level` as an HTML attribute otherwise!
|
|
31
|
+
typeof render === "function" ? { ...attributes, id: slug, __collectHeading: true, level } : { ...attributes, id: slug }
|
|
32
|
+
);
|
|
33
|
+
return new Markdoc.Tag(render, tagProps, children);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
export {
|
|
37
|
+
heading,
|
|
38
|
+
headingSlugger
|
|
39
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { MarkdownHeading } from '@astrojs/markdown-remark';
|
|
2
|
+
import { type ConfigType as MarkdocConfig, type RenderableTreeNode } from '@markdoc/markdoc';
|
|
3
|
+
import type { ContentEntryModule } from 'astro';
|
|
4
|
+
/** Used to reset Slugger cache on each build at runtime */
|
|
5
|
+
export { default as Markdoc } from '@markdoc/markdoc';
|
|
6
|
+
export { headingSlugger } from './nodes/index.js';
|
|
7
|
+
export declare function applyDefaultConfig(config: MarkdocConfig, entry: ContentEntryModule): MarkdocConfig;
|
|
8
|
+
/**
|
|
9
|
+
* Get text content as a string from a Markdoc transform AST
|
|
10
|
+
*/
|
|
11
|
+
export declare function getTextContent(childNodes: RenderableTreeNode[]): string;
|
|
12
|
+
/**
|
|
13
|
+
* Collect headings from Markdoc transform AST
|
|
14
|
+
* for `headings` result on `render()` return value
|
|
15
|
+
*/
|
|
16
|
+
export declare function collectHeadings(children: RenderableTreeNode[]): MarkdownHeading[];
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import Markdoc from "@markdoc/markdoc";
|
|
2
|
+
import { nodes as astroNodes } from "./nodes/index.js";
|
|
3
|
+
import { default as default2 } from "@markdoc/markdoc";
|
|
4
|
+
import { headingSlugger } from "./nodes/index.js";
|
|
5
|
+
function applyDefaultConfig(config, entry) {
|
|
6
|
+
return {
|
|
7
|
+
...config,
|
|
8
|
+
variables: {
|
|
9
|
+
entry,
|
|
10
|
+
...config.variables
|
|
11
|
+
},
|
|
12
|
+
nodes: {
|
|
13
|
+
...astroNodes,
|
|
14
|
+
...config.nodes
|
|
15
|
+
}
|
|
16
|
+
// TODO: Syntax highlighting
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function getTextContent(childNodes) {
|
|
20
|
+
let text = "";
|
|
21
|
+
for (const node of childNodes) {
|
|
22
|
+
if (typeof node === "string" || typeof node === "number") {
|
|
23
|
+
text += node;
|
|
24
|
+
} else if (typeof node === "object" && Markdoc.Tag.isTag(node)) {
|
|
25
|
+
text += getTextContent(node.children);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return text;
|
|
29
|
+
}
|
|
30
|
+
const headingLevels = [1, 2, 3, 4, 5, 6];
|
|
31
|
+
function collectHeadings(children) {
|
|
32
|
+
let collectedHeadings = [];
|
|
33
|
+
for (const node of children) {
|
|
34
|
+
if (typeof node !== "object" || !Markdoc.Tag.isTag(node))
|
|
35
|
+
continue;
|
|
36
|
+
if (node.attributes.__collectHeading === true && typeof node.attributes.level === "number") {
|
|
37
|
+
collectedHeadings.push({
|
|
38
|
+
slug: node.attributes.id,
|
|
39
|
+
depth: node.attributes.level,
|
|
40
|
+
text: getTextContent(node.children)
|
|
41
|
+
});
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
for (const level of headingLevels) {
|
|
45
|
+
if (node.name === "h" + level) {
|
|
46
|
+
collectedHeadings.push({
|
|
47
|
+
slug: node.attributes.id,
|
|
48
|
+
depth: level,
|
|
49
|
+
text: getTextContent(node.children)
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
collectedHeadings.concat(collectHeadings(node.children));
|
|
54
|
+
}
|
|
55
|
+
return collectedHeadings;
|
|
56
|
+
}
|
|
57
|
+
export {
|
|
58
|
+
default2 as Markdoc,
|
|
59
|
+
applyDefaultConfig,
|
|
60
|
+
collectHeadings,
|
|
61
|
+
getTextContent,
|
|
62
|
+
headingSlugger
|
|
63
|
+
};
|
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.
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"author": "withastro",
|
|
@@ -21,22 +21,29 @@
|
|
|
21
21
|
"exports": {
|
|
22
22
|
".": "./dist/index.js",
|
|
23
23
|
"./components": "./components/index.ts",
|
|
24
|
-
"./
|
|
24
|
+
"./runtime": "./dist/runtime.js",
|
|
25
25
|
"./config": "./dist/config.js",
|
|
26
26
|
"./experimental-assets-config": "./dist/experimental-assets-config.js",
|
|
27
27
|
"./package.json": "./package.json"
|
|
28
28
|
},
|
|
29
|
+
"files": [
|
|
30
|
+
"components",
|
|
31
|
+
"dist",
|
|
32
|
+
"template"
|
|
33
|
+
],
|
|
29
34
|
"dependencies": {
|
|
30
35
|
"@markdoc/markdoc": "^0.2.2",
|
|
31
36
|
"esbuild": "^0.17.12",
|
|
37
|
+
"github-slugger": "^2.0.0",
|
|
32
38
|
"gray-matter": "^4.0.3",
|
|
33
39
|
"kleur": "^4.1.5",
|
|
34
40
|
"zod": "^3.17.3"
|
|
35
41
|
},
|
|
36
42
|
"peerDependencies": {
|
|
37
|
-
"astro": "^2.
|
|
43
|
+
"astro": "^2.5.0"
|
|
38
44
|
},
|
|
39
45
|
"devDependencies": {
|
|
46
|
+
"@astrojs/markdown-remark": "^2.2.1",
|
|
40
47
|
"@types/chai": "^4.3.1",
|
|
41
48
|
"@types/html-escaper": "^3.0.0",
|
|
42
49
|
"@types/mocha": "^9.1.1",
|
|
@@ -46,7 +53,7 @@
|
|
|
46
53
|
"mocha": "^9.2.2",
|
|
47
54
|
"rollup": "^3.20.1",
|
|
48
55
|
"vite": "^4.3.1",
|
|
49
|
-
"astro": "2.
|
|
56
|
+
"astro": "2.5.0",
|
|
50
57
|
"astro-scripts": "0.0.14"
|
|
51
58
|
},
|
|
52
59
|
"engines": {
|
package/.turbo/turbo-build.log
DELETED
package/CHANGELOG.md
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
# @astrojs/markdoc
|
|
2
|
-
|
|
3
|
-
## 0.1.2
|
|
4
|
-
|
|
5
|
-
### Patch Changes
|
|
6
|
-
|
|
7
|
-
- [#6932](https://github.com/withastro/astro/pull/6932) [`49514e4ce`](https://github.com/withastro/astro/commit/49514e4ce40fedb39bf7decd2c296258efbdafc7) Thanks [@bluwy](https://github.com/bluwy)! - Upgrade shiki to v0.14.1. This updates the shiki theme colors and adds the theme name to the `pre` tag, e.g. `<pre class="astro-code github-dark">`.
|
|
8
|
-
|
|
9
|
-
- Updated dependencies [[`818252acd`](https://github.com/withastro/astro/commit/818252acda3c00499cea51ffa0f26d4c2ccd3a02), [`80e3d4d3d`](https://github.com/withastro/astro/commit/80e3d4d3d0f7719d8eae5435bba3805503057511), [`3326492b9`](https://github.com/withastro/astro/commit/3326492b94f76ed2b0154dd9b9a1a9eb883c1e31), [`cac4a321e`](https://github.com/withastro/astro/commit/cac4a321e814fb805eb0e3ced469e25261a50885), [`831b67cdb`](https://github.com/withastro/astro/commit/831b67cdb8250f93f66e3b171fab024652bf80f2), [`49514e4ce`](https://github.com/withastro/astro/commit/49514e4ce40fedb39bf7decd2c296258efbdafc7), [`0883fd487`](https://github.com/withastro/astro/commit/0883fd4875548a613df122f0b87a1ca8b7a7cf7d)]:
|
|
10
|
-
- astro@2.4.0
|
|
11
|
-
|
|
12
|
-
## 0.1.1
|
|
13
|
-
|
|
14
|
-
### Patch Changes
|
|
15
|
-
|
|
16
|
-
- [#6723](https://github.com/withastro/astro/pull/6723) [`73fcc7627`](https://github.com/withastro/astro/commit/73fcc7627e27a001d3ed2f4d046999d91f1aef85) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Fix: when using `render: null` in your config, content is now rendered without a wrapper element.
|
|
17
|
-
|
|
18
|
-
- Updated dependencies [[`489dd8d69`](https://github.com/withastro/astro/commit/489dd8d69cdd9d7c243cf8bec96051a914984b9c), [`a1a4f45b5`](https://github.com/withastro/astro/commit/a1a4f45b51a80215fa7598da83bd0d9c5acd20d2), [`a1108e037`](https://github.com/withastro/astro/commit/a1108e037115cdb67d03505286c7d3a4fc2a1ff5), [`8b88e4cf1`](https://github.com/withastro/astro/commit/8b88e4cf15c8bea7942b3985380164e0edf7250b), [`d54cbe413`](https://github.com/withastro/astro/commit/d54cbe41349e55f8544212ad9320705f07325920), [`4c347ab51`](https://github.com/withastro/astro/commit/4c347ab51e46f2319d614f8577fe502e3dc816e2), [`ff0430786`](https://github.com/withastro/astro/commit/ff043078630e678348ae4f4757b3015b3b862c16), [`2f2e572e9`](https://github.com/withastro/astro/commit/2f2e572e937fd25451bbc78a05d55b7caa1ca3ec), [`7116c021a`](https://github.com/withastro/astro/commit/7116c021a39eac15a6e1264dfbd11bef0f5d618a)]:
|
|
19
|
-
- astro@2.2.0
|
|
20
|
-
|
|
21
|
-
## 0.1.0
|
|
22
|
-
|
|
23
|
-
### Minor Changes
|
|
24
|
-
|
|
25
|
-
- [#6653](https://github.com/withastro/astro/pull/6653) [`7c439868a`](https://github.com/withastro/astro/commit/7c439868a3bc7d466418da9af669966014f3d9fe) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Simplify Markdoc configuration with a new `markdoc.config.mjs` file. This lets you import Astro components directly to render as Markdoc tags and nodes, without the need for the previous `components` property. This new configuration also unlocks passing variables to your Markdoc from the `Content` component ([see the new docs](https://docs.astro.build/en/guides/integrations-guide/markdoc/#pass-markdoc-variables)).
|
|
26
|
-
|
|
27
|
-
## Migration
|
|
28
|
-
|
|
29
|
-
Move any existing Markdoc config from your `astro.config` to a new `markdoc.config.mjs` file at the root of your project. This should be applied as a default export, with the optional `defineMarkdocConfig()` helper for autocomplete in your editor.
|
|
30
|
-
|
|
31
|
-
This example configures an `aside` Markdoc tag. Note that components should be imported and applied to the `render` attribute _directly,_ instead of passing the name as a string:
|
|
32
|
-
|
|
33
|
-
```js
|
|
34
|
-
// markdoc.config.mjs
|
|
35
|
-
import { defineMarkdocConfig } from '@astrojs/markdoc/config';
|
|
36
|
-
import Aside from './src/components/Aside.astro';
|
|
37
|
-
|
|
38
|
-
export default defineMarkdocConfig({
|
|
39
|
-
tags: {
|
|
40
|
-
aside: {
|
|
41
|
-
render: Aside,
|
|
42
|
-
},
|
|
43
|
-
},
|
|
44
|
-
});
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
You should also remove the `components` prop from your `Content` components. Since components are imported into your config directly, this is no longer needed.
|
|
48
|
-
|
|
49
|
-
```diff
|
|
50
|
-
---
|
|
51
|
-
- import Aside from '../components/Aside.astro';
|
|
52
|
-
import { getEntryBySlug } from 'astro:content';
|
|
53
|
-
|
|
54
|
-
const entry = await getEntryBySlug('docs', 'why-markdoc');
|
|
55
|
-
const { Content } = await entry.render();
|
|
56
|
-
---
|
|
57
|
-
|
|
58
|
-
<Content
|
|
59
|
-
- components={{ Aside }}
|
|
60
|
-
/>
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
### Patch Changes
|
|
64
|
-
|
|
65
|
-
- Updated dependencies [[`1f783e320`](https://github.com/withastro/astro/commit/1f783e32075c20b13063599696644f5d47b75d8d), [`2e92e9aa9`](https://github.com/withastro/astro/commit/2e92e9aa976735c3ddb647152bb9c4850136e386), [`adecda7d6`](https://github.com/withastro/astro/commit/adecda7d6009793c5d20519a997e3b7afb08ad57), [`386336441`](https://github.com/withastro/astro/commit/386336441ad70017eea22db0683591126131db21), [`7c439868a`](https://github.com/withastro/astro/commit/7c439868a3bc7d466418da9af669966014f3d9fe), [`25cd3e574`](https://github.com/withastro/astro/commit/25cd3e574999c1c7294a089ad8c39df27ccdbf17), [`4bf87c64f`](https://github.com/withastro/astro/commit/4bf87c64ff7e9ca49e0f5c27e06bd49faaf60542), [`fc0ed9c53`](https://github.com/withastro/astro/commit/fc0ed9c53cd374860bbdb2503318a55ca09a2662)]:
|
|
66
|
-
- astro@2.1.8
|
|
67
|
-
|
|
68
|
-
## 0.0.5
|
|
69
|
-
|
|
70
|
-
### Patch Changes
|
|
71
|
-
|
|
72
|
-
- [#6630](https://github.com/withastro/astro/pull/6630) [`cfcf2e2ff`](https://github.com/withastro/astro/commit/cfcf2e2ffdaa68ace5c84329c05b83559a29d638) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Support automatic image optimization for Markdoc images when using `experimental.assets`. You can [follow our Assets guide](https://docs.astro.build/en/guides/assets/#enabling-assets-in-your-project) to enable this feature in your project. Then, start using relative or aliased image sources in your Markdoc files for automatic optimization:
|
|
73
|
-
|
|
74
|
-
```md
|
|
75
|
-
<!--Relative paths-->
|
|
76
|
-
|
|
77
|
-

|
|
78
|
-
|
|
79
|
-
<!--Or configured aliases-->
|
|
80
|
-
|
|
81
|
-

|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
- Updated dependencies [[`b7194103e`](https://github.com/withastro/astro/commit/b7194103e39267bf59dcd6ba00f522e424219d16), [`cfcf2e2ff`](https://github.com/withastro/astro/commit/cfcf2e2ffdaa68ace5c84329c05b83559a29d638), [`45da39a86`](https://github.com/withastro/astro/commit/45da39a8642d64eb318840b18dfc2b5ccc6561bc), [`7daef9a29`](https://github.com/withastro/astro/commit/7daef9a2993b5d457f3d243a1ebfd1dd383b3327)]:
|
|
85
|
-
- astro@2.1.7
|
|
86
|
-
|
|
87
|
-
## 0.0.4
|
|
88
|
-
|
|
89
|
-
### Patch Changes
|
|
90
|
-
|
|
91
|
-
- [#6588](https://github.com/withastro/astro/pull/6588) [`f42f47dc6`](https://github.com/withastro/astro/commit/f42f47dc6a91cdb6534dab0ecbf9e8e85f00ba40) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Allow access to content collection entry information (including parsed frontmatter and the entry slug) from your Markdoc using the `$entry` variable:
|
|
92
|
-
|
|
93
|
-
```mdx
|
|
94
|
-
---
|
|
95
|
-
title: Hello Markdoc!
|
|
96
|
-
---
|
|
97
|
-
|
|
98
|
-
# {% $entry.data.title %}
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
- [#6607](https://github.com/withastro/astro/pull/6607) [`86273b588`](https://github.com/withastro/astro/commit/86273b5881cc61ebee11d40280b4c0aba8f4bb2e) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Fix: Update Markdoc renderer internals to remove unneeded dependencies
|
|
102
|
-
|
|
103
|
-
- [#6622](https://github.com/withastro/astro/pull/6622) [`b37b86540`](https://github.com/withastro/astro/commit/b37b865400e77e92878d7e150244acce47e933c6) Thanks [@paulrudy](https://github.com/paulrudy)! - Fix README instructions for installing Markdoc manually.
|
|
104
|
-
|
|
105
|
-
## 0.0.3
|
|
106
|
-
|
|
107
|
-
### Patch Changes
|
|
108
|
-
|
|
109
|
-
- [#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
|
|
110
|
-
|
|
111
|
-
## 0.0.2
|
|
112
|
-
|
|
113
|
-
### Patch Changes
|
|
114
|
-
|
|
115
|
-
- [#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
|
|
116
|
-
|
|
117
|
-
## 0.0.1
|
|
118
|
-
|
|
119
|
-
### Patch Changes
|
|
120
|
-
|
|
121
|
-
- [#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.
|
|
122
|
-
|
|
123
|
-
You can install this integration using the `astro add` command:
|
|
124
|
-
|
|
125
|
-
```
|
|
126
|
-
astro add markdoc
|
|
127
|
-
```
|
|
128
|
-
|
|
129
|
-
[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/dist/default-config.d.ts
DELETED
package/dist/default-config.js
DELETED
package/src/config.ts
DELETED
package/src/default-config.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { ConfigType as MarkdocConfig } from '@markdoc/markdoc';
|
|
2
|
-
import type { ContentEntryModule } from 'astro';
|
|
3
|
-
|
|
4
|
-
export function applyDefaultConfig(
|
|
5
|
-
config: MarkdocConfig,
|
|
6
|
-
ctx: {
|
|
7
|
-
entry: ContentEntryModule;
|
|
8
|
-
}
|
|
9
|
-
): MarkdocConfig {
|
|
10
|
-
return {
|
|
11
|
-
...config,
|
|
12
|
-
variables: {
|
|
13
|
-
entry: ctx.entry,
|
|
14
|
-
...config.variables,
|
|
15
|
-
},
|
|
16
|
-
// TODO: heading ID calculation, Shiki syntax highlighting
|
|
17
|
-
};
|
|
18
|
-
}
|