fumadocs-mdx 11.8.0 → 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-DnC1jKvn.d.cts +46 -0
- package/dist/build-mdx-DnC1jKvn.d.ts +46 -0
- package/dist/{chunk-XZR5QXVY.js → chunk-BWRDVK5L.js} +1 -1
- package/dist/{chunk-7JFPDRW7.js → chunk-QQWCBFFE.js} +2 -4
- package/dist/{chunk-XVL4ZQFK.js → chunk-QVZ7JH4H.js} +1 -1
- package/dist/{chunk-GYWPPGFD.js → chunk-SMSNZ6N5.js} +38 -7
- package/dist/chunk-SVTXMVLQ.js +139 -0
- package/dist/config/index.cjs +121 -70
- 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 +121 -70
- 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 +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/loader-mdx.cjs +130 -81
- package/dist/loader-mdx.js +8 -8
- package/dist/{mdx-options-3NB74EMB.js → mdx-options-T73E4LQB.js} +1 -1
- package/dist/next/index.cjs +42 -11
- package/dist/next/index.js +2 -2
- package/dist/runtime/async.cjs +122 -73
- package/dist/runtime/async.d.cts +3 -2
- package/dist/runtime/async.d.ts +3 -2
- package/dist/runtime/async.js +3 -3
- package/dist/runtime/vite.d.cts +3 -12
- package/dist/runtime/vite.d.ts +3 -12
- package/dist/{types-BukvTPdG.d.cts → types-BmVgoqsr.d.cts} +3 -12
- package/dist/{types-B2ozVm_9.d.ts → types-WSHJKA8L.d.ts} +3 -12
- package/dist/vite/index.cjs +126 -77
- package/dist/vite/index.js +3 -3
- package/package.json +9 -7
- package/dist/chunk-IGXZS2W6.js +0 -119
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { createProcessor } from '@mdx-js/mdx';
|
|
2
|
+
import { StructuredData } from 'fumadocs-core/mdx-plugins';
|
|
3
|
+
import { TableOfContents } from 'fumadocs-core/server';
|
|
4
|
+
import { FC } from 'react';
|
|
5
|
+
import { MDXProps } from 'mdx/types';
|
|
6
|
+
|
|
7
|
+
declare module 'vfile' {
|
|
8
|
+
interface DataMap {
|
|
9
|
+
extractedReferences: ExtractedReference[];
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
interface ExtractedReference {
|
|
13
|
+
href: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type Processor = ReturnType<typeof createProcessor>;
|
|
17
|
+
interface CompilerOptions {
|
|
18
|
+
addDependency: (file: string) => void;
|
|
19
|
+
}
|
|
20
|
+
interface CompiledMDXProperties<Frontmatter = Record<string, unknown>> {
|
|
21
|
+
frontmatter: Frontmatter;
|
|
22
|
+
structuredData: StructuredData;
|
|
23
|
+
toc: TableOfContents;
|
|
24
|
+
default: FC<MDXProps>;
|
|
25
|
+
/**
|
|
26
|
+
* Only available when `lastModifiedTime` is enabled on MDX loader
|
|
27
|
+
*/
|
|
28
|
+
lastModified?: Date;
|
|
29
|
+
extractedReferences?: ExtractedReference[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
declare module 'vfile' {
|
|
33
|
+
interface DataMap {
|
|
34
|
+
/**
|
|
35
|
+
* [Fumadocs MDX] raw frontmatter, you can modify it
|
|
36
|
+
*/
|
|
37
|
+
frontmatter?: Record<string, unknown>;
|
|
38
|
+
/**
|
|
39
|
+
* [Fumadocs MDX] The compiler object from loader
|
|
40
|
+
*/
|
|
41
|
+
_compiler?: CompilerOptions;
|
|
42
|
+
_getProcessor?: (format: 'md' | 'mdx') => Processor;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export type { CompiledMDXProperties as C };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { createProcessor } from '@mdx-js/mdx';
|
|
2
|
+
import { StructuredData } from 'fumadocs-core/mdx-plugins';
|
|
3
|
+
import { TableOfContents } from 'fumadocs-core/server';
|
|
4
|
+
import { FC } from 'react';
|
|
5
|
+
import { MDXProps } from 'mdx/types';
|
|
6
|
+
|
|
7
|
+
declare module 'vfile' {
|
|
8
|
+
interface DataMap {
|
|
9
|
+
extractedReferences: ExtractedReference[];
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
interface ExtractedReference {
|
|
13
|
+
href: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type Processor = ReturnType<typeof createProcessor>;
|
|
17
|
+
interface CompilerOptions {
|
|
18
|
+
addDependency: (file: string) => void;
|
|
19
|
+
}
|
|
20
|
+
interface CompiledMDXProperties<Frontmatter = Record<string, unknown>> {
|
|
21
|
+
frontmatter: Frontmatter;
|
|
22
|
+
structuredData: StructuredData;
|
|
23
|
+
toc: TableOfContents;
|
|
24
|
+
default: FC<MDXProps>;
|
|
25
|
+
/**
|
|
26
|
+
* Only available when `lastModifiedTime` is enabled on MDX loader
|
|
27
|
+
*/
|
|
28
|
+
lastModified?: Date;
|
|
29
|
+
extractedReferences?: ExtractedReference[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
declare module 'vfile' {
|
|
33
|
+
interface DataMap {
|
|
34
|
+
/**
|
|
35
|
+
* [Fumadocs MDX] raw frontmatter, you can modify it
|
|
36
|
+
*/
|
|
37
|
+
frontmatter?: Record<string, unknown>;
|
|
38
|
+
/**
|
|
39
|
+
* [Fumadocs MDX] The compiler object from loader
|
|
40
|
+
*/
|
|
41
|
+
_compiler?: CompilerOptions;
|
|
42
|
+
_getProcessor?: (format: 'md' | 'mdx') => Processor;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export type { CompiledMDXProperties as C };
|
|
@@ -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: {
|
|
@@ -68,6 +95,7 @@ function getDefaultMDXOptions({
|
|
|
68
95
|
}) {
|
|
69
96
|
const mdxExports = [
|
|
70
97
|
"structuredData",
|
|
98
|
+
"extractedReferences",
|
|
71
99
|
"frontmatter",
|
|
72
100
|
"lastModified",
|
|
73
101
|
...valueToExport
|
|
@@ -99,7 +127,10 @@ function getDefaultMDXOptions({
|
|
|
99
127
|
plugins.remarkStructure,
|
|
100
128
|
remarkStructureOptions
|
|
101
129
|
],
|
|
102
|
-
[
|
|
130
|
+
[
|
|
131
|
+
remarkPostprocess,
|
|
132
|
+
{ injectExports: mdxExports }
|
|
133
|
+
]
|
|
103
134
|
],
|
|
104
135
|
mdxOptions.remarkPlugins
|
|
105
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: {
|
|
@@ -160,6 +187,7 @@ function getDefaultMDXOptions({
|
|
|
160
187
|
}) {
|
|
161
188
|
const mdxExports = [
|
|
162
189
|
"structuredData",
|
|
190
|
+
"extractedReferences",
|
|
163
191
|
"frontmatter",
|
|
164
192
|
"lastModified",
|
|
165
193
|
...valueToExport
|
|
@@ -191,7 +219,10 @@ function getDefaultMDXOptions({
|
|
|
191
219
|
plugins.remarkStructure,
|
|
192
220
|
remarkStructureOptions
|
|
193
221
|
],
|
|
194
|
-
[
|
|
222
|
+
[
|
|
223
|
+
remarkPostprocess,
|
|
224
|
+
{ injectExports: mdxExports }
|
|
225
|
+
]
|
|
195
226
|
],
|
|
196
227
|
mdxOptions.remarkPlugins
|
|
197
228
|
);
|
|
@@ -212,7 +243,8 @@ function getDefaultMDXOptions({
|
|
|
212
243
|
}
|
|
213
244
|
|
|
214
245
|
// src/mdx-plugins/remark-include.ts
|
|
215
|
-
var
|
|
246
|
+
var import_unified = require("unified");
|
|
247
|
+
var import_unist_util_visit2 = require("unist-util-visit");
|
|
216
248
|
var path = __toESM(require("path"), 1);
|
|
217
249
|
var fs = __toESM(require("fs/promises"), 1);
|
|
218
250
|
|
|
@@ -233,9 +265,13 @@ function fumaMatter(input) {
|
|
|
233
265
|
}
|
|
234
266
|
|
|
235
267
|
// src/mdx-plugins/remark-include.ts
|
|
236
|
-
|
|
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) {
|
|
237
273
|
if ("children" in node)
|
|
238
|
-
return node.children.map((child) =>
|
|
274
|
+
return node.children.map((child) => flattenNode2(child)).join("");
|
|
239
275
|
if ("value" in node) return node.value;
|
|
240
276
|
return "";
|
|
241
277
|
}
|
|
@@ -248,86 +284,96 @@ function parseSpecifier(specifier) {
|
|
|
248
284
|
};
|
|
249
285
|
}
|
|
250
286
|
function extractSection(root, section) {
|
|
287
|
+
let nodes;
|
|
251
288
|
for (const node of root.children) {
|
|
252
289
|
if (node.type === "mdxJsxFlowElement" && node.name === "section" && node.attributes.some(
|
|
253
290
|
(attr) => attr.type === "mdxJsxAttribute" && attr.name === "id" && attr.value === section
|
|
254
291
|
)) {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
292
|
+
nodes = node.children;
|
|
293
|
+
break;
|
|
294
|
+
}
|
|
295
|
+
if (node.type === "heading" && node.data?.hProperties?.id === section) {
|
|
296
|
+
nodes = [node];
|
|
297
|
+
continue;
|
|
259
298
|
}
|
|
299
|
+
if (!nodes) continue;
|
|
300
|
+
if (node.type === "heading") break;
|
|
301
|
+
nodes.push(node);
|
|
260
302
|
}
|
|
303
|
+
if (nodes)
|
|
304
|
+
return {
|
|
305
|
+
type: "root",
|
|
306
|
+
children: nodes
|
|
307
|
+
};
|
|
261
308
|
}
|
|
262
309
|
function remarkInclude() {
|
|
263
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
|
+
}
|
|
264
351
|
async function update(tree, directory, data) {
|
|
265
352
|
const queue = [];
|
|
266
|
-
(0,
|
|
353
|
+
(0, import_unist_util_visit2.visit)(
|
|
267
354
|
tree,
|
|
268
355
|
["mdxJsxFlowElement", "mdxJsxTextElement"],
|
|
269
|
-
(
|
|
270
|
-
|
|
356
|
+
(_node, _, parent) => {
|
|
357
|
+
const node = _node;
|
|
358
|
+
if (node.name !== TagName) return;
|
|
271
359
|
const params = {};
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
params[attr.name] = attr.value;
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
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;
|
|
281
365
|
}
|
|
282
366
|
}
|
|
283
|
-
|
|
284
|
-
const
|
|
285
|
-
const targetPath = path.resolve(
|
|
367
|
+
const { file: relativePath, section } = parseSpecifier(specifier);
|
|
368
|
+
const file = path.resolve(
|
|
286
369
|
"cwd" in params ? process.cwd() : directory,
|
|
287
|
-
|
|
370
|
+
relativePath
|
|
288
371
|
);
|
|
289
|
-
const asCode = params.lang || !file.endsWith(".md") && !file.endsWith(".mdx");
|
|
290
372
|
queue.push(
|
|
291
|
-
|
|
292
|
-
data._compiler?.addDependency(targetPath);
|
|
293
|
-
if (asCode) {
|
|
294
|
-
const lang = params.lang ?? path.extname(file).slice(1);
|
|
295
|
-
Object.assign(node, {
|
|
296
|
-
type: "code",
|
|
297
|
-
lang,
|
|
298
|
-
meta: params.meta,
|
|
299
|
-
value: content,
|
|
300
|
-
data: {}
|
|
301
|
-
});
|
|
302
|
-
return;
|
|
303
|
-
}
|
|
304
|
-
const processor = data._processor ? data._processor.getProcessor(
|
|
305
|
-
targetPath.endsWith(".md") ? "md" : "mdx"
|
|
306
|
-
) : this;
|
|
307
|
-
let parsed = processor.parse(fumaMatter(content).content);
|
|
308
|
-
if (section) {
|
|
309
|
-
const extracted = extractSection(parsed, section);
|
|
310
|
-
if (!extracted)
|
|
311
|
-
throw new Error(
|
|
312
|
-
`Cannot find section ${section} in ${file}, make sure you have encapsulated the section in a <section id="${section}"> tag`
|
|
313
|
-
);
|
|
314
|
-
parsed = extracted;
|
|
315
|
-
}
|
|
316
|
-
await update.call(
|
|
317
|
-
processor,
|
|
318
|
-
parsed,
|
|
319
|
-
path.dirname(targetPath),
|
|
320
|
-
data
|
|
321
|
-
);
|
|
373
|
+
embedContent(file, section, params, data).then((replace) => {
|
|
322
374
|
Object.assign(
|
|
323
375
|
parent && parent.type === "paragraph" ? parent : node,
|
|
324
|
-
|
|
325
|
-
);
|
|
326
|
-
}).catch((e) => {
|
|
327
|
-
throw new Error(
|
|
328
|
-
`failed to read file ${targetPath}
|
|
329
|
-
${e instanceof Error ? e.message : String(e)}`,
|
|
330
|
-
{ cause: e }
|
|
376
|
+
replace
|
|
331
377
|
);
|
|
332
378
|
})
|
|
333
379
|
);
|
|
@@ -337,9 +383,14 @@ ${e instanceof Error ? e.message : String(e)}`,
|
|
|
337
383
|
await Promise.all(queue);
|
|
338
384
|
}
|
|
339
385
|
return async (tree, file) => {
|
|
340
|
-
await update
|
|
386
|
+
await update(tree, path.dirname(file.path), file.data);
|
|
341
387
|
};
|
|
342
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
|
+
}
|
|
343
394
|
// Annotate the CommonJS export names for ESM import in node:
|
|
344
395
|
0 && (module.exports = {
|
|
345
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,
|