fumadocs-mdx 11.8.1 → 11.8.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/dist/{build-mdx-DMe0r3s_.d.cts → build-mdx-DnC1jKvn.d.cts} +6 -4
- package/dist/{build-mdx-DMe0r3s_.d.ts → build-mdx-DnC1jKvn.d.ts} +6 -4
- package/dist/{chunk-BCOXAJMC.js → chunk-BWRDVK5L.js} +1 -1
- package/dist/{chunk-7JFPDRW7.js → chunk-QQWCBFFE.js} +2 -4
- package/dist/{chunk-S73DGUHI.js → chunk-QVZ7JH4H.js} +1 -1
- package/dist/{chunk-DJNS22RO.js → chunk-SMSNZ6N5.js} +37 -23
- package/dist/chunk-SVTXMVLQ.js +139 -0
- package/dist/config/index.cjs +118 -84
- package/dist/config/index.d.cts +5 -1
- package/dist/config/index.d.ts +5 -1
- package/dist/config/index.js +2 -2
- package/dist/config/zod-3.cjs +118 -84
- package/dist/config/zod-3.d.cts +1 -1
- package/dist/config/zod-3.d.ts +1 -1
- package/dist/config/zod-3.js +2 -2
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/loader-mdx.cjs +127 -102
- package/dist/loader-mdx.js +8 -8
- package/dist/{mdx-options-ILHLYYTL.js → mdx-options-T73E4LQB.js} +1 -1
- package/dist/next/index.cjs +41 -34
- package/dist/next/index.js +2 -2
- package/dist/runtime/async.cjs +121 -96
- package/dist/runtime/async.d.cts +2 -2
- package/dist/runtime/async.d.ts +2 -2
- package/dist/runtime/async.js +3 -3
- package/dist/runtime/vite.d.cts +1 -1
- package/dist/runtime/vite.d.ts +1 -1
- package/dist/{types-DvnkeVI3.d.cts → types-BmVgoqsr.d.cts} +1 -1
- package/dist/{types-zBRxlyaM.d.ts → types-WSHJKA8L.d.ts} +1 -1
- package/dist/vite/index.cjs +123 -98
- package/dist/vite/index.js +3 -3
- package/package.json +9 -7
- package/dist/chunk-IGXZS2W6.js +0 -119
|
@@ -32,12 +32,14 @@ interface CompiledMDXProperties<Frontmatter = Record<string, unknown>> {
|
|
|
32
32
|
declare module 'vfile' {
|
|
33
33
|
interface DataMap {
|
|
34
34
|
/**
|
|
35
|
-
*
|
|
35
|
+
* [Fumadocs MDX] raw frontmatter, you can modify it
|
|
36
|
+
*/
|
|
37
|
+
frontmatter?: Record<string, unknown>;
|
|
38
|
+
/**
|
|
39
|
+
* [Fumadocs MDX] The compiler object from loader
|
|
36
40
|
*/
|
|
37
41
|
_compiler?: CompilerOptions;
|
|
38
|
-
|
|
39
|
-
getProcessor: (format: 'md' | 'mdx') => Processor;
|
|
40
|
-
};
|
|
42
|
+
_getProcessor?: (format: 'md' | 'mdx') => Processor;
|
|
41
43
|
}
|
|
42
44
|
}
|
|
43
45
|
|
|
@@ -32,12 +32,14 @@ interface CompiledMDXProperties<Frontmatter = Record<string, unknown>> {
|
|
|
32
32
|
declare module 'vfile' {
|
|
33
33
|
interface DataMap {
|
|
34
34
|
/**
|
|
35
|
-
*
|
|
35
|
+
* [Fumadocs MDX] raw frontmatter, you can modify it
|
|
36
|
+
*/
|
|
37
|
+
frontmatter?: Record<string, unknown>;
|
|
38
|
+
/**
|
|
39
|
+
* [Fumadocs MDX] The compiler object from loader
|
|
36
40
|
*/
|
|
37
41
|
_compiler?: CompilerOptions;
|
|
38
|
-
|
|
39
|
-
getProcessor: (format: 'md' | 'mdx') => Processor;
|
|
40
|
-
};
|
|
42
|
+
_getProcessor?: (format: 'md' | 'mdx') => Processor;
|
|
41
43
|
}
|
|
42
44
|
}
|
|
43
45
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
remarkInclude
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-SVTXMVLQ.js";
|
|
4
4
|
|
|
5
5
|
// src/utils/build-mdx.ts
|
|
6
6
|
import { createProcessor } from "@mdx-js/mdx";
|
|
@@ -30,9 +30,7 @@ async function buildMDX(cacheKey, source, options) {
|
|
|
30
30
|
...data,
|
|
31
31
|
frontmatter,
|
|
32
32
|
_compiler,
|
|
33
|
-
|
|
34
|
-
getProcessor
|
|
35
|
-
}
|
|
33
|
+
_getProcessor: getProcessor
|
|
36
34
|
}
|
|
37
35
|
});
|
|
38
36
|
}
|
|
@@ -34,7 +34,7 @@ function buildConfig(config) {
|
|
|
34
34
|
const input = this.global.mdxOptions;
|
|
35
35
|
async function uncached() {
|
|
36
36
|
const options = typeof input === "function" ? await input() : input;
|
|
37
|
-
const { getDefaultMDXOptions } = await import("./mdx-options-
|
|
37
|
+
const { getDefaultMDXOptions } = await import("./mdx-options-T73E4LQB.js");
|
|
38
38
|
if (options?.preset === "minimal") return options;
|
|
39
39
|
return getDefaultMDXOptions({
|
|
40
40
|
...options,
|
|
@@ -1,16 +1,42 @@
|
|
|
1
1
|
// src/utils/mdx-options.ts
|
|
2
2
|
import * as plugins from "fumadocs-core/mdx-plugins";
|
|
3
3
|
|
|
4
|
-
// src/mdx-plugins/remark-
|
|
4
|
+
// src/mdx-plugins/remark-postprocess.ts
|
|
5
|
+
import { visit } from "unist-util-visit";
|
|
5
6
|
import { valueToEstree } from "estree-util-value-to-estree";
|
|
6
|
-
function
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
function remarkPostprocess({
|
|
8
|
+
injectExports
|
|
9
|
+
}) {
|
|
10
|
+
return (tree, file) => {
|
|
11
|
+
let title;
|
|
12
|
+
const urls = [];
|
|
13
|
+
visit(tree, ["heading", "link"], (node) => {
|
|
14
|
+
if (node.type === "heading" && node.depth === 1) {
|
|
15
|
+
title = flattenNode(node);
|
|
16
|
+
}
|
|
17
|
+
if (node.type !== "link") return;
|
|
18
|
+
urls.push({
|
|
19
|
+
href: node.url
|
|
20
|
+
});
|
|
21
|
+
return "skip";
|
|
22
|
+
});
|
|
23
|
+
if (title) {
|
|
24
|
+
file.data.frontmatter ??= {};
|
|
25
|
+
if (!file.data.frontmatter.title) file.data.frontmatter.title = title;
|
|
26
|
+
}
|
|
27
|
+
file.data.extractedReferences = urls;
|
|
28
|
+
for (const name of injectExports) {
|
|
29
|
+
if (!(name in file.data)) continue;
|
|
30
|
+
tree.children.unshift(getMdastExport(name, file.data[name]));
|
|
11
31
|
}
|
|
12
32
|
};
|
|
13
33
|
}
|
|
34
|
+
function flattenNode(node) {
|
|
35
|
+
if ("children" in node)
|
|
36
|
+
return node.children.map((child) => flattenNode(child)).join("");
|
|
37
|
+
if ("value" in node) return node.value;
|
|
38
|
+
return "";
|
|
39
|
+
}
|
|
14
40
|
function getMdastExport(name, value) {
|
|
15
41
|
return {
|
|
16
42
|
type: "mdxjsEsm",
|
|
@@ -22,6 +48,7 @@ function getMdastExport(name, value) {
|
|
|
22
48
|
body: [
|
|
23
49
|
{
|
|
24
50
|
type: "ExportNamedDeclaration",
|
|
51
|
+
attributes: [],
|
|
25
52
|
specifiers: [],
|
|
26
53
|
source: null,
|
|
27
54
|
declaration: {
|
|
@@ -45,21 +72,6 @@ function getMdastExport(name, value) {
|
|
|
45
72
|
};
|
|
46
73
|
}
|
|
47
74
|
|
|
48
|
-
// src/mdx-plugins/remark-extract.ts
|
|
49
|
-
import { visit } from "unist-util-visit";
|
|
50
|
-
function remarkExtract() {
|
|
51
|
-
return (tree, file) => {
|
|
52
|
-
const urls = [];
|
|
53
|
-
visit(tree, "link", (node) => {
|
|
54
|
-
urls.push({
|
|
55
|
-
href: node.url
|
|
56
|
-
});
|
|
57
|
-
return "skip";
|
|
58
|
-
});
|
|
59
|
-
file.data.extractedReferences = urls;
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
|
|
63
75
|
// src/utils/mdx-options.ts
|
|
64
76
|
function pluginOption(def, options = []) {
|
|
65
77
|
const list = def(Array.isArray(options) ? options : []).filter(
|
|
@@ -111,12 +123,14 @@ function getDefaultMDXOptions({
|
|
|
111
123
|
],
|
|
112
124
|
"remarkNpm" in plugins && remarkNpmOptions !== false && [plugins.remarkNpm, remarkNpmOptions],
|
|
113
125
|
...v,
|
|
114
|
-
remarkExtract,
|
|
115
126
|
remarkStructureOptions !== false && [
|
|
116
127
|
plugins.remarkStructure,
|
|
117
128
|
remarkStructureOptions
|
|
118
129
|
],
|
|
119
|
-
[
|
|
130
|
+
[
|
|
131
|
+
remarkPostprocess,
|
|
132
|
+
{ injectExports: mdxExports }
|
|
133
|
+
]
|
|
120
134
|
],
|
|
121
135
|
mdxOptions.remarkPlugins
|
|
122
136
|
);
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import {
|
|
2
|
+
fumaMatter
|
|
3
|
+
} from "./chunk-VWJKRQZR.js";
|
|
4
|
+
|
|
5
|
+
// src/mdx-plugins/remark-include.ts
|
|
6
|
+
import { unified } from "unified";
|
|
7
|
+
import { visit } from "unist-util-visit";
|
|
8
|
+
import * as path from "path";
|
|
9
|
+
import * as fs from "fs/promises";
|
|
10
|
+
import remarkParse from "remark-parse";
|
|
11
|
+
import remarkMdx from "remark-mdx";
|
|
12
|
+
import { remarkHeading } from "fumadocs-core/mdx-plugins";
|
|
13
|
+
var baseProcessor = unified().use(remarkHeading);
|
|
14
|
+
function flattenNode(node) {
|
|
15
|
+
if ("children" in node)
|
|
16
|
+
return node.children.map((child) => flattenNode(child)).join("");
|
|
17
|
+
if ("value" in node) return node.value;
|
|
18
|
+
return "";
|
|
19
|
+
}
|
|
20
|
+
function parseSpecifier(specifier) {
|
|
21
|
+
const idx = specifier.lastIndexOf("#");
|
|
22
|
+
if (idx === -1) return { file: specifier };
|
|
23
|
+
return {
|
|
24
|
+
file: specifier.slice(0, idx),
|
|
25
|
+
section: specifier.slice(idx + 1)
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function extractSection(root, section) {
|
|
29
|
+
let nodes;
|
|
30
|
+
for (const node of root.children) {
|
|
31
|
+
if (node.type === "mdxJsxFlowElement" && node.name === "section" && node.attributes.some(
|
|
32
|
+
(attr) => attr.type === "mdxJsxAttribute" && attr.name === "id" && attr.value === section
|
|
33
|
+
)) {
|
|
34
|
+
nodes = node.children;
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
if (node.type === "heading" && node.data?.hProperties?.id === section) {
|
|
38
|
+
nodes = [node];
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (!nodes) continue;
|
|
42
|
+
if (node.type === "heading") break;
|
|
43
|
+
nodes.push(node);
|
|
44
|
+
}
|
|
45
|
+
if (nodes)
|
|
46
|
+
return {
|
|
47
|
+
type: "root",
|
|
48
|
+
children: nodes
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function remarkInclude() {
|
|
52
|
+
const TagName = "include";
|
|
53
|
+
async function embedContent(file, heading, params, data) {
|
|
54
|
+
let content;
|
|
55
|
+
try {
|
|
56
|
+
content = (await fs.readFile(file)).toString();
|
|
57
|
+
} catch (e) {
|
|
58
|
+
throw new Error(
|
|
59
|
+
`failed to read file ${file}
|
|
60
|
+
${e instanceof Error ? e.message : String(e)}`,
|
|
61
|
+
{ cause: e }
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
const ext = path.extname(file);
|
|
65
|
+
data._compiler?.addDependency(file);
|
|
66
|
+
if (params.lang || ext !== ".md" && ext !== ".mdx") {
|
|
67
|
+
const lang = params.lang ?? ext.slice(1);
|
|
68
|
+
return {
|
|
69
|
+
type: "code",
|
|
70
|
+
lang,
|
|
71
|
+
meta: params.meta,
|
|
72
|
+
value: content,
|
|
73
|
+
data: {}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
const processor = (data._getProcessor ?? getDefaultProcessor)(
|
|
77
|
+
ext === ".mdx" ? "mdx" : "md"
|
|
78
|
+
);
|
|
79
|
+
let parsed = await baseProcessor.run(
|
|
80
|
+
processor.parse(fumaMatter(content).content)
|
|
81
|
+
);
|
|
82
|
+
if (heading) {
|
|
83
|
+
const extracted = extractSection(parsed, heading);
|
|
84
|
+
if (!extracted)
|
|
85
|
+
throw new Error(
|
|
86
|
+
`Cannot find section ${heading} in ${file}, make sure you have encapsulated the section in a <section id="${heading}"> tag.`
|
|
87
|
+
);
|
|
88
|
+
parsed = extracted;
|
|
89
|
+
}
|
|
90
|
+
await update(parsed, path.dirname(file), data);
|
|
91
|
+
return parsed;
|
|
92
|
+
}
|
|
93
|
+
async function update(tree, directory, data) {
|
|
94
|
+
const queue = [];
|
|
95
|
+
visit(
|
|
96
|
+
tree,
|
|
97
|
+
["mdxJsxFlowElement", "mdxJsxTextElement"],
|
|
98
|
+
(_node, _, parent) => {
|
|
99
|
+
const node = _node;
|
|
100
|
+
if (node.name !== TagName) return;
|
|
101
|
+
const params = {};
|
|
102
|
+
const specifier = flattenNode(node);
|
|
103
|
+
if (specifier.length === 0) return "skip";
|
|
104
|
+
for (const attr of node.attributes) {
|
|
105
|
+
if (attr.type === "mdxJsxAttribute" && (typeof attr.value === "string" || attr.value === null)) {
|
|
106
|
+
params[attr.name] = attr.value;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const { file: relativePath, section } = parseSpecifier(specifier);
|
|
110
|
+
const file = path.resolve(
|
|
111
|
+
"cwd" in params ? process.cwd() : directory,
|
|
112
|
+
relativePath
|
|
113
|
+
);
|
|
114
|
+
queue.push(
|
|
115
|
+
embedContent(file, section, params, data).then((replace) => {
|
|
116
|
+
Object.assign(
|
|
117
|
+
parent && parent.type === "paragraph" ? parent : node,
|
|
118
|
+
replace
|
|
119
|
+
);
|
|
120
|
+
})
|
|
121
|
+
);
|
|
122
|
+
return "skip";
|
|
123
|
+
}
|
|
124
|
+
);
|
|
125
|
+
await Promise.all(queue);
|
|
126
|
+
}
|
|
127
|
+
return async (tree, file) => {
|
|
128
|
+
await update(tree, path.dirname(file.path), file.data);
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
function getDefaultProcessor(format) {
|
|
132
|
+
const mdProcessor = unified().use(remarkParse);
|
|
133
|
+
if (format === "md") return mdProcessor;
|
|
134
|
+
return mdProcessor.use(remarkMdx);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export {
|
|
138
|
+
remarkInclude
|
|
139
|
+
};
|
package/dist/config/index.cjs
CHANGED
|
@@ -93,16 +93,42 @@ function defineConfig(config = {}) {
|
|
|
93
93
|
// src/utils/mdx-options.ts
|
|
94
94
|
var plugins = __toESM(require("fumadocs-core/mdx-plugins"), 1);
|
|
95
95
|
|
|
96
|
-
// src/mdx-plugins/remark-
|
|
96
|
+
// src/mdx-plugins/remark-postprocess.ts
|
|
97
|
+
var import_unist_util_visit = require("unist-util-visit");
|
|
97
98
|
var import_estree_util_value_to_estree = require("estree-util-value-to-estree");
|
|
98
|
-
function
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
99
|
+
function remarkPostprocess({
|
|
100
|
+
injectExports
|
|
101
|
+
}) {
|
|
102
|
+
return (tree, file) => {
|
|
103
|
+
let title;
|
|
104
|
+
const urls = [];
|
|
105
|
+
(0, import_unist_util_visit.visit)(tree, ["heading", "link"], (node) => {
|
|
106
|
+
if (node.type === "heading" && node.depth === 1) {
|
|
107
|
+
title = flattenNode(node);
|
|
108
|
+
}
|
|
109
|
+
if (node.type !== "link") return;
|
|
110
|
+
urls.push({
|
|
111
|
+
href: node.url
|
|
112
|
+
});
|
|
113
|
+
return "skip";
|
|
114
|
+
});
|
|
115
|
+
if (title) {
|
|
116
|
+
file.data.frontmatter ??= {};
|
|
117
|
+
if (!file.data.frontmatter.title) file.data.frontmatter.title = title;
|
|
118
|
+
}
|
|
119
|
+
file.data.extractedReferences = urls;
|
|
120
|
+
for (const name of injectExports) {
|
|
121
|
+
if (!(name in file.data)) continue;
|
|
122
|
+
tree.children.unshift(getMdastExport(name, file.data[name]));
|
|
103
123
|
}
|
|
104
124
|
};
|
|
105
125
|
}
|
|
126
|
+
function flattenNode(node) {
|
|
127
|
+
if ("children" in node)
|
|
128
|
+
return node.children.map((child) => flattenNode(child)).join("");
|
|
129
|
+
if ("value" in node) return node.value;
|
|
130
|
+
return "";
|
|
131
|
+
}
|
|
106
132
|
function getMdastExport(name, value) {
|
|
107
133
|
return {
|
|
108
134
|
type: "mdxjsEsm",
|
|
@@ -114,6 +140,7 @@ function getMdastExport(name, value) {
|
|
|
114
140
|
body: [
|
|
115
141
|
{
|
|
116
142
|
type: "ExportNamedDeclaration",
|
|
143
|
+
attributes: [],
|
|
117
144
|
specifiers: [],
|
|
118
145
|
source: null,
|
|
119
146
|
declaration: {
|
|
@@ -137,21 +164,6 @@ function getMdastExport(name, value) {
|
|
|
137
164
|
};
|
|
138
165
|
}
|
|
139
166
|
|
|
140
|
-
// src/mdx-plugins/remark-extract.ts
|
|
141
|
-
var import_unist_util_visit = require("unist-util-visit");
|
|
142
|
-
function remarkExtract() {
|
|
143
|
-
return (tree, file) => {
|
|
144
|
-
const urls = [];
|
|
145
|
-
(0, import_unist_util_visit.visit)(tree, "link", (node) => {
|
|
146
|
-
urls.push({
|
|
147
|
-
href: node.url
|
|
148
|
-
});
|
|
149
|
-
return "skip";
|
|
150
|
-
});
|
|
151
|
-
file.data.extractedReferences = urls;
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
|
|
155
167
|
// src/utils/mdx-options.ts
|
|
156
168
|
function pluginOption(def, options = []) {
|
|
157
169
|
const list = def(Array.isArray(options) ? options : []).filter(
|
|
@@ -203,12 +215,14 @@ function getDefaultMDXOptions({
|
|
|
203
215
|
],
|
|
204
216
|
"remarkNpm" in plugins && remarkNpmOptions !== false && [plugins.remarkNpm, remarkNpmOptions],
|
|
205
217
|
...v,
|
|
206
|
-
remarkExtract,
|
|
207
218
|
remarkStructureOptions !== false && [
|
|
208
219
|
plugins.remarkStructure,
|
|
209
220
|
remarkStructureOptions
|
|
210
221
|
],
|
|
211
|
-
[
|
|
222
|
+
[
|
|
223
|
+
remarkPostprocess,
|
|
224
|
+
{ injectExports: mdxExports }
|
|
225
|
+
]
|
|
212
226
|
],
|
|
213
227
|
mdxOptions.remarkPlugins
|
|
214
228
|
);
|
|
@@ -229,6 +243,7 @@ function getDefaultMDXOptions({
|
|
|
229
243
|
}
|
|
230
244
|
|
|
231
245
|
// src/mdx-plugins/remark-include.ts
|
|
246
|
+
var import_unified = require("unified");
|
|
232
247
|
var import_unist_util_visit2 = require("unist-util-visit");
|
|
233
248
|
var path = __toESM(require("path"), 1);
|
|
234
249
|
var fs = __toESM(require("fs/promises"), 1);
|
|
@@ -250,9 +265,13 @@ function fumaMatter(input) {
|
|
|
250
265
|
}
|
|
251
266
|
|
|
252
267
|
// src/mdx-plugins/remark-include.ts
|
|
253
|
-
|
|
268
|
+
var import_remark_parse = __toESM(require("remark-parse"), 1);
|
|
269
|
+
var import_remark_mdx = __toESM(require("remark-mdx"), 1);
|
|
270
|
+
var import_mdx_plugins = require("fumadocs-core/mdx-plugins");
|
|
271
|
+
var baseProcessor = (0, import_unified.unified)().use(import_mdx_plugins.remarkHeading);
|
|
272
|
+
function flattenNode2(node) {
|
|
254
273
|
if ("children" in node)
|
|
255
|
-
return node.children.map((child) =>
|
|
274
|
+
return node.children.map((child) => flattenNode2(child)).join("");
|
|
256
275
|
if ("value" in node) return node.value;
|
|
257
276
|
return "";
|
|
258
277
|
}
|
|
@@ -265,86 +284,96 @@ function parseSpecifier(specifier) {
|
|
|
265
284
|
};
|
|
266
285
|
}
|
|
267
286
|
function extractSection(root, section) {
|
|
287
|
+
let nodes;
|
|
268
288
|
for (const node of root.children) {
|
|
269
289
|
if (node.type === "mdxJsxFlowElement" && node.name === "section" && node.attributes.some(
|
|
270
290
|
(attr) => attr.type === "mdxJsxAttribute" && attr.name === "id" && attr.value === section
|
|
271
291
|
)) {
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
292
|
+
nodes = node.children;
|
|
293
|
+
break;
|
|
294
|
+
}
|
|
295
|
+
if (node.type === "heading" && node.data?.hProperties?.id === section) {
|
|
296
|
+
nodes = [node];
|
|
297
|
+
continue;
|
|
276
298
|
}
|
|
299
|
+
if (!nodes) continue;
|
|
300
|
+
if (node.type === "heading") break;
|
|
301
|
+
nodes.push(node);
|
|
277
302
|
}
|
|
303
|
+
if (nodes)
|
|
304
|
+
return {
|
|
305
|
+
type: "root",
|
|
306
|
+
children: nodes
|
|
307
|
+
};
|
|
278
308
|
}
|
|
279
309
|
function remarkInclude() {
|
|
280
310
|
const TagName = "include";
|
|
311
|
+
async function embedContent(file, heading, params, data) {
|
|
312
|
+
let content;
|
|
313
|
+
try {
|
|
314
|
+
content = (await fs.readFile(file)).toString();
|
|
315
|
+
} catch (e) {
|
|
316
|
+
throw new Error(
|
|
317
|
+
`failed to read file ${file}
|
|
318
|
+
${e instanceof Error ? e.message : String(e)}`,
|
|
319
|
+
{ cause: e }
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
const ext = path.extname(file);
|
|
323
|
+
data._compiler?.addDependency(file);
|
|
324
|
+
if (params.lang || ext !== ".md" && ext !== ".mdx") {
|
|
325
|
+
const lang = params.lang ?? ext.slice(1);
|
|
326
|
+
return {
|
|
327
|
+
type: "code",
|
|
328
|
+
lang,
|
|
329
|
+
meta: params.meta,
|
|
330
|
+
value: content,
|
|
331
|
+
data: {}
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
const processor = (data._getProcessor ?? getDefaultProcessor)(
|
|
335
|
+
ext === ".mdx" ? "mdx" : "md"
|
|
336
|
+
);
|
|
337
|
+
let parsed = await baseProcessor.run(
|
|
338
|
+
processor.parse(fumaMatter(content).content)
|
|
339
|
+
);
|
|
340
|
+
if (heading) {
|
|
341
|
+
const extracted = extractSection(parsed, heading);
|
|
342
|
+
if (!extracted)
|
|
343
|
+
throw new Error(
|
|
344
|
+
`Cannot find section ${heading} in ${file}, make sure you have encapsulated the section in a <section id="${heading}"> tag.`
|
|
345
|
+
);
|
|
346
|
+
parsed = extracted;
|
|
347
|
+
}
|
|
348
|
+
await update(parsed, path.dirname(file), data);
|
|
349
|
+
return parsed;
|
|
350
|
+
}
|
|
281
351
|
async function update(tree, directory, data) {
|
|
282
352
|
const queue = [];
|
|
283
353
|
(0, import_unist_util_visit2.visit)(
|
|
284
354
|
tree,
|
|
285
355
|
["mdxJsxFlowElement", "mdxJsxTextElement"],
|
|
286
|
-
(
|
|
287
|
-
|
|
356
|
+
(_node, _, parent) => {
|
|
357
|
+
const node = _node;
|
|
358
|
+
if (node.name !== TagName) return;
|
|
288
359
|
const params = {};
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
params[attr.name] = attr.value;
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
specifier = value;
|
|
360
|
+
const specifier = flattenNode2(node);
|
|
361
|
+
if (specifier.length === 0) return "skip";
|
|
362
|
+
for (const attr of node.attributes) {
|
|
363
|
+
if (attr.type === "mdxJsxAttribute" && (typeof attr.value === "string" || attr.value === null)) {
|
|
364
|
+
params[attr.name] = attr.value;
|
|
298
365
|
}
|
|
299
366
|
}
|
|
300
|
-
|
|
301
|
-
const
|
|
302
|
-
const targetPath = path.resolve(
|
|
367
|
+
const { file: relativePath, section } = parseSpecifier(specifier);
|
|
368
|
+
const file = path.resolve(
|
|
303
369
|
"cwd" in params ? process.cwd() : directory,
|
|
304
|
-
|
|
370
|
+
relativePath
|
|
305
371
|
);
|
|
306
|
-
const asCode = params.lang || !file.endsWith(".md") && !file.endsWith(".mdx");
|
|
307
372
|
queue.push(
|
|
308
|
-
|
|
309
|
-
data._compiler?.addDependency(targetPath);
|
|
310
|
-
if (asCode) {
|
|
311
|
-
const lang = params.lang ?? path.extname(file).slice(1);
|
|
312
|
-
Object.assign(node, {
|
|
313
|
-
type: "code",
|
|
314
|
-
lang,
|
|
315
|
-
meta: params.meta,
|
|
316
|
-
value: content,
|
|
317
|
-
data: {}
|
|
318
|
-
});
|
|
319
|
-
return;
|
|
320
|
-
}
|
|
321
|
-
const processor = data._processor ? data._processor.getProcessor(
|
|
322
|
-
targetPath.endsWith(".md") ? "md" : "mdx"
|
|
323
|
-
) : this;
|
|
324
|
-
let parsed = processor.parse(fumaMatter(content).content);
|
|
325
|
-
if (section) {
|
|
326
|
-
const extracted = extractSection(parsed, section);
|
|
327
|
-
if (!extracted)
|
|
328
|
-
throw new Error(
|
|
329
|
-
`Cannot find section ${section} in ${file}, make sure you have encapsulated the section in a <section id="${section}"> tag`
|
|
330
|
-
);
|
|
331
|
-
parsed = extracted;
|
|
332
|
-
}
|
|
333
|
-
await update.call(
|
|
334
|
-
processor,
|
|
335
|
-
parsed,
|
|
336
|
-
path.dirname(targetPath),
|
|
337
|
-
data
|
|
338
|
-
);
|
|
373
|
+
embedContent(file, section, params, data).then((replace) => {
|
|
339
374
|
Object.assign(
|
|
340
375
|
parent && parent.type === "paragraph" ? parent : node,
|
|
341
|
-
|
|
342
|
-
);
|
|
343
|
-
}).catch((e) => {
|
|
344
|
-
throw new Error(
|
|
345
|
-
`failed to read file ${targetPath}
|
|
346
|
-
${e instanceof Error ? e.message : String(e)}`,
|
|
347
|
-
{ cause: e }
|
|
376
|
+
replace
|
|
348
377
|
);
|
|
349
378
|
})
|
|
350
379
|
);
|
|
@@ -354,9 +383,14 @@ ${e instanceof Error ? e.message : String(e)}`,
|
|
|
354
383
|
await Promise.all(queue);
|
|
355
384
|
}
|
|
356
385
|
return async (tree, file) => {
|
|
357
|
-
await update
|
|
386
|
+
await update(tree, path.dirname(file.path), file.data);
|
|
358
387
|
};
|
|
359
388
|
}
|
|
389
|
+
function getDefaultProcessor(format) {
|
|
390
|
+
const mdProcessor = (0, import_unified.unified)().use(import_remark_parse.default);
|
|
391
|
+
if (format === "md") return mdProcessor;
|
|
392
|
+
return mdProcessor.use(import_remark_mdx.default);
|
|
393
|
+
}
|
|
360
394
|
// Annotate the CommonJS export names for ESM import in node:
|
|
361
395
|
0 && (module.exports = {
|
|
362
396
|
defineCollections,
|
package/dist/config/index.d.cts
CHANGED
|
@@ -6,6 +6,10 @@ import 'fumadocs-core/mdx-plugins';
|
|
|
6
6
|
import '@mdx-js/mdx';
|
|
7
7
|
import 'zod';
|
|
8
8
|
|
|
9
|
+
interface Params {
|
|
10
|
+
lang?: string;
|
|
11
|
+
meta?: string;
|
|
12
|
+
}
|
|
9
13
|
declare function remarkInclude(this: Processor): Transformer<Root, Root>;
|
|
10
14
|
|
|
11
|
-
export { remarkInclude };
|
|
15
|
+
export { type Params, remarkInclude };
|
package/dist/config/index.d.ts
CHANGED
|
@@ -6,6 +6,10 @@ import 'fumadocs-core/mdx-plugins';
|
|
|
6
6
|
import '@mdx-js/mdx';
|
|
7
7
|
import 'zod';
|
|
8
8
|
|
|
9
|
+
interface Params {
|
|
10
|
+
lang?: string;
|
|
11
|
+
meta?: string;
|
|
12
|
+
}
|
|
9
13
|
declare function remarkInclude(this: Processor): Transformer<Root, Root>;
|
|
10
14
|
|
|
11
|
-
export { remarkInclude };
|
|
15
|
+
export { type Params, remarkInclude };
|
package/dist/config/index.js
CHANGED
|
@@ -7,10 +7,10 @@ import {
|
|
|
7
7
|
} from "../chunk-GBMFGEC7.js";
|
|
8
8
|
import {
|
|
9
9
|
getDefaultMDXOptions
|
|
10
|
-
} from "../chunk-
|
|
10
|
+
} from "../chunk-SMSNZ6N5.js";
|
|
11
11
|
import {
|
|
12
12
|
remarkInclude
|
|
13
|
-
} from "../chunk-
|
|
13
|
+
} from "../chunk-SVTXMVLQ.js";
|
|
14
14
|
import "../chunk-VWJKRQZR.js";
|
|
15
15
|
export {
|
|
16
16
|
defineCollections,
|