fumadocs-mdx 11.9.0 → 11.10.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/dist/{runtime/vite.d.ts → browser-B2G8uAF2.d.cts} +9 -35
- package/dist/{runtime/vite.d.cts → browser-DrH7tKRi.d.ts} +9 -35
- package/dist/bun/index.cjs +667 -0
- package/dist/bun/index.d.cts +8 -0
- package/dist/bun/index.d.ts +8 -0
- package/dist/bun/index.js +50 -0
- package/dist/{chunk-766EAFX6.js → chunk-2HKRTQYP.js} +64 -0
- package/dist/chunk-46UPKP5R.js +77 -0
- package/dist/chunk-5XJM5RPV.js +149 -0
- package/dist/chunk-NVX3U5YE.js +82 -0
- package/dist/config/index.d.cts +1 -1
- package/dist/config/index.d.ts +1 -1
- package/dist/config/zod-3.d.cts +1 -1
- package/dist/config/zod-3.d.ts +1 -1
- package/dist/{define-DnJzAZrj.d.ts → define-BH4bnHQl.d.cts} +6 -0
- package/dist/{define-DnJzAZrj.d.cts → define-BH4bnHQl.d.ts} +6 -0
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/loader-mdx.cjs +345 -265
- package/dist/loader-mdx.js +11 -80
- package/dist/next/index.js +4 -6
- package/dist/node/loader.cjs +748 -0
- package/dist/node/loader.d.cts +5 -0
- package/dist/node/loader.d.ts +5 -0
- package/dist/node/loader.js +23 -0
- package/dist/runtime/async.d.cts +2 -2
- package/dist/runtime/async.d.ts +2 -2
- package/dist/runtime/vite/browser.cjs +103 -0
- package/dist/runtime/vite/browser.d.cts +11 -0
- package/dist/runtime/vite/browser.d.ts +11 -0
- package/dist/runtime/vite/browser.js +10 -0
- package/dist/runtime/{vite.cjs → vite/server.cjs} +77 -63
- package/dist/runtime/vite/server.d.cts +41 -0
- package/dist/runtime/vite/server.d.ts +41 -0
- package/dist/runtime/{vite.js → vite/server.js} +10 -64
- package/dist/{types-WSHJKA8L.d.ts → types-DN9KrG7R.d.ts} +1 -1
- package/dist/{types-BmVgoqsr.d.cts → types-DT83Ijs6.d.cts} +1 -1
- package/dist/vite/index.cjs +356 -282
- package/dist/vite/index.js +12 -78
- package/package.json +21 -11
- package/dist/chunk-GX3THK2Q.js +0 -66
- package/dist/chunk-UCY7OBZG.js +0 -12
|
@@ -0,0 +1,667 @@
|
|
|
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/mdx-plugins/remark-postprocess.ts
|
|
34
|
+
function remarkPostprocess({
|
|
35
|
+
injectExports
|
|
36
|
+
}) {
|
|
37
|
+
return (tree, file) => {
|
|
38
|
+
let title;
|
|
39
|
+
const urls = [];
|
|
40
|
+
(0, import_unist_util_visit2.visit)(tree, ["heading", "link"], (node) => {
|
|
41
|
+
if (node.type === "heading" && node.depth === 1) {
|
|
42
|
+
title = flattenNode2(node);
|
|
43
|
+
}
|
|
44
|
+
if (node.type !== "link") return;
|
|
45
|
+
urls.push({
|
|
46
|
+
href: node.url
|
|
47
|
+
});
|
|
48
|
+
return "skip";
|
|
49
|
+
});
|
|
50
|
+
if (title) {
|
|
51
|
+
file.data.frontmatter ??= {};
|
|
52
|
+
if (!file.data.frontmatter.title) file.data.frontmatter.title = title;
|
|
53
|
+
}
|
|
54
|
+
file.data.extractedReferences = urls;
|
|
55
|
+
for (const name of injectExports) {
|
|
56
|
+
if (!(name in file.data)) continue;
|
|
57
|
+
tree.children.unshift(getMdastExport(name, file.data[name]));
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function flattenNode2(node) {
|
|
62
|
+
if ("children" in node)
|
|
63
|
+
return node.children.map((child) => flattenNode2(child)).join("");
|
|
64
|
+
if ("value" in node) return node.value;
|
|
65
|
+
return "";
|
|
66
|
+
}
|
|
67
|
+
function getMdastExport(name, value) {
|
|
68
|
+
return {
|
|
69
|
+
type: "mdxjsEsm",
|
|
70
|
+
value: "",
|
|
71
|
+
data: {
|
|
72
|
+
estree: {
|
|
73
|
+
type: "Program",
|
|
74
|
+
sourceType: "module",
|
|
75
|
+
body: [
|
|
76
|
+
{
|
|
77
|
+
type: "ExportNamedDeclaration",
|
|
78
|
+
attributes: [],
|
|
79
|
+
specifiers: [],
|
|
80
|
+
source: null,
|
|
81
|
+
declaration: {
|
|
82
|
+
type: "VariableDeclaration",
|
|
83
|
+
kind: "let",
|
|
84
|
+
declarations: [
|
|
85
|
+
{
|
|
86
|
+
type: "VariableDeclarator",
|
|
87
|
+
id: {
|
|
88
|
+
type: "Identifier",
|
|
89
|
+
name
|
|
90
|
+
},
|
|
91
|
+
init: (0, import_estree_util_value_to_estree.valueToEstree)(value)
|
|
92
|
+
}
|
|
93
|
+
]
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
]
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
var import_unist_util_visit2, import_estree_util_value_to_estree;
|
|
102
|
+
var init_remark_postprocess = __esm({
|
|
103
|
+
"src/mdx-plugins/remark-postprocess.ts"() {
|
|
104
|
+
"use strict";
|
|
105
|
+
import_unist_util_visit2 = require("unist-util-visit");
|
|
106
|
+
import_estree_util_value_to_estree = require("estree-util-value-to-estree");
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// src/utils/mdx-options.ts
|
|
111
|
+
var mdx_options_exports = {};
|
|
112
|
+
__export(mdx_options_exports, {
|
|
113
|
+
getDefaultMDXOptions: () => getDefaultMDXOptions
|
|
114
|
+
});
|
|
115
|
+
function pluginOption(def, options = []) {
|
|
116
|
+
const list = def(Array.isArray(options) ? options : []).filter(
|
|
117
|
+
Boolean
|
|
118
|
+
);
|
|
119
|
+
if (typeof options === "function") {
|
|
120
|
+
return options(list);
|
|
121
|
+
}
|
|
122
|
+
return list;
|
|
123
|
+
}
|
|
124
|
+
function getDefaultMDXOptions({
|
|
125
|
+
valueToExport = [],
|
|
126
|
+
rehypeCodeOptions,
|
|
127
|
+
remarkImageOptions,
|
|
128
|
+
remarkHeadingOptions,
|
|
129
|
+
remarkStructureOptions,
|
|
130
|
+
remarkCodeTabOptions,
|
|
131
|
+
remarkNpmOptions,
|
|
132
|
+
_withoutBundler = false,
|
|
133
|
+
...mdxOptions
|
|
134
|
+
}) {
|
|
135
|
+
const mdxExports = [
|
|
136
|
+
"structuredData",
|
|
137
|
+
"extractedReferences",
|
|
138
|
+
"frontmatter",
|
|
139
|
+
"lastModified",
|
|
140
|
+
...valueToExport
|
|
141
|
+
];
|
|
142
|
+
const remarkPlugins = pluginOption(
|
|
143
|
+
(v) => [
|
|
144
|
+
plugins.remarkGfm,
|
|
145
|
+
[
|
|
146
|
+
plugins.remarkHeading,
|
|
147
|
+
{
|
|
148
|
+
generateToc: false,
|
|
149
|
+
...remarkHeadingOptions
|
|
150
|
+
}
|
|
151
|
+
],
|
|
152
|
+
remarkImageOptions !== false && [
|
|
153
|
+
plugins.remarkImage,
|
|
154
|
+
{
|
|
155
|
+
...remarkImageOptions,
|
|
156
|
+
useImport: _withoutBundler ? false : remarkImageOptions?.useImport
|
|
157
|
+
}
|
|
158
|
+
],
|
|
159
|
+
"remarkCodeTab" in plugins && remarkCodeTabOptions !== false && [
|
|
160
|
+
plugins.remarkCodeTab,
|
|
161
|
+
remarkCodeTabOptions
|
|
162
|
+
],
|
|
163
|
+
"remarkNpm" in plugins && remarkNpmOptions !== false && [plugins.remarkNpm, remarkNpmOptions],
|
|
164
|
+
...v,
|
|
165
|
+
remarkStructureOptions !== false && [
|
|
166
|
+
plugins.remarkStructure,
|
|
167
|
+
remarkStructureOptions
|
|
168
|
+
],
|
|
169
|
+
[
|
|
170
|
+
remarkPostprocess,
|
|
171
|
+
{ injectExports: mdxExports }
|
|
172
|
+
]
|
|
173
|
+
],
|
|
174
|
+
mdxOptions.remarkPlugins
|
|
175
|
+
);
|
|
176
|
+
const rehypePlugins = pluginOption(
|
|
177
|
+
(v) => [
|
|
178
|
+
rehypeCodeOptions !== false && [plugins.rehypeCode, rehypeCodeOptions],
|
|
179
|
+
...v,
|
|
180
|
+
plugins.rehypeToc
|
|
181
|
+
],
|
|
182
|
+
mdxOptions.rehypePlugins
|
|
183
|
+
);
|
|
184
|
+
return {
|
|
185
|
+
...mdxOptions,
|
|
186
|
+
outputFormat: _withoutBundler ? "function-body" : mdxOptions.outputFormat,
|
|
187
|
+
remarkPlugins,
|
|
188
|
+
rehypePlugins
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
var plugins;
|
|
192
|
+
var init_mdx_options = __esm({
|
|
193
|
+
"src/utils/mdx-options.ts"() {
|
|
194
|
+
"use strict";
|
|
195
|
+
plugins = __toESM(require("fumadocs-core/mdx-plugins"), 1);
|
|
196
|
+
init_remark_postprocess();
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
// src/bun/index.ts
|
|
201
|
+
var bun_exports = {};
|
|
202
|
+
__export(bun_exports, {
|
|
203
|
+
createMdxPlugin: () => createMdxPlugin
|
|
204
|
+
});
|
|
205
|
+
module.exports = __toCommonJS(bun_exports);
|
|
206
|
+
|
|
207
|
+
// src/utils/fuma-matter.ts
|
|
208
|
+
var import_js_yaml = require("js-yaml");
|
|
209
|
+
var regex = /^---\r?\n(.+?)\r?\n---\r?\n/s;
|
|
210
|
+
function fumaMatter(input) {
|
|
211
|
+
const output = { matter: "", data: {}, content: input };
|
|
212
|
+
const match = regex.exec(input);
|
|
213
|
+
if (!match) {
|
|
214
|
+
return output;
|
|
215
|
+
}
|
|
216
|
+
output.matter = match[0];
|
|
217
|
+
output.content = input.slice(match[0].length);
|
|
218
|
+
const loaded = (0, import_js_yaml.load)(match[1]);
|
|
219
|
+
output.data = loaded ?? {};
|
|
220
|
+
return output;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// src/utils/validation.ts
|
|
224
|
+
var import_picocolors = __toESM(require("picocolors"), 1);
|
|
225
|
+
var ValidationError = class extends Error {
|
|
226
|
+
constructor(message, issues) {
|
|
227
|
+
super(
|
|
228
|
+
`${message}:
|
|
229
|
+
${issues.map((issue) => ` ${issue.path}: ${issue.message}`).join("\n")}`
|
|
230
|
+
);
|
|
231
|
+
this.title = message;
|
|
232
|
+
this.issues = issues;
|
|
233
|
+
}
|
|
234
|
+
toStringFormatted() {
|
|
235
|
+
return [
|
|
236
|
+
import_picocolors.default.bold(`[MDX] ${this.title}:`),
|
|
237
|
+
...this.issues.map(
|
|
238
|
+
(issue) => import_picocolors.default.redBright(
|
|
239
|
+
`- ${import_picocolors.default.bold(issue.path?.join(".") ?? "*")}: ${issue.message}`
|
|
240
|
+
)
|
|
241
|
+
)
|
|
242
|
+
].join("\n");
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
async function validate(schema, data, context, errorMessage) {
|
|
246
|
+
if (typeof schema === "function" && !("~standard" in schema)) {
|
|
247
|
+
schema = schema(context);
|
|
248
|
+
}
|
|
249
|
+
if ("~standard" in schema) {
|
|
250
|
+
const result = await schema["~standard"].validate(
|
|
251
|
+
data
|
|
252
|
+
);
|
|
253
|
+
if (result.issues) {
|
|
254
|
+
throw new ValidationError(errorMessage, result.issues);
|
|
255
|
+
}
|
|
256
|
+
return result.value;
|
|
257
|
+
}
|
|
258
|
+
return data;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// src/utils/git-timestamp.ts
|
|
262
|
+
var import_node_path = __toESM(require("path"), 1);
|
|
263
|
+
var import_tinyexec = require("tinyexec");
|
|
264
|
+
var cache = /* @__PURE__ */ new Map();
|
|
265
|
+
async function getGitTimestamp(file) {
|
|
266
|
+
const cached = cache.get(file);
|
|
267
|
+
if (cached) return cached;
|
|
268
|
+
try {
|
|
269
|
+
const out = await (0, import_tinyexec.x)(
|
|
270
|
+
"git",
|
|
271
|
+
["log", "-1", '--pretty="%ai"', import_node_path.default.relative(process.cwd(), file)],
|
|
272
|
+
{
|
|
273
|
+
throwOnError: true
|
|
274
|
+
}
|
|
275
|
+
);
|
|
276
|
+
const time = new Date(out.stdout);
|
|
277
|
+
cache.set(file, time);
|
|
278
|
+
return time;
|
|
279
|
+
} catch {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// src/utils/count-lines.ts
|
|
285
|
+
function countLines(s) {
|
|
286
|
+
let num = 0;
|
|
287
|
+
for (const c of s) {
|
|
288
|
+
if (c === "\n") num++;
|
|
289
|
+
}
|
|
290
|
+
return num;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// src/utils/build-mdx.ts
|
|
294
|
+
var import_mdx = require("@mdx-js/mdx");
|
|
295
|
+
|
|
296
|
+
// src/mdx-plugins/remark-include.ts
|
|
297
|
+
var import_unified = require("unified");
|
|
298
|
+
var import_unist_util_visit = require("unist-util-visit");
|
|
299
|
+
var path2 = __toESM(require("path"), 1);
|
|
300
|
+
var fs = __toESM(require("fs/promises"), 1);
|
|
301
|
+
var import_remark_parse = __toESM(require("remark-parse"), 1);
|
|
302
|
+
var import_remark_mdx = __toESM(require("remark-mdx"), 1);
|
|
303
|
+
var import_mdx_plugins = require("fumadocs-core/mdx-plugins");
|
|
304
|
+
var baseProcessor = (0, import_unified.unified)().use(import_mdx_plugins.remarkHeading);
|
|
305
|
+
function flattenNode(node) {
|
|
306
|
+
if ("children" in node)
|
|
307
|
+
return node.children.map((child) => flattenNode(child)).join("");
|
|
308
|
+
if ("value" in node) return node.value;
|
|
309
|
+
return "";
|
|
310
|
+
}
|
|
311
|
+
function parseSpecifier(specifier) {
|
|
312
|
+
const idx = specifier.lastIndexOf("#");
|
|
313
|
+
if (idx === -1) return { file: specifier };
|
|
314
|
+
return {
|
|
315
|
+
file: specifier.slice(0, idx),
|
|
316
|
+
section: specifier.slice(idx + 1)
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
function extractSection(root, section) {
|
|
320
|
+
let nodes;
|
|
321
|
+
for (const node of root.children) {
|
|
322
|
+
if (node.type === "mdxJsxFlowElement" && node.name === "section" && node.attributes.some(
|
|
323
|
+
(attr) => attr.type === "mdxJsxAttribute" && attr.name === "id" && attr.value === section
|
|
324
|
+
)) {
|
|
325
|
+
nodes = node.children;
|
|
326
|
+
break;
|
|
327
|
+
}
|
|
328
|
+
if (node.type === "heading" && node.data?.hProperties?.id === section) {
|
|
329
|
+
nodes = [node];
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
if (!nodes) continue;
|
|
333
|
+
if (node.type === "heading") break;
|
|
334
|
+
nodes.push(node);
|
|
335
|
+
}
|
|
336
|
+
if (nodes)
|
|
337
|
+
return {
|
|
338
|
+
type: "root",
|
|
339
|
+
children: nodes
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
function remarkInclude() {
|
|
343
|
+
const TagName = "include";
|
|
344
|
+
async function embedContent(file, heading, params, data) {
|
|
345
|
+
let content;
|
|
346
|
+
try {
|
|
347
|
+
content = (await fs.readFile(file)).toString();
|
|
348
|
+
} catch (e) {
|
|
349
|
+
throw new Error(
|
|
350
|
+
`failed to read file ${file}
|
|
351
|
+
${e instanceof Error ? e.message : String(e)}`,
|
|
352
|
+
{ cause: e }
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
const ext = path2.extname(file);
|
|
356
|
+
data._compiler?.addDependency(file);
|
|
357
|
+
if (params.lang || ext !== ".md" && ext !== ".mdx") {
|
|
358
|
+
const lang = params.lang ?? ext.slice(1);
|
|
359
|
+
return {
|
|
360
|
+
type: "code",
|
|
361
|
+
lang,
|
|
362
|
+
meta: params.meta,
|
|
363
|
+
value: content,
|
|
364
|
+
data: {}
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
const processor = (data._getProcessor ?? getDefaultProcessor)(
|
|
368
|
+
ext === ".mdx" ? "mdx" : "md"
|
|
369
|
+
);
|
|
370
|
+
let parsed = await baseProcessor.run(
|
|
371
|
+
processor.parse(fumaMatter(content).content)
|
|
372
|
+
);
|
|
373
|
+
if (heading) {
|
|
374
|
+
const extracted = extractSection(parsed, heading);
|
|
375
|
+
if (!extracted)
|
|
376
|
+
throw new Error(
|
|
377
|
+
`Cannot find section ${heading} in ${file}, make sure you have encapsulated the section in a <section id="${heading}"> tag.`
|
|
378
|
+
);
|
|
379
|
+
parsed = extracted;
|
|
380
|
+
}
|
|
381
|
+
await update(parsed, path2.dirname(file), data);
|
|
382
|
+
return parsed;
|
|
383
|
+
}
|
|
384
|
+
async function update(tree, directory, data) {
|
|
385
|
+
const queue = [];
|
|
386
|
+
(0, import_unist_util_visit.visit)(
|
|
387
|
+
tree,
|
|
388
|
+
["mdxJsxFlowElement", "mdxJsxTextElement"],
|
|
389
|
+
(_node, _, parent) => {
|
|
390
|
+
const node = _node;
|
|
391
|
+
if (node.name !== TagName) return;
|
|
392
|
+
const params = {};
|
|
393
|
+
const specifier = flattenNode(node);
|
|
394
|
+
if (specifier.length === 0) return "skip";
|
|
395
|
+
for (const attr of node.attributes) {
|
|
396
|
+
if (attr.type === "mdxJsxAttribute" && (typeof attr.value === "string" || attr.value === null)) {
|
|
397
|
+
params[attr.name] = attr.value;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
const { file: relativePath, section } = parseSpecifier(specifier);
|
|
401
|
+
const file = path2.resolve(
|
|
402
|
+
"cwd" in params ? process.cwd() : directory,
|
|
403
|
+
relativePath
|
|
404
|
+
);
|
|
405
|
+
queue.push(
|
|
406
|
+
embedContent(file, section, params, data).then((replace) => {
|
|
407
|
+
Object.assign(
|
|
408
|
+
parent && parent.type === "paragraph" ? parent : node,
|
|
409
|
+
replace
|
|
410
|
+
);
|
|
411
|
+
})
|
|
412
|
+
);
|
|
413
|
+
return "skip";
|
|
414
|
+
}
|
|
415
|
+
);
|
|
416
|
+
await Promise.all(queue);
|
|
417
|
+
}
|
|
418
|
+
return async (tree, file) => {
|
|
419
|
+
await update(tree, path2.dirname(file.path), file.data);
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
function getDefaultProcessor(format) {
|
|
423
|
+
const mdProcessor = (0, import_unified.unified)().use(import_remark_parse.default);
|
|
424
|
+
if (format === "md") return mdProcessor;
|
|
425
|
+
return mdProcessor.use(import_remark_mdx.default);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// src/utils/build-mdx.ts
|
|
429
|
+
var cache2 = /* @__PURE__ */ new Map();
|
|
430
|
+
async function buildMDX(cacheKey, source, options) {
|
|
431
|
+
const { filePath, frontmatter, data, _compiler, ...rest } = options;
|
|
432
|
+
function getProcessor(format) {
|
|
433
|
+
const key = `${cacheKey}:${format}`;
|
|
434
|
+
let processor = cache2.get(key);
|
|
435
|
+
if (!processor) {
|
|
436
|
+
processor = (0, import_mdx.createProcessor)({
|
|
437
|
+
outputFormat: "program",
|
|
438
|
+
...rest,
|
|
439
|
+
remarkPlugins: [remarkInclude, ...rest.remarkPlugins ?? []],
|
|
440
|
+
format
|
|
441
|
+
});
|
|
442
|
+
cache2.set(key, processor);
|
|
443
|
+
}
|
|
444
|
+
return processor;
|
|
445
|
+
}
|
|
446
|
+
return getProcessor(
|
|
447
|
+
options.format ?? filePath.endsWith(".mdx") ? "mdx" : "md"
|
|
448
|
+
).process({
|
|
449
|
+
value: source,
|
|
450
|
+
path: filePath,
|
|
451
|
+
data: {
|
|
452
|
+
...data,
|
|
453
|
+
frontmatter,
|
|
454
|
+
_compiler,
|
|
455
|
+
_getProcessor: getProcessor
|
|
456
|
+
}
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// src/loaders/mdx.ts
|
|
461
|
+
var import_zod = require("zod");
|
|
462
|
+
var import_promises = __toESM(require("fs/promises"), 1);
|
|
463
|
+
var import_node_path2 = __toESM(require("path"), 1);
|
|
464
|
+
var import_node_crypto = require("crypto");
|
|
465
|
+
var querySchema = import_zod.z.object({
|
|
466
|
+
only: import_zod.z.literal(["frontmatter", "all"]).default("all"),
|
|
467
|
+
collection: import_zod.z.string().optional(),
|
|
468
|
+
hash: import_zod.z.string().describe(
|
|
469
|
+
"the hash of config, used for revalidation on Turbopack/Webpack."
|
|
470
|
+
).optional()
|
|
471
|
+
}).loose();
|
|
472
|
+
var cacheEntry = import_zod.z.object({
|
|
473
|
+
code: import_zod.z.string(),
|
|
474
|
+
map: import_zod.z.any().optional(),
|
|
475
|
+
hash: import_zod.z.string().optional()
|
|
476
|
+
});
|
|
477
|
+
function createMdxLoader(configLoader) {
|
|
478
|
+
return async ({
|
|
479
|
+
source: value,
|
|
480
|
+
development: isDevelopment,
|
|
481
|
+
query,
|
|
482
|
+
compiler,
|
|
483
|
+
filePath
|
|
484
|
+
}) => {
|
|
485
|
+
const matter = fumaMatter(value);
|
|
486
|
+
const parsed = querySchema.parse(query);
|
|
487
|
+
const loaded = await configLoader.getConfig(parsed.hash);
|
|
488
|
+
const cacheDir = isDevelopment ? void 0 : loaded.global.experimentalBuildCache;
|
|
489
|
+
const cacheKey = `${parsed.hash}_${parsed.collection ?? "global"}_${generateCacheHash(filePath)}`;
|
|
490
|
+
if (cacheDir) {
|
|
491
|
+
const cached = await import_promises.default.readFile(import_node_path2.default.join(cacheDir, cacheKey)).then((content) => cacheEntry.parse(JSON.parse(content.toString()))).catch(() => null);
|
|
492
|
+
if (cached && cached.hash === generateCacheHash(value)) return cached;
|
|
493
|
+
}
|
|
494
|
+
const collection = parsed.collection ? loaded.collections.get(parsed.collection) : void 0;
|
|
495
|
+
let schema;
|
|
496
|
+
let mdxOptions;
|
|
497
|
+
switch (collection?.type) {
|
|
498
|
+
case "doc":
|
|
499
|
+
mdxOptions = collection.mdxOptions;
|
|
500
|
+
schema = collection.schema;
|
|
501
|
+
break;
|
|
502
|
+
case "docs":
|
|
503
|
+
mdxOptions = collection.docs.mdxOptions;
|
|
504
|
+
schema = collection.docs.schema;
|
|
505
|
+
break;
|
|
506
|
+
}
|
|
507
|
+
if (schema) {
|
|
508
|
+
matter.data = await validate(
|
|
509
|
+
schema,
|
|
510
|
+
matter.data,
|
|
511
|
+
{
|
|
512
|
+
source: value,
|
|
513
|
+
path: filePath
|
|
514
|
+
},
|
|
515
|
+
`invalid frontmatter in ${filePath}`
|
|
516
|
+
);
|
|
517
|
+
}
|
|
518
|
+
if (parsed.only === "frontmatter") {
|
|
519
|
+
return {
|
|
520
|
+
code: `export const frontmatter = ${JSON.stringify(matter.data)}`,
|
|
521
|
+
map: null
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
const data = {};
|
|
525
|
+
if (loaded.global.lastModifiedTime === "git") {
|
|
526
|
+
data.lastModified = (await getGitTimestamp(filePath))?.getTime();
|
|
527
|
+
}
|
|
528
|
+
const lineOffset = isDevelopment ? countLines(matter.matter) : 0;
|
|
529
|
+
const compiled = await buildMDX(
|
|
530
|
+
`${parsed.hash ?? ""}:${parsed.collection ?? "global"}`,
|
|
531
|
+
"\n".repeat(lineOffset) + matter.content,
|
|
532
|
+
{
|
|
533
|
+
development: isDevelopment,
|
|
534
|
+
...mdxOptions ?? await loaded.getDefaultMDXOptions(),
|
|
535
|
+
data,
|
|
536
|
+
filePath,
|
|
537
|
+
frontmatter: matter.data,
|
|
538
|
+
_compiler: compiler
|
|
539
|
+
}
|
|
540
|
+
);
|
|
541
|
+
const out = {
|
|
542
|
+
code: String(compiled.value),
|
|
543
|
+
map: compiled.map
|
|
544
|
+
};
|
|
545
|
+
if (cacheDir) {
|
|
546
|
+
await import_promises.default.mkdir(cacheDir, { recursive: true });
|
|
547
|
+
await import_promises.default.writeFile(
|
|
548
|
+
import_node_path2.default.join(cacheDir, cacheKey),
|
|
549
|
+
JSON.stringify({
|
|
550
|
+
...out,
|
|
551
|
+
hash: generateCacheHash(value)
|
|
552
|
+
})
|
|
553
|
+
);
|
|
554
|
+
}
|
|
555
|
+
return out;
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
function generateCacheHash(input) {
|
|
559
|
+
return (0, import_node_crypto.createHash)("md5").update(input).digest("hex");
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
// src/utils/config.ts
|
|
563
|
+
var fs3 = __toESM(require("fs/promises"), 1);
|
|
564
|
+
var path4 = __toESM(require("path"), 1);
|
|
565
|
+
var import_node_url = require("url");
|
|
566
|
+
|
|
567
|
+
// src/config/build.ts
|
|
568
|
+
function buildConfig(config) {
|
|
569
|
+
const collections = /* @__PURE__ */ new Map();
|
|
570
|
+
let globalConfig = {};
|
|
571
|
+
for (const [k, v] of Object.entries(config)) {
|
|
572
|
+
if (!v) {
|
|
573
|
+
continue;
|
|
574
|
+
}
|
|
575
|
+
if (typeof v === "object" && "type" in v) {
|
|
576
|
+
if (v.type === "docs") {
|
|
577
|
+
collections.set(k, v);
|
|
578
|
+
continue;
|
|
579
|
+
}
|
|
580
|
+
if (v.type === "doc" || v.type === "meta") {
|
|
581
|
+
collections.set(k, v);
|
|
582
|
+
continue;
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
if (k === "default" && v) {
|
|
586
|
+
globalConfig = v;
|
|
587
|
+
continue;
|
|
588
|
+
}
|
|
589
|
+
throw new Error(
|
|
590
|
+
`Unknown export "${k}", you can only export collections from source configuration file.`
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
const mdxOptionsCache = /* @__PURE__ */ new Map();
|
|
594
|
+
return {
|
|
595
|
+
global: globalConfig,
|
|
596
|
+
collections,
|
|
597
|
+
async getDefaultMDXOptions(mode = "default") {
|
|
598
|
+
const cached = mdxOptionsCache.get(mode);
|
|
599
|
+
if (cached) return cached;
|
|
600
|
+
const input = this.global.mdxOptions;
|
|
601
|
+
async function uncached() {
|
|
602
|
+
const options = typeof input === "function" ? await input() : input;
|
|
603
|
+
const { getDefaultMDXOptions: getDefaultMDXOptions2 } = await Promise.resolve().then(() => (init_mdx_options(), mdx_options_exports));
|
|
604
|
+
if (options?.preset === "minimal") return options;
|
|
605
|
+
return getDefaultMDXOptions2({
|
|
606
|
+
...options,
|
|
607
|
+
_withoutBundler: mode === "remote"
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
const result = uncached();
|
|
611
|
+
mdxOptionsCache.set(mode, result);
|
|
612
|
+
return result;
|
|
613
|
+
}
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
// src/utils/config.ts
|
|
618
|
+
function findConfigFile() {
|
|
619
|
+
return path4.resolve("source.config.ts");
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
// src/loaders/config-loader.ts
|
|
623
|
+
function resolvedConfig(loaded) {
|
|
624
|
+
return {
|
|
625
|
+
getConfig() {
|
|
626
|
+
return loaded;
|
|
627
|
+
}
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
// src/bun/index.ts
|
|
632
|
+
var import_node_querystring = require("querystring");
|
|
633
|
+
function createMdxPlugin(options = {}) {
|
|
634
|
+
const { configPath = findConfigFile() } = options;
|
|
635
|
+
async function getMdxLoader() {
|
|
636
|
+
const out = buildConfig(await import(configPath));
|
|
637
|
+
return createMdxLoader(resolvedConfig(out));
|
|
638
|
+
}
|
|
639
|
+
return {
|
|
640
|
+
name: "bun-plugin-fumadocs-mdx",
|
|
641
|
+
setup(build) {
|
|
642
|
+
const mdxLoader = getMdxLoader();
|
|
643
|
+
build.onLoad({ filter: /\.mdx(\?.+?)?$/ }, async (args) => {
|
|
644
|
+
const [filePath, query] = args.path.split("?", 2);
|
|
645
|
+
const content = await Bun.file(filePath).text();
|
|
646
|
+
const result = await (await mdxLoader)({
|
|
647
|
+
source: content,
|
|
648
|
+
query: (0, import_node_querystring.parse)(query),
|
|
649
|
+
filePath,
|
|
650
|
+
development: false,
|
|
651
|
+
compiler: {
|
|
652
|
+
addDependency() {
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
});
|
|
656
|
+
return {
|
|
657
|
+
contents: result.code,
|
|
658
|
+
loader: "js"
|
|
659
|
+
};
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
665
|
+
0 && (module.exports = {
|
|
666
|
+
createMdxPlugin
|
|
667
|
+
});
|