fumadocs-mdx 11.6.0 → 13.0.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/bin.cjs +1728 -0
- package/dist/bin.d.cts +1 -0
- package/dist/bin.d.ts +1 -0
- package/dist/bin.js +16 -0
- package/dist/build-mdx-CCNr86q6.d.ts +53 -0
- package/dist/build-mdx-D-r3_eQL.d.cts +53 -0
- package/dist/bun/index.cjs +857 -0
- package/dist/bun/index.d.cts +13 -0
- package/dist/bun/index.d.ts +13 -0
- package/dist/bun/index.js +62 -0
- package/dist/chunk-3J3WL7WN.js +160 -0
- package/dist/chunk-CXA4JO4Z.js +45 -0
- package/dist/chunk-EELYB2XC.js +207 -0
- package/dist/chunk-FSZMKRVH.js +80 -0
- package/dist/chunk-II3H5ZVZ.js +77 -0
- package/dist/chunk-JVZFH6ND.js +40 -0
- package/dist/chunk-K5ZLPEIQ.js +207 -0
- package/dist/{chunk-VFALQK6O.js → chunk-KILFIBVW.js} +21 -12
- package/dist/chunk-NVRDCY6Z.js +30 -0
- package/dist/chunk-U4MQ44TS.js +53 -0
- package/dist/chunk-VWJKRQZR.js +19 -0
- package/dist/chunk-XQ5O7IPO.js +128 -0
- package/dist/chunk-XZY2AWJI.js +81 -0
- package/dist/chunk-YVCR6FUH.js +82 -0
- package/dist/config/index.cjs +232 -128
- package/dist/config/index.d.cts +4 -6
- package/dist/config/index.d.ts +4 -6
- package/dist/config/index.js +26 -19
- package/dist/core-B6j6Fxse.d.cts +218 -0
- package/dist/core-B6j6Fxse.d.ts +218 -0
- package/dist/index.cjs +0 -76
- package/dist/index.d.cts +73 -20
- package/dist/index.d.ts +73 -20
- package/dist/index.js +0 -10
- package/dist/load-MNG3CLET.js +7 -0
- package/dist/next/index.cjs +567 -314
- package/dist/next/index.d.cts +9 -12
- package/dist/next/index.d.ts +9 -12
- package/dist/next/index.js +238 -211
- package/dist/node/loader.cjs +922 -0
- package/dist/node/loader.d.cts +5 -0
- package/dist/node/loader.d.ts +5 -0
- package/dist/node/loader.js +33 -0
- package/dist/plugins/json-schema.cjs +162 -0
- package/dist/plugins/json-schema.d.cts +24 -0
- package/dist/plugins/json-schema.d.ts +24 -0
- package/dist/plugins/json-schema.js +78 -0
- package/dist/{mdx-options-CAU273O3.js → preset-ZMP6U62C.js} +1 -1
- package/dist/runtime/next/async.cjs +715 -0
- package/dist/runtime/next/async.d.cts +21 -0
- package/dist/runtime/next/async.d.ts +21 -0
- package/dist/runtime/next/async.js +89 -0
- package/dist/runtime/next/index.cjs +136 -0
- package/dist/runtime/next/index.d.cts +33 -0
- package/dist/runtime/next/index.d.ts +33 -0
- package/dist/runtime/next/index.js +11 -0
- package/dist/runtime/vite/browser.cjs +107 -0
- package/dist/runtime/vite/browser.d.cts +59 -0
- package/dist/runtime/vite/browser.d.ts +59 -0
- package/dist/runtime/vite/browser.js +11 -0
- package/dist/runtime/vite/server.cjs +243 -0
- package/dist/runtime/vite/server.d.cts +30 -0
- package/dist/runtime/vite/server.d.ts +30 -0
- package/dist/runtime/vite/server.js +111 -0
- package/dist/types-AGzTfBmf.d.ts +45 -0
- package/dist/types-DKGMoay5.d.cts +45 -0
- package/dist/vite/index.cjs +1185 -0
- package/dist/vite/index.d.cts +45 -0
- package/dist/vite/index.d.ts +45 -0
- package/dist/vite/index.js +297 -0
- package/dist/webpack/index.cjs +957 -0
- package/dist/{loader-mdx.d.cts → webpack/index.d.cts} +3 -6
- package/dist/{loader-mdx.d.ts → webpack/index.d.ts} +3 -6
- package/dist/webpack/index.js +44 -0
- package/loader-mdx.cjs +1 -1
- package/package.json +86 -29
- package/bin.js +0 -5
- package/dist/chunk-2ZOW45YZ.js +0 -63
- package/dist/chunk-DRVUBK5B.js +0 -39
- package/dist/chunk-HFLDWPJA.js +0 -62
- package/dist/chunk-IOENRFUX.js +0 -112
- package/dist/chunk-MK7EXW7O.js +0 -75
- package/dist/define-BaW0PQDJ.d.cts +0 -201
- package/dist/define-BaW0PQDJ.d.ts +0 -201
- package/dist/loader-mdx.cjs +0 -527
- package/dist/loader-mdx.js +0 -162
- package/dist/runtime/async.cjs +0 -269
- package/dist/runtime/async.d.cts +0 -18
- package/dist/runtime/async.d.ts +0 -18
- package/dist/runtime/async.js +0 -73
- package/dist/types-BNrQHCj5.d.cts +0 -100
- package/dist/types-DEduCvIT.d.ts +0 -100
- package/dist/watcher-IAZDSTU7.js +0 -24
|
@@ -0,0 +1,857 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __esm = (fn, res) => function __init() {
|
|
9
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
10
|
+
};
|
|
11
|
+
var __export = (target, all) => {
|
|
12
|
+
for (var name in all)
|
|
13
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
+
};
|
|
15
|
+
var __copyProps = (to, from, except, desc) => {
|
|
16
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
17
|
+
for (let key of __getOwnPropNames(from))
|
|
18
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
19
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
20
|
+
}
|
|
21
|
+
return to;
|
|
22
|
+
};
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
24
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
29
|
+
mod
|
|
30
|
+
));
|
|
31
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
32
|
+
|
|
33
|
+
// src/loaders/mdx/preset.ts
|
|
34
|
+
var preset_exports = {};
|
|
35
|
+
__export(preset_exports, {
|
|
36
|
+
getDefaultMDXOptions: () => getDefaultMDXOptions
|
|
37
|
+
});
|
|
38
|
+
function pluginOption(def, options = []) {
|
|
39
|
+
const list = def(Array.isArray(options) ? options : []).filter(
|
|
40
|
+
Boolean
|
|
41
|
+
);
|
|
42
|
+
if (typeof options === "function") {
|
|
43
|
+
return options(list);
|
|
44
|
+
}
|
|
45
|
+
return list;
|
|
46
|
+
}
|
|
47
|
+
function getDefaultMDXOptions({
|
|
48
|
+
valueToExport = [],
|
|
49
|
+
rehypeCodeOptions,
|
|
50
|
+
remarkImageOptions,
|
|
51
|
+
remarkHeadingOptions,
|
|
52
|
+
remarkStructureOptions,
|
|
53
|
+
remarkCodeTabOptions,
|
|
54
|
+
remarkNpmOptions,
|
|
55
|
+
_withoutBundler = false,
|
|
56
|
+
...mdxOptions
|
|
57
|
+
}) {
|
|
58
|
+
const remarkPlugins = pluginOption(
|
|
59
|
+
(v) => [
|
|
60
|
+
plugins.remarkGfm,
|
|
61
|
+
[
|
|
62
|
+
plugins.remarkHeading,
|
|
63
|
+
{
|
|
64
|
+
generateToc: false,
|
|
65
|
+
...remarkHeadingOptions
|
|
66
|
+
}
|
|
67
|
+
],
|
|
68
|
+
remarkImageOptions !== false && [
|
|
69
|
+
plugins.remarkImage,
|
|
70
|
+
{
|
|
71
|
+
...remarkImageOptions,
|
|
72
|
+
useImport: _withoutBundler ? false : remarkImageOptions?.useImport
|
|
73
|
+
}
|
|
74
|
+
],
|
|
75
|
+
"remarkCodeTab" in plugins && remarkCodeTabOptions !== false && [
|
|
76
|
+
plugins.remarkCodeTab,
|
|
77
|
+
remarkCodeTabOptions
|
|
78
|
+
],
|
|
79
|
+
"remarkNpm" in plugins && remarkNpmOptions !== false && [plugins.remarkNpm, remarkNpmOptions],
|
|
80
|
+
...v,
|
|
81
|
+
remarkStructureOptions !== false && [
|
|
82
|
+
plugins.remarkStructure,
|
|
83
|
+
remarkStructureOptions
|
|
84
|
+
],
|
|
85
|
+
() => {
|
|
86
|
+
return (_, file) => {
|
|
87
|
+
file.data["mdx-export"] ??= [];
|
|
88
|
+
for (const name of valueToExport) {
|
|
89
|
+
if (name in file.data)
|
|
90
|
+
file.data["mdx-export"].push({ name, value: file.data[name] });
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
],
|
|
95
|
+
mdxOptions.remarkPlugins
|
|
96
|
+
);
|
|
97
|
+
const rehypePlugins = pluginOption(
|
|
98
|
+
(v) => [
|
|
99
|
+
rehypeCodeOptions !== false && [plugins.rehypeCode, rehypeCodeOptions],
|
|
100
|
+
...v,
|
|
101
|
+
plugins.rehypeToc
|
|
102
|
+
],
|
|
103
|
+
mdxOptions.rehypePlugins
|
|
104
|
+
);
|
|
105
|
+
return {
|
|
106
|
+
...mdxOptions,
|
|
107
|
+
outputFormat: _withoutBundler ? "function-body" : mdxOptions.outputFormat,
|
|
108
|
+
remarkPlugins,
|
|
109
|
+
rehypePlugins
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
var plugins;
|
|
113
|
+
var init_preset = __esm({
|
|
114
|
+
"src/loaders/mdx/preset.ts"() {
|
|
115
|
+
"use strict";
|
|
116
|
+
plugins = __toESM(require("fumadocs-core/mdx-plugins"), 1);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
// src/config/build.ts
|
|
121
|
+
function buildConfig(config) {
|
|
122
|
+
const collections = /* @__PURE__ */ new Map();
|
|
123
|
+
let globalConfig = {};
|
|
124
|
+
for (const [k, v] of Object.entries(config)) {
|
|
125
|
+
if (!v) {
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (typeof v === "object" && "type" in v) {
|
|
129
|
+
if (v.type === "docs") {
|
|
130
|
+
collections.set(k, v);
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (v.type === "doc" || v.type === "meta") {
|
|
134
|
+
collections.set(k, v);
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (k === "default" && v) {
|
|
139
|
+
globalConfig = v;
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
throw new Error(
|
|
143
|
+
`Unknown export "${k}", you can only export collections from source configuration file.`
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
const mdxOptionsCache = /* @__PURE__ */ new Map();
|
|
147
|
+
return {
|
|
148
|
+
global: globalConfig,
|
|
149
|
+
collections,
|
|
150
|
+
async getDefaultMDXOptions(mode = "default") {
|
|
151
|
+
const cached = mdxOptionsCache.get(mode);
|
|
152
|
+
if (cached) return cached;
|
|
153
|
+
const input = this.global.mdxOptions;
|
|
154
|
+
async function uncached() {
|
|
155
|
+
const options = typeof input === "function" ? await input() : input;
|
|
156
|
+
const { getDefaultMDXOptions: getDefaultMDXOptions2 } = await Promise.resolve().then(() => (init_preset(), preset_exports));
|
|
157
|
+
if (options?.preset === "minimal") return options;
|
|
158
|
+
return getDefaultMDXOptions2({
|
|
159
|
+
...options,
|
|
160
|
+
_withoutBundler: mode === "remote"
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
const result = uncached();
|
|
164
|
+
mdxOptionsCache.set(mode, result);
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
var init_build = __esm({
|
|
170
|
+
"src/config/build.ts"() {
|
|
171
|
+
"use strict";
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
// src/bun/index.ts
|
|
176
|
+
var bun_exports = {};
|
|
177
|
+
__export(bun_exports, {
|
|
178
|
+
createMdxPlugin: () => createMdxPlugin
|
|
179
|
+
});
|
|
180
|
+
module.exports = __toCommonJS(bun_exports);
|
|
181
|
+
|
|
182
|
+
// src/utils/fuma-matter.ts
|
|
183
|
+
var import_js_yaml = require("js-yaml");
|
|
184
|
+
var regex = /^---\r?\n(.+?)\r?\n---\r?\n/s;
|
|
185
|
+
function fumaMatter(input) {
|
|
186
|
+
const output = { matter: "", data: {}, content: input };
|
|
187
|
+
const match = regex.exec(input);
|
|
188
|
+
if (!match) {
|
|
189
|
+
return output;
|
|
190
|
+
}
|
|
191
|
+
output.matter = match[0];
|
|
192
|
+
output.content = input.slice(match[0].length);
|
|
193
|
+
const loaded = (0, import_js_yaml.load)(match[1]);
|
|
194
|
+
output.data = loaded ?? {};
|
|
195
|
+
return output;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// src/utils/validation.ts
|
|
199
|
+
var import_picocolors = __toESM(require("picocolors"), 1);
|
|
200
|
+
var ValidationError = class extends Error {
|
|
201
|
+
constructor(message, issues) {
|
|
202
|
+
super(
|
|
203
|
+
`${message}:
|
|
204
|
+
${issues.map((issue) => ` ${issue.path}: ${issue.message}`).join("\n")}`
|
|
205
|
+
);
|
|
206
|
+
this.title = message;
|
|
207
|
+
this.issues = issues;
|
|
208
|
+
}
|
|
209
|
+
toStringFormatted() {
|
|
210
|
+
return [
|
|
211
|
+
import_picocolors.default.bold(`[MDX] ${this.title}:`),
|
|
212
|
+
...this.issues.map(
|
|
213
|
+
(issue) => import_picocolors.default.redBright(
|
|
214
|
+
`- ${import_picocolors.default.bold(issue.path?.join(".") ?? "*")}: ${issue.message}`
|
|
215
|
+
)
|
|
216
|
+
)
|
|
217
|
+
].join("\n");
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
async function validate(schema, data, context, errorMessage) {
|
|
221
|
+
if (typeof schema === "function" && !("~standard" in schema)) {
|
|
222
|
+
schema = schema(context);
|
|
223
|
+
}
|
|
224
|
+
if ("~standard" in schema) {
|
|
225
|
+
const result = await schema["~standard"].validate(
|
|
226
|
+
data
|
|
227
|
+
);
|
|
228
|
+
if (result.issues) {
|
|
229
|
+
throw new ValidationError(errorMessage, result.issues);
|
|
230
|
+
}
|
|
231
|
+
return result.value;
|
|
232
|
+
}
|
|
233
|
+
return data;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// src/utils/git-timestamp.ts
|
|
237
|
+
var import_node_path = __toESM(require("path"), 1);
|
|
238
|
+
var import_tinyexec = require("tinyexec");
|
|
239
|
+
var cache = /* @__PURE__ */ new Map();
|
|
240
|
+
async function getGitTimestamp(file) {
|
|
241
|
+
const cached = cache.get(file);
|
|
242
|
+
if (cached) return cached;
|
|
243
|
+
try {
|
|
244
|
+
const out = await (0, import_tinyexec.x)(
|
|
245
|
+
"git",
|
|
246
|
+
["log", "-1", '--pretty="%ai"', import_node_path.default.relative(process.cwd(), file)],
|
|
247
|
+
{
|
|
248
|
+
throwOnError: true
|
|
249
|
+
}
|
|
250
|
+
);
|
|
251
|
+
const time = new Date(out.stdout);
|
|
252
|
+
cache.set(file, time);
|
|
253
|
+
return time;
|
|
254
|
+
} catch {
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// src/loaders/mdx/build-mdx.ts
|
|
260
|
+
var import_mdx = require("@mdx-js/mdx");
|
|
261
|
+
|
|
262
|
+
// src/loaders/mdx/remark-include.ts
|
|
263
|
+
var import_unified = require("unified");
|
|
264
|
+
var import_unist_util_visit2 = require("unist-util-visit");
|
|
265
|
+
var path2 = __toESM(require("path"), 1);
|
|
266
|
+
var fs = __toESM(require("fs/promises"), 1);
|
|
267
|
+
var import_mdx_plugins = require("fumadocs-core/mdx-plugins");
|
|
268
|
+
|
|
269
|
+
// src/loaders/mdx/remark-unravel.ts
|
|
270
|
+
var import_unist_util_visit = require("unist-util-visit");
|
|
271
|
+
function remarkMarkAndUnravel() {
|
|
272
|
+
return (tree) => {
|
|
273
|
+
(0, import_unist_util_visit.visit)(tree, function(node, index, parent) {
|
|
274
|
+
let offset = -1;
|
|
275
|
+
let all = true;
|
|
276
|
+
let oneOrMore = false;
|
|
277
|
+
if (parent && typeof index === "number" && node.type === "paragraph") {
|
|
278
|
+
const children = node.children;
|
|
279
|
+
while (++offset < children.length) {
|
|
280
|
+
const child = children[offset];
|
|
281
|
+
if (child.type === "mdxJsxTextElement" || child.type === "mdxTextExpression") {
|
|
282
|
+
oneOrMore = true;
|
|
283
|
+
} else if (child.type === "text" && child.value.trim().length === 0) {
|
|
284
|
+
} else {
|
|
285
|
+
all = false;
|
|
286
|
+
break;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
if (all && oneOrMore) {
|
|
290
|
+
offset = -1;
|
|
291
|
+
const newChildren = [];
|
|
292
|
+
while (++offset < children.length) {
|
|
293
|
+
const child = children[offset];
|
|
294
|
+
if (child.type === "mdxJsxTextElement") {
|
|
295
|
+
child.type = "mdxJsxFlowElement";
|
|
296
|
+
}
|
|
297
|
+
if (child.type === "mdxTextExpression") {
|
|
298
|
+
child.type = "mdxFlowExpression";
|
|
299
|
+
}
|
|
300
|
+
if (child.type === "text" && /^[\t\r\n ]+$/.test(String(child.value))) {
|
|
301
|
+
} else {
|
|
302
|
+
newChildren.push(child);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
parent.children.splice(index, 1, ...newChildren);
|
|
306
|
+
return index;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// src/loaders/mdx/remark-include.ts
|
|
314
|
+
var ElementLikeTypes = [
|
|
315
|
+
"mdxJsxFlowElement",
|
|
316
|
+
"mdxJsxTextElement",
|
|
317
|
+
"containerDirective",
|
|
318
|
+
"textDirective",
|
|
319
|
+
"leafDirective"
|
|
320
|
+
];
|
|
321
|
+
function isElementLike(node) {
|
|
322
|
+
return ElementLikeTypes.includes(node.type);
|
|
323
|
+
}
|
|
324
|
+
function parseElementAttributes(element) {
|
|
325
|
+
if (Array.isArray(element.attributes)) {
|
|
326
|
+
const attributes = {};
|
|
327
|
+
for (const attr of element.attributes) {
|
|
328
|
+
if (attr.type === "mdxJsxAttribute" && (typeof attr.value === "string" || attr.value === null)) {
|
|
329
|
+
attributes[attr.name] = attr.value;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return attributes;
|
|
333
|
+
}
|
|
334
|
+
return element.attributes ?? {};
|
|
335
|
+
}
|
|
336
|
+
function flattenNode(node) {
|
|
337
|
+
if ("children" in node)
|
|
338
|
+
return node.children.map((child) => flattenNode(child)).join("");
|
|
339
|
+
if ("value" in node) return node.value;
|
|
340
|
+
return "";
|
|
341
|
+
}
|
|
342
|
+
function parseSpecifier(specifier) {
|
|
343
|
+
const idx = specifier.lastIndexOf("#");
|
|
344
|
+
if (idx === -1) return { file: specifier };
|
|
345
|
+
return {
|
|
346
|
+
file: specifier.slice(0, idx),
|
|
347
|
+
section: specifier.slice(idx + 1)
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
function extractSection(root, section) {
|
|
351
|
+
let nodes;
|
|
352
|
+
let capturingHeadingContent = false;
|
|
353
|
+
(0, import_unist_util_visit2.visit)(root, (node) => {
|
|
354
|
+
if (node.type === "heading") {
|
|
355
|
+
if (capturingHeadingContent) {
|
|
356
|
+
return false;
|
|
357
|
+
}
|
|
358
|
+
if (node.data?.hProperties?.id === section) {
|
|
359
|
+
capturingHeadingContent = true;
|
|
360
|
+
nodes = [node];
|
|
361
|
+
return "skip";
|
|
362
|
+
}
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
if (capturingHeadingContent) {
|
|
366
|
+
nodes?.push(node);
|
|
367
|
+
return "skip";
|
|
368
|
+
}
|
|
369
|
+
if (isElementLike(node) && node.name === "section") {
|
|
370
|
+
const attributes = parseElementAttributes(node);
|
|
371
|
+
if (attributes.id === section) {
|
|
372
|
+
nodes = node.children;
|
|
373
|
+
return false;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
if (nodes)
|
|
378
|
+
return {
|
|
379
|
+
type: "root",
|
|
380
|
+
children: nodes
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
function remarkInclude() {
|
|
384
|
+
const TagName = "include";
|
|
385
|
+
const embedContent = async (file, heading, params, data) => {
|
|
386
|
+
let content;
|
|
387
|
+
try {
|
|
388
|
+
content = (await fs.readFile(file)).toString();
|
|
389
|
+
} catch (e) {
|
|
390
|
+
throw new Error(
|
|
391
|
+
`failed to read file ${file}
|
|
392
|
+
${e instanceof Error ? e.message : String(e)}`,
|
|
393
|
+
{ cause: e }
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
const ext = path2.extname(file);
|
|
397
|
+
data._compiler?.addDependency(file);
|
|
398
|
+
if (params.lang || ext !== ".md" && ext !== ".mdx") {
|
|
399
|
+
const lang = params.lang ?? ext.slice(1);
|
|
400
|
+
return {
|
|
401
|
+
type: "code",
|
|
402
|
+
lang,
|
|
403
|
+
meta: params.meta,
|
|
404
|
+
value: content,
|
|
405
|
+
data: {}
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
const parser = data._getProcessor ? data._getProcessor(ext === ".mdx" ? "mdx" : "md") : this;
|
|
409
|
+
const parsed = fumaMatter(content);
|
|
410
|
+
let mdast = parser.parse({
|
|
411
|
+
path: file,
|
|
412
|
+
value: parsed.content,
|
|
413
|
+
data: { frontmatter: parsed.data }
|
|
414
|
+
});
|
|
415
|
+
const baseProcessor = (0, import_unified.unified)().use(remarkMarkAndUnravel);
|
|
416
|
+
if (heading) {
|
|
417
|
+
const extracted = extractSection(
|
|
418
|
+
await baseProcessor.use(import_mdx_plugins.remarkHeading).run(mdast),
|
|
419
|
+
heading
|
|
420
|
+
);
|
|
421
|
+
if (!extracted)
|
|
422
|
+
throw new Error(
|
|
423
|
+
`Cannot find section ${heading} in ${file}, make sure you have encapsulated the section in a <section id="${heading}"> tag, or a :::section directive with remark-directive configured.`
|
|
424
|
+
);
|
|
425
|
+
mdast = extracted;
|
|
426
|
+
} else {
|
|
427
|
+
mdast = await baseProcessor.run(mdast);
|
|
428
|
+
}
|
|
429
|
+
await update(mdast, path2.dirname(file), data);
|
|
430
|
+
return mdast;
|
|
431
|
+
};
|
|
432
|
+
async function update(tree, directory, data) {
|
|
433
|
+
const queue = [];
|
|
434
|
+
(0, import_unist_util_visit2.visit)(tree, ElementLikeTypes, (_node, _, parent) => {
|
|
435
|
+
const node = _node;
|
|
436
|
+
if (node.name !== TagName) return;
|
|
437
|
+
const specifier = flattenNode(node);
|
|
438
|
+
if (specifier.length === 0) return "skip";
|
|
439
|
+
const attributes = parseElementAttributes(node);
|
|
440
|
+
const { file: relativePath, section } = parseSpecifier(specifier);
|
|
441
|
+
const file = path2.resolve(
|
|
442
|
+
"cwd" in attributes ? process.cwd() : directory,
|
|
443
|
+
relativePath
|
|
444
|
+
);
|
|
445
|
+
queue.push(
|
|
446
|
+
embedContent(file, section, attributes, data).then((replace) => {
|
|
447
|
+
Object.assign(
|
|
448
|
+
parent && parent.type === "paragraph" ? parent : node,
|
|
449
|
+
replace
|
|
450
|
+
);
|
|
451
|
+
})
|
|
452
|
+
);
|
|
453
|
+
return "skip";
|
|
454
|
+
});
|
|
455
|
+
await Promise.all(queue);
|
|
456
|
+
}
|
|
457
|
+
return async (tree, file) => {
|
|
458
|
+
await update(tree, path2.dirname(file.path), file.data);
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
// src/loaders/mdx/remark-postprocess.ts
|
|
463
|
+
var import_unist_util_visit3 = require("unist-util-visit");
|
|
464
|
+
var import_mdast_util_to_markdown = require("mdast-util-to-markdown");
|
|
465
|
+
var import_estree_util_value_to_estree = require("estree-util-value-to-estree");
|
|
466
|
+
var import_unist_util_remove_position = require("unist-util-remove-position");
|
|
467
|
+
var import_remark_mdx = __toESM(require("remark-mdx"), 1);
|
|
468
|
+
function remarkPostprocess({
|
|
469
|
+
_format,
|
|
470
|
+
includeProcessedMarkdown = false,
|
|
471
|
+
includeMDAST = false,
|
|
472
|
+
valueToExport = []
|
|
473
|
+
}) {
|
|
474
|
+
let _stringifyProcessor;
|
|
475
|
+
const getStringifyProcessor = () => {
|
|
476
|
+
if (_format === "mdx") return this;
|
|
477
|
+
return _stringifyProcessor ??= this().use(import_remark_mdx.default).freeze();
|
|
478
|
+
};
|
|
479
|
+
return (tree, file) => {
|
|
480
|
+
let title;
|
|
481
|
+
const urls = [];
|
|
482
|
+
(0, import_unist_util_visit3.visit)(tree, ["heading", "link"], (node) => {
|
|
483
|
+
if (node.type === "heading" && node.depth === 1) {
|
|
484
|
+
title = flattenNode2(node);
|
|
485
|
+
}
|
|
486
|
+
if (node.type !== "link") return;
|
|
487
|
+
urls.push({
|
|
488
|
+
href: node.url
|
|
489
|
+
});
|
|
490
|
+
return "skip";
|
|
491
|
+
});
|
|
492
|
+
if (title) {
|
|
493
|
+
file.data.frontmatter ??= {};
|
|
494
|
+
if (!file.data.frontmatter.title) file.data.frontmatter.title = title;
|
|
495
|
+
}
|
|
496
|
+
file.data.extractedReferences = urls;
|
|
497
|
+
if (includeProcessedMarkdown) {
|
|
498
|
+
const processor = getStringifyProcessor();
|
|
499
|
+
file.data._markdown = (0, import_mdast_util_to_markdown.toMarkdown)(tree, {
|
|
500
|
+
...processor.data("settings"),
|
|
501
|
+
// from https://github.com/remarkjs/remark/blob/main/packages/remark-stringify/lib/index.js
|
|
502
|
+
extensions: processor.data("toMarkdownExtensions") || []
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
if (includeMDAST) {
|
|
506
|
+
const options = includeMDAST === true ? {} : includeMDAST;
|
|
507
|
+
file.data._mdast = JSON.stringify(
|
|
508
|
+
options.removePosition ? (0, import_unist_util_remove_position.removePosition)(structuredClone(tree)) : tree
|
|
509
|
+
);
|
|
510
|
+
}
|
|
511
|
+
for (const { name, value } of file.data["mdx-export"] ?? []) {
|
|
512
|
+
tree.children.unshift(getMdastExport(name, value));
|
|
513
|
+
}
|
|
514
|
+
for (const name of valueToExport) {
|
|
515
|
+
if (!(name in file.data)) continue;
|
|
516
|
+
tree.children.unshift(getMdastExport(name, file.data[name]));
|
|
517
|
+
}
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
function getMdastExport(name, value) {
|
|
521
|
+
return {
|
|
522
|
+
type: "mdxjsEsm",
|
|
523
|
+
value: "",
|
|
524
|
+
data: {
|
|
525
|
+
estree: {
|
|
526
|
+
type: "Program",
|
|
527
|
+
sourceType: "module",
|
|
528
|
+
body: [
|
|
529
|
+
{
|
|
530
|
+
type: "ExportNamedDeclaration",
|
|
531
|
+
attributes: [],
|
|
532
|
+
specifiers: [],
|
|
533
|
+
source: null,
|
|
534
|
+
declaration: {
|
|
535
|
+
type: "VariableDeclaration",
|
|
536
|
+
kind: "let",
|
|
537
|
+
declarations: [
|
|
538
|
+
{
|
|
539
|
+
type: "VariableDeclarator",
|
|
540
|
+
id: {
|
|
541
|
+
type: "Identifier",
|
|
542
|
+
name
|
|
543
|
+
},
|
|
544
|
+
init: (0, import_estree_util_value_to_estree.valueToEstree)(value)
|
|
545
|
+
}
|
|
546
|
+
]
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
]
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
function flattenNode2(node) {
|
|
555
|
+
if ("children" in node)
|
|
556
|
+
return node.children.map((child) => flattenNode2(child)).join("");
|
|
557
|
+
if ("value" in node) return node.value;
|
|
558
|
+
return "";
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
// src/loaders/mdx/build-mdx.ts
|
|
562
|
+
var cache2 = /* @__PURE__ */ new Map();
|
|
563
|
+
async function buildMDX(cacheKey, source, options) {
|
|
564
|
+
const { filePath, frontmatter, data, _compiler, ...rest } = options;
|
|
565
|
+
function getProcessor(format) {
|
|
566
|
+
const key = `${cacheKey}:${format}`;
|
|
567
|
+
let processor = cache2.get(key);
|
|
568
|
+
if (!processor) {
|
|
569
|
+
processor = (0, import_mdx.createProcessor)({
|
|
570
|
+
outputFormat: "program",
|
|
571
|
+
...rest,
|
|
572
|
+
remarkPlugins: [
|
|
573
|
+
remarkInclude,
|
|
574
|
+
...rest.remarkPlugins ?? [],
|
|
575
|
+
[
|
|
576
|
+
remarkPostprocess,
|
|
577
|
+
{
|
|
578
|
+
_format: format,
|
|
579
|
+
...options.postprocess,
|
|
580
|
+
valueToExport: [
|
|
581
|
+
...options.postprocess?.valueToExport ?? [],
|
|
582
|
+
"structuredData",
|
|
583
|
+
"extractedReferences",
|
|
584
|
+
"frontmatter",
|
|
585
|
+
"lastModified",
|
|
586
|
+
"_markdown",
|
|
587
|
+
"_mdast"
|
|
588
|
+
]
|
|
589
|
+
}
|
|
590
|
+
]
|
|
591
|
+
],
|
|
592
|
+
format
|
|
593
|
+
});
|
|
594
|
+
cache2.set(key, processor);
|
|
595
|
+
}
|
|
596
|
+
return processor;
|
|
597
|
+
}
|
|
598
|
+
return getProcessor(
|
|
599
|
+
options.format ?? (filePath.endsWith(".mdx") ? "mdx" : "md")
|
|
600
|
+
).process({
|
|
601
|
+
value: source,
|
|
602
|
+
path: filePath,
|
|
603
|
+
data: {
|
|
604
|
+
...data,
|
|
605
|
+
frontmatter,
|
|
606
|
+
_compiler,
|
|
607
|
+
_getProcessor: getProcessor
|
|
608
|
+
}
|
|
609
|
+
});
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
// src/loaders/mdx/index.ts
|
|
613
|
+
var import_zod = require("zod");
|
|
614
|
+
var import_promises = __toESM(require("fs/promises"), 1);
|
|
615
|
+
var import_node_path2 = __toESM(require("path"), 1);
|
|
616
|
+
var import_node_crypto = require("crypto");
|
|
617
|
+
var querySchema = import_zod.z.object({
|
|
618
|
+
only: import_zod.z.literal(["frontmatter", "all"]).default("all"),
|
|
619
|
+
collection: import_zod.z.string().optional()
|
|
620
|
+
}).loose();
|
|
621
|
+
var cacheEntry = import_zod.z.object({
|
|
622
|
+
code: import_zod.z.string(),
|
|
623
|
+
map: import_zod.z.any().optional(),
|
|
624
|
+
hash: import_zod.z.string().optional()
|
|
625
|
+
});
|
|
626
|
+
function createMdxLoader(configLoader) {
|
|
627
|
+
return async ({
|
|
628
|
+
source: value,
|
|
629
|
+
development: isDevelopment,
|
|
630
|
+
query,
|
|
631
|
+
compiler,
|
|
632
|
+
filePath
|
|
633
|
+
}) => {
|
|
634
|
+
const matter = fumaMatter(value);
|
|
635
|
+
const parsed = querySchema.parse(query);
|
|
636
|
+
const config = await configLoader.getConfig();
|
|
637
|
+
let after;
|
|
638
|
+
if (!isDevelopment && config.global.experimentalBuildCache) {
|
|
639
|
+
const cacheDir = config.global.experimentalBuildCache;
|
|
640
|
+
const cacheKey = `${parsed.hash}_${parsed.collection ?? "global"}_${generateCacheHash(filePath)}`;
|
|
641
|
+
const cached = await import_promises.default.readFile(import_node_path2.default.join(cacheDir, cacheKey)).then((content) => cacheEntry.parse(JSON.parse(content.toString()))).catch(() => null);
|
|
642
|
+
if (cached && cached.hash === generateCacheHash(value)) return cached;
|
|
643
|
+
after = async () => {
|
|
644
|
+
await import_promises.default.mkdir(cacheDir, { recursive: true });
|
|
645
|
+
await import_promises.default.writeFile(
|
|
646
|
+
import_node_path2.default.join(cacheDir, cacheKey),
|
|
647
|
+
JSON.stringify({
|
|
648
|
+
...out,
|
|
649
|
+
hash: generateCacheHash(value)
|
|
650
|
+
})
|
|
651
|
+
);
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
const collection = parsed.collection ? config.collections.get(parsed.collection) : void 0;
|
|
655
|
+
let docCollection;
|
|
656
|
+
switch (collection?.type) {
|
|
657
|
+
case "doc":
|
|
658
|
+
docCollection = collection;
|
|
659
|
+
break;
|
|
660
|
+
case "docs":
|
|
661
|
+
docCollection = collection.docs;
|
|
662
|
+
break;
|
|
663
|
+
}
|
|
664
|
+
if (docCollection?.schema) {
|
|
665
|
+
matter.data = await validate(
|
|
666
|
+
docCollection.schema,
|
|
667
|
+
matter.data,
|
|
668
|
+
{
|
|
669
|
+
source: value,
|
|
670
|
+
path: filePath
|
|
671
|
+
},
|
|
672
|
+
`invalid frontmatter in ${filePath}`
|
|
673
|
+
);
|
|
674
|
+
}
|
|
675
|
+
if (parsed.only === "frontmatter") {
|
|
676
|
+
return {
|
|
677
|
+
code: `export const frontmatter = ${JSON.stringify(matter.data)}`,
|
|
678
|
+
map: null
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
const data = {};
|
|
682
|
+
if (config.global.lastModifiedTime === "git") {
|
|
683
|
+
data.lastModified = (await getGitTimestamp(filePath))?.getTime();
|
|
684
|
+
}
|
|
685
|
+
const lineOffset = isDevelopment ? countLines(matter.matter) : 0;
|
|
686
|
+
const compiled = await buildMDX(
|
|
687
|
+
`${getConfigHash(config)}:${parsed.collection ?? "global"}`,
|
|
688
|
+
"\n".repeat(lineOffset) + matter.content,
|
|
689
|
+
{
|
|
690
|
+
development: isDevelopment,
|
|
691
|
+
...docCollection?.mdxOptions ?? await config.getDefaultMDXOptions(),
|
|
692
|
+
postprocess: docCollection?.postprocess,
|
|
693
|
+
data,
|
|
694
|
+
filePath,
|
|
695
|
+
frontmatter: matter.data,
|
|
696
|
+
_compiler: compiler
|
|
697
|
+
}
|
|
698
|
+
);
|
|
699
|
+
const out = {
|
|
700
|
+
code: String(compiled.value),
|
|
701
|
+
map: compiled.map
|
|
702
|
+
};
|
|
703
|
+
await after?.();
|
|
704
|
+
return out;
|
|
705
|
+
};
|
|
706
|
+
}
|
|
707
|
+
var hashes = /* @__PURE__ */ new WeakMap();
|
|
708
|
+
function getConfigHash(config) {
|
|
709
|
+
let hash = hashes.get(config);
|
|
710
|
+
if (hash) return hash;
|
|
711
|
+
hash = Date.now().toString();
|
|
712
|
+
hashes.set(config, hash);
|
|
713
|
+
return hash;
|
|
714
|
+
}
|
|
715
|
+
function generateCacheHash(input) {
|
|
716
|
+
return (0, import_node_crypto.createHash)("md5").update(input).digest("hex");
|
|
717
|
+
}
|
|
718
|
+
function countLines(s) {
|
|
719
|
+
let num = 0;
|
|
720
|
+
for (const c of s) {
|
|
721
|
+
if (c === "\n") num++;
|
|
722
|
+
}
|
|
723
|
+
return num;
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
// src/loaders/config/index.ts
|
|
727
|
+
var import_node_path3 = __toESM(require("path"), 1);
|
|
728
|
+
var import_promises2 = __toESM(require("fs/promises"), 1);
|
|
729
|
+
function findConfigFile() {
|
|
730
|
+
return import_node_path3.default.resolve("source.config.ts");
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
// src/bun/index.ts
|
|
734
|
+
init_build();
|
|
735
|
+
var import_node_querystring = require("querystring");
|
|
736
|
+
var import_node_url = require("url");
|
|
737
|
+
|
|
738
|
+
// src/core.ts
|
|
739
|
+
var import_node_path4 = __toESM(require("path"), 1);
|
|
740
|
+
var import_promises3 = __toESM(require("fs/promises"), 1);
|
|
741
|
+
function createCore(options, defaultPlugins = []) {
|
|
742
|
+
let config;
|
|
743
|
+
let plugins2;
|
|
744
|
+
return {
|
|
745
|
+
_options: options,
|
|
746
|
+
getPluginContext() {
|
|
747
|
+
return {
|
|
748
|
+
core: this,
|
|
749
|
+
...options
|
|
750
|
+
};
|
|
751
|
+
},
|
|
752
|
+
/**
|
|
753
|
+
* Convenient cache store, reset when config changes
|
|
754
|
+
*/
|
|
755
|
+
cache: /* @__PURE__ */ new Map(),
|
|
756
|
+
async init({ config: newConfig }) {
|
|
757
|
+
config = await newConfig;
|
|
758
|
+
this.cache.clear();
|
|
759
|
+
plugins2 = [];
|
|
760
|
+
for await (const option of [
|
|
761
|
+
...defaultPlugins,
|
|
762
|
+
...config.global.plugins ?? []
|
|
763
|
+
]) {
|
|
764
|
+
if (!option) continue;
|
|
765
|
+
if (Array.isArray(option)) plugins2.push(...option);
|
|
766
|
+
else plugins2.push(option);
|
|
767
|
+
}
|
|
768
|
+
for (const plugin of plugins2) {
|
|
769
|
+
const out = await plugin.config?.call(this.getPluginContext(), config);
|
|
770
|
+
if (out) config = out;
|
|
771
|
+
}
|
|
772
|
+
return this;
|
|
773
|
+
},
|
|
774
|
+
getConfig() {
|
|
775
|
+
return config;
|
|
776
|
+
},
|
|
777
|
+
creatConfigLoader() {
|
|
778
|
+
return {
|
|
779
|
+
getConfig() {
|
|
780
|
+
return config;
|
|
781
|
+
}
|
|
782
|
+
};
|
|
783
|
+
},
|
|
784
|
+
async initServer(server) {
|
|
785
|
+
for (const plugin of plugins2) {
|
|
786
|
+
await plugin.configureServer?.call(this.getPluginContext(), server);
|
|
787
|
+
}
|
|
788
|
+
},
|
|
789
|
+
async emitAndWrite({
|
|
790
|
+
filterPlugin = () => true
|
|
791
|
+
} = {}) {
|
|
792
|
+
const start = performance.now();
|
|
793
|
+
const out = await Promise.all(
|
|
794
|
+
plugins2.map((plugin) => {
|
|
795
|
+
if (!filterPlugin(plugin) || !plugin.emit) return [];
|
|
796
|
+
return plugin.emit.call(this.getPluginContext());
|
|
797
|
+
})
|
|
798
|
+
);
|
|
799
|
+
await Promise.all(
|
|
800
|
+
out.flat().map(async (entry) => {
|
|
801
|
+
const file = import_node_path4.default.join(options.outDir, entry.path);
|
|
802
|
+
await import_promises3.default.mkdir(import_node_path4.default.dirname(file), { recursive: true });
|
|
803
|
+
await import_promises3.default.writeFile(file, entry.content);
|
|
804
|
+
})
|
|
805
|
+
);
|
|
806
|
+
console.log(`[MDX] generated files in ${performance.now() - start}ms`);
|
|
807
|
+
}
|
|
808
|
+
};
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
// src/bun/index.ts
|
|
812
|
+
function createMdxPlugin(options = {}) {
|
|
813
|
+
const {
|
|
814
|
+
environment = "bun",
|
|
815
|
+
outDir = ".source",
|
|
816
|
+
configPath = findConfigFile()
|
|
817
|
+
} = options;
|
|
818
|
+
async function getMdxLoader() {
|
|
819
|
+
const importPath = (0, import_node_url.pathToFileURL)(configPath).href;
|
|
820
|
+
const core = await createCore({
|
|
821
|
+
environment,
|
|
822
|
+
outDir,
|
|
823
|
+
configPath
|
|
824
|
+
}).init({
|
|
825
|
+
config: buildConfig(await import(importPath))
|
|
826
|
+
});
|
|
827
|
+
return createMdxLoader(core.creatConfigLoader());
|
|
828
|
+
}
|
|
829
|
+
return {
|
|
830
|
+
name: "bun-plugin-fumadocs-mdx",
|
|
831
|
+
setup(build) {
|
|
832
|
+
const mdxLoader = getMdxLoader();
|
|
833
|
+
build.onLoad({ filter: /\.mdx(\?.+?)?$/ }, async (args) => {
|
|
834
|
+
const [filePath, query] = args.path.split("?", 2);
|
|
835
|
+
const content = await Bun.file(filePath).text();
|
|
836
|
+
const result = await (await mdxLoader)({
|
|
837
|
+
source: content,
|
|
838
|
+
query: (0, import_node_querystring.parse)(query),
|
|
839
|
+
filePath,
|
|
840
|
+
development: false,
|
|
841
|
+
compiler: {
|
|
842
|
+
addDependency() {
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
});
|
|
846
|
+
return {
|
|
847
|
+
contents: result.code,
|
|
848
|
+
loader: "js"
|
|
849
|
+
};
|
|
850
|
+
});
|
|
851
|
+
}
|
|
852
|
+
};
|
|
853
|
+
}
|
|
854
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
855
|
+
0 && (module.exports = {
|
|
856
|
+
createMdxPlugin
|
|
857
|
+
});
|