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,1185 @@
|
|
|
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/loaders/config/load.ts
|
|
176
|
+
var load_exports = {};
|
|
177
|
+
__export(load_exports, {
|
|
178
|
+
loadConfig: () => loadConfig
|
|
179
|
+
});
|
|
180
|
+
async function compileConfig(configPath, outDir) {
|
|
181
|
+
const { build } = await import("esbuild");
|
|
182
|
+
const transformed = await build({
|
|
183
|
+
entryPoints: [{ in: configPath, out: "source.config" }],
|
|
184
|
+
bundle: true,
|
|
185
|
+
outdir: outDir,
|
|
186
|
+
target: "node20",
|
|
187
|
+
write: true,
|
|
188
|
+
platform: "node",
|
|
189
|
+
format: "esm",
|
|
190
|
+
packages: "external",
|
|
191
|
+
outExtension: {
|
|
192
|
+
".js": ".mjs"
|
|
193
|
+
},
|
|
194
|
+
allowOverwrite: true
|
|
195
|
+
});
|
|
196
|
+
if (transformed.errors.length > 0) {
|
|
197
|
+
throw new Error("failed to compile configuration file");
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
async function loadConfig(configPath, outDir, build = false) {
|
|
201
|
+
if (build) await compileConfig(configPath, outDir);
|
|
202
|
+
const url = (0, import_node_url3.pathToFileURL)(path11.resolve(outDir, "source.config.mjs"));
|
|
203
|
+
url.searchParams.set("hash", Date.now().toString());
|
|
204
|
+
const config = import(url.href).then(
|
|
205
|
+
(loaded) => buildConfig(loaded)
|
|
206
|
+
);
|
|
207
|
+
return await config;
|
|
208
|
+
}
|
|
209
|
+
var path11, import_node_url3;
|
|
210
|
+
var init_load = __esm({
|
|
211
|
+
"src/loaders/config/load.ts"() {
|
|
212
|
+
"use strict";
|
|
213
|
+
path11 = __toESM(require("path"), 1);
|
|
214
|
+
import_node_url3 = require("url");
|
|
215
|
+
init_build();
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
// src/vite/index.ts
|
|
220
|
+
var vite_exports = {};
|
|
221
|
+
__export(vite_exports, {
|
|
222
|
+
default: () => mdx,
|
|
223
|
+
postInstall: () => postInstall
|
|
224
|
+
});
|
|
225
|
+
module.exports = __toCommonJS(vite_exports);
|
|
226
|
+
var import_vite = require("vite");
|
|
227
|
+
init_build();
|
|
228
|
+
var import_node_querystring2 = require("querystring");
|
|
229
|
+
|
|
230
|
+
// src/utils/validation.ts
|
|
231
|
+
var import_picocolors = __toESM(require("picocolors"), 1);
|
|
232
|
+
var ValidationError = class extends Error {
|
|
233
|
+
constructor(message, issues) {
|
|
234
|
+
super(
|
|
235
|
+
`${message}:
|
|
236
|
+
${issues.map((issue) => ` ${issue.path}: ${issue.message}`).join("\n")}`
|
|
237
|
+
);
|
|
238
|
+
this.title = message;
|
|
239
|
+
this.issues = issues;
|
|
240
|
+
}
|
|
241
|
+
toStringFormatted() {
|
|
242
|
+
return [
|
|
243
|
+
import_picocolors.default.bold(`[MDX] ${this.title}:`),
|
|
244
|
+
...this.issues.map(
|
|
245
|
+
(issue) => import_picocolors.default.redBright(
|
|
246
|
+
`- ${import_picocolors.default.bold(issue.path?.join(".") ?? "*")}: ${issue.message}`
|
|
247
|
+
)
|
|
248
|
+
)
|
|
249
|
+
].join("\n");
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
async function validate(schema, data, context, errorMessage) {
|
|
253
|
+
if (typeof schema === "function" && !("~standard" in schema)) {
|
|
254
|
+
schema = schema(context);
|
|
255
|
+
}
|
|
256
|
+
if ("~standard" in schema) {
|
|
257
|
+
const result = await schema["~standard"].validate(
|
|
258
|
+
data
|
|
259
|
+
);
|
|
260
|
+
if (result.issues) {
|
|
261
|
+
throw new ValidationError(errorMessage, result.issues);
|
|
262
|
+
}
|
|
263
|
+
return result.value;
|
|
264
|
+
}
|
|
265
|
+
return data;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// src/vite/index.ts
|
|
269
|
+
var path12 = __toESM(require("path"), 1);
|
|
270
|
+
var import_js_yaml2 = require("js-yaml");
|
|
271
|
+
|
|
272
|
+
// src/utils/fuma-matter.ts
|
|
273
|
+
var import_js_yaml = require("js-yaml");
|
|
274
|
+
var regex = /^---\r?\n(.+?)\r?\n---\r?\n/s;
|
|
275
|
+
function fumaMatter(input) {
|
|
276
|
+
const output = { matter: "", data: {}, content: input };
|
|
277
|
+
const match = regex.exec(input);
|
|
278
|
+
if (!match) {
|
|
279
|
+
return output;
|
|
280
|
+
}
|
|
281
|
+
output.matter = match[0];
|
|
282
|
+
output.content = input.slice(match[0].length);
|
|
283
|
+
const loaded = (0, import_js_yaml.load)(match[1]);
|
|
284
|
+
output.data = loaded ?? {};
|
|
285
|
+
return output;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// src/utils/git-timestamp.ts
|
|
289
|
+
var import_node_path = __toESM(require("path"), 1);
|
|
290
|
+
var import_tinyexec = require("tinyexec");
|
|
291
|
+
var cache = /* @__PURE__ */ new Map();
|
|
292
|
+
async function getGitTimestamp(file) {
|
|
293
|
+
const cached = cache.get(file);
|
|
294
|
+
if (cached) return cached;
|
|
295
|
+
try {
|
|
296
|
+
const out = await (0, import_tinyexec.x)(
|
|
297
|
+
"git",
|
|
298
|
+
["log", "-1", '--pretty="%ai"', import_node_path.default.relative(process.cwd(), file)],
|
|
299
|
+
{
|
|
300
|
+
throwOnError: true
|
|
301
|
+
}
|
|
302
|
+
);
|
|
303
|
+
const time = new Date(out.stdout);
|
|
304
|
+
cache.set(file, time);
|
|
305
|
+
return time;
|
|
306
|
+
} catch {
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// src/loaders/mdx/build-mdx.ts
|
|
312
|
+
var import_mdx = require("@mdx-js/mdx");
|
|
313
|
+
|
|
314
|
+
// src/loaders/mdx/remark-include.ts
|
|
315
|
+
var import_unified = require("unified");
|
|
316
|
+
var import_unist_util_visit2 = require("unist-util-visit");
|
|
317
|
+
var path2 = __toESM(require("path"), 1);
|
|
318
|
+
var fs = __toESM(require("fs/promises"), 1);
|
|
319
|
+
var import_mdx_plugins = require("fumadocs-core/mdx-plugins");
|
|
320
|
+
|
|
321
|
+
// src/loaders/mdx/remark-unravel.ts
|
|
322
|
+
var import_unist_util_visit = require("unist-util-visit");
|
|
323
|
+
function remarkMarkAndUnravel() {
|
|
324
|
+
return (tree) => {
|
|
325
|
+
(0, import_unist_util_visit.visit)(tree, function(node, index, parent) {
|
|
326
|
+
let offset = -1;
|
|
327
|
+
let all = true;
|
|
328
|
+
let oneOrMore = false;
|
|
329
|
+
if (parent && typeof index === "number" && node.type === "paragraph") {
|
|
330
|
+
const children = node.children;
|
|
331
|
+
while (++offset < children.length) {
|
|
332
|
+
const child = children[offset];
|
|
333
|
+
if (child.type === "mdxJsxTextElement" || child.type === "mdxTextExpression") {
|
|
334
|
+
oneOrMore = true;
|
|
335
|
+
} else if (child.type === "text" && child.value.trim().length === 0) {
|
|
336
|
+
} else {
|
|
337
|
+
all = false;
|
|
338
|
+
break;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
if (all && oneOrMore) {
|
|
342
|
+
offset = -1;
|
|
343
|
+
const newChildren = [];
|
|
344
|
+
while (++offset < children.length) {
|
|
345
|
+
const child = children[offset];
|
|
346
|
+
if (child.type === "mdxJsxTextElement") {
|
|
347
|
+
child.type = "mdxJsxFlowElement";
|
|
348
|
+
}
|
|
349
|
+
if (child.type === "mdxTextExpression") {
|
|
350
|
+
child.type = "mdxFlowExpression";
|
|
351
|
+
}
|
|
352
|
+
if (child.type === "text" && /^[\t\r\n ]+$/.test(String(child.value))) {
|
|
353
|
+
} else {
|
|
354
|
+
newChildren.push(child);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
parent.children.splice(index, 1, ...newChildren);
|
|
358
|
+
return index;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// src/loaders/mdx/remark-include.ts
|
|
366
|
+
var ElementLikeTypes = [
|
|
367
|
+
"mdxJsxFlowElement",
|
|
368
|
+
"mdxJsxTextElement",
|
|
369
|
+
"containerDirective",
|
|
370
|
+
"textDirective",
|
|
371
|
+
"leafDirective"
|
|
372
|
+
];
|
|
373
|
+
function isElementLike(node) {
|
|
374
|
+
return ElementLikeTypes.includes(node.type);
|
|
375
|
+
}
|
|
376
|
+
function parseElementAttributes(element) {
|
|
377
|
+
if (Array.isArray(element.attributes)) {
|
|
378
|
+
const attributes = {};
|
|
379
|
+
for (const attr of element.attributes) {
|
|
380
|
+
if (attr.type === "mdxJsxAttribute" && (typeof attr.value === "string" || attr.value === null)) {
|
|
381
|
+
attributes[attr.name] = attr.value;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return attributes;
|
|
385
|
+
}
|
|
386
|
+
return element.attributes ?? {};
|
|
387
|
+
}
|
|
388
|
+
function flattenNode(node) {
|
|
389
|
+
if ("children" in node)
|
|
390
|
+
return node.children.map((child) => flattenNode(child)).join("");
|
|
391
|
+
if ("value" in node) return node.value;
|
|
392
|
+
return "";
|
|
393
|
+
}
|
|
394
|
+
function parseSpecifier(specifier) {
|
|
395
|
+
const idx = specifier.lastIndexOf("#");
|
|
396
|
+
if (idx === -1) return { file: specifier };
|
|
397
|
+
return {
|
|
398
|
+
file: specifier.slice(0, idx),
|
|
399
|
+
section: specifier.slice(idx + 1)
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
function extractSection(root, section) {
|
|
403
|
+
let nodes;
|
|
404
|
+
let capturingHeadingContent = false;
|
|
405
|
+
(0, import_unist_util_visit2.visit)(root, (node) => {
|
|
406
|
+
if (node.type === "heading") {
|
|
407
|
+
if (capturingHeadingContent) {
|
|
408
|
+
return false;
|
|
409
|
+
}
|
|
410
|
+
if (node.data?.hProperties?.id === section) {
|
|
411
|
+
capturingHeadingContent = true;
|
|
412
|
+
nodes = [node];
|
|
413
|
+
return "skip";
|
|
414
|
+
}
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
if (capturingHeadingContent) {
|
|
418
|
+
nodes?.push(node);
|
|
419
|
+
return "skip";
|
|
420
|
+
}
|
|
421
|
+
if (isElementLike(node) && node.name === "section") {
|
|
422
|
+
const attributes = parseElementAttributes(node);
|
|
423
|
+
if (attributes.id === section) {
|
|
424
|
+
nodes = node.children;
|
|
425
|
+
return false;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
});
|
|
429
|
+
if (nodes)
|
|
430
|
+
return {
|
|
431
|
+
type: "root",
|
|
432
|
+
children: nodes
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
function remarkInclude() {
|
|
436
|
+
const TagName = "include";
|
|
437
|
+
const embedContent = async (file, heading, params, data) => {
|
|
438
|
+
let content;
|
|
439
|
+
try {
|
|
440
|
+
content = (await fs.readFile(file)).toString();
|
|
441
|
+
} catch (e) {
|
|
442
|
+
throw new Error(
|
|
443
|
+
`failed to read file ${file}
|
|
444
|
+
${e instanceof Error ? e.message : String(e)}`,
|
|
445
|
+
{ cause: e }
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
const ext = path2.extname(file);
|
|
449
|
+
data._compiler?.addDependency(file);
|
|
450
|
+
if (params.lang || ext !== ".md" && ext !== ".mdx") {
|
|
451
|
+
const lang = params.lang ?? ext.slice(1);
|
|
452
|
+
return {
|
|
453
|
+
type: "code",
|
|
454
|
+
lang,
|
|
455
|
+
meta: params.meta,
|
|
456
|
+
value: content,
|
|
457
|
+
data: {}
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
const parser = data._getProcessor ? data._getProcessor(ext === ".mdx" ? "mdx" : "md") : this;
|
|
461
|
+
const parsed = fumaMatter(content);
|
|
462
|
+
let mdast = parser.parse({
|
|
463
|
+
path: file,
|
|
464
|
+
value: parsed.content,
|
|
465
|
+
data: { frontmatter: parsed.data }
|
|
466
|
+
});
|
|
467
|
+
const baseProcessor = (0, import_unified.unified)().use(remarkMarkAndUnravel);
|
|
468
|
+
if (heading) {
|
|
469
|
+
const extracted = extractSection(
|
|
470
|
+
await baseProcessor.use(import_mdx_plugins.remarkHeading).run(mdast),
|
|
471
|
+
heading
|
|
472
|
+
);
|
|
473
|
+
if (!extracted)
|
|
474
|
+
throw new Error(
|
|
475
|
+
`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.`
|
|
476
|
+
);
|
|
477
|
+
mdast = extracted;
|
|
478
|
+
} else {
|
|
479
|
+
mdast = await baseProcessor.run(mdast);
|
|
480
|
+
}
|
|
481
|
+
await update(mdast, path2.dirname(file), data);
|
|
482
|
+
return mdast;
|
|
483
|
+
};
|
|
484
|
+
async function update(tree, directory, data) {
|
|
485
|
+
const queue = [];
|
|
486
|
+
(0, import_unist_util_visit2.visit)(tree, ElementLikeTypes, (_node, _, parent) => {
|
|
487
|
+
const node = _node;
|
|
488
|
+
if (node.name !== TagName) return;
|
|
489
|
+
const specifier = flattenNode(node);
|
|
490
|
+
if (specifier.length === 0) return "skip";
|
|
491
|
+
const attributes = parseElementAttributes(node);
|
|
492
|
+
const { file: relativePath, section } = parseSpecifier(specifier);
|
|
493
|
+
const file = path2.resolve(
|
|
494
|
+
"cwd" in attributes ? process.cwd() : directory,
|
|
495
|
+
relativePath
|
|
496
|
+
);
|
|
497
|
+
queue.push(
|
|
498
|
+
embedContent(file, section, attributes, data).then((replace) => {
|
|
499
|
+
Object.assign(
|
|
500
|
+
parent && parent.type === "paragraph" ? parent : node,
|
|
501
|
+
replace
|
|
502
|
+
);
|
|
503
|
+
})
|
|
504
|
+
);
|
|
505
|
+
return "skip";
|
|
506
|
+
});
|
|
507
|
+
await Promise.all(queue);
|
|
508
|
+
}
|
|
509
|
+
return async (tree, file) => {
|
|
510
|
+
await update(tree, path2.dirname(file.path), file.data);
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// src/loaders/mdx/remark-postprocess.ts
|
|
515
|
+
var import_unist_util_visit3 = require("unist-util-visit");
|
|
516
|
+
var import_mdast_util_to_markdown = require("mdast-util-to-markdown");
|
|
517
|
+
var import_estree_util_value_to_estree = require("estree-util-value-to-estree");
|
|
518
|
+
var import_unist_util_remove_position = require("unist-util-remove-position");
|
|
519
|
+
var import_remark_mdx = __toESM(require("remark-mdx"), 1);
|
|
520
|
+
function remarkPostprocess({
|
|
521
|
+
_format,
|
|
522
|
+
includeProcessedMarkdown = false,
|
|
523
|
+
includeMDAST = false,
|
|
524
|
+
valueToExport = []
|
|
525
|
+
}) {
|
|
526
|
+
let _stringifyProcessor;
|
|
527
|
+
const getStringifyProcessor = () => {
|
|
528
|
+
if (_format === "mdx") return this;
|
|
529
|
+
return _stringifyProcessor ??= this().use(import_remark_mdx.default).freeze();
|
|
530
|
+
};
|
|
531
|
+
return (tree, file) => {
|
|
532
|
+
let title;
|
|
533
|
+
const urls = [];
|
|
534
|
+
(0, import_unist_util_visit3.visit)(tree, ["heading", "link"], (node) => {
|
|
535
|
+
if (node.type === "heading" && node.depth === 1) {
|
|
536
|
+
title = flattenNode2(node);
|
|
537
|
+
}
|
|
538
|
+
if (node.type !== "link") return;
|
|
539
|
+
urls.push({
|
|
540
|
+
href: node.url
|
|
541
|
+
});
|
|
542
|
+
return "skip";
|
|
543
|
+
});
|
|
544
|
+
if (title) {
|
|
545
|
+
file.data.frontmatter ??= {};
|
|
546
|
+
if (!file.data.frontmatter.title) file.data.frontmatter.title = title;
|
|
547
|
+
}
|
|
548
|
+
file.data.extractedReferences = urls;
|
|
549
|
+
if (includeProcessedMarkdown) {
|
|
550
|
+
const processor = getStringifyProcessor();
|
|
551
|
+
file.data._markdown = (0, import_mdast_util_to_markdown.toMarkdown)(tree, {
|
|
552
|
+
...processor.data("settings"),
|
|
553
|
+
// from https://github.com/remarkjs/remark/blob/main/packages/remark-stringify/lib/index.js
|
|
554
|
+
extensions: processor.data("toMarkdownExtensions") || []
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
if (includeMDAST) {
|
|
558
|
+
const options = includeMDAST === true ? {} : includeMDAST;
|
|
559
|
+
file.data._mdast = JSON.stringify(
|
|
560
|
+
options.removePosition ? (0, import_unist_util_remove_position.removePosition)(structuredClone(tree)) : tree
|
|
561
|
+
);
|
|
562
|
+
}
|
|
563
|
+
for (const { name, value } of file.data["mdx-export"] ?? []) {
|
|
564
|
+
tree.children.unshift(getMdastExport(name, value));
|
|
565
|
+
}
|
|
566
|
+
for (const name of valueToExport) {
|
|
567
|
+
if (!(name in file.data)) continue;
|
|
568
|
+
tree.children.unshift(getMdastExport(name, file.data[name]));
|
|
569
|
+
}
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
function getMdastExport(name, value) {
|
|
573
|
+
return {
|
|
574
|
+
type: "mdxjsEsm",
|
|
575
|
+
value: "",
|
|
576
|
+
data: {
|
|
577
|
+
estree: {
|
|
578
|
+
type: "Program",
|
|
579
|
+
sourceType: "module",
|
|
580
|
+
body: [
|
|
581
|
+
{
|
|
582
|
+
type: "ExportNamedDeclaration",
|
|
583
|
+
attributes: [],
|
|
584
|
+
specifiers: [],
|
|
585
|
+
source: null,
|
|
586
|
+
declaration: {
|
|
587
|
+
type: "VariableDeclaration",
|
|
588
|
+
kind: "let",
|
|
589
|
+
declarations: [
|
|
590
|
+
{
|
|
591
|
+
type: "VariableDeclarator",
|
|
592
|
+
id: {
|
|
593
|
+
type: "Identifier",
|
|
594
|
+
name
|
|
595
|
+
},
|
|
596
|
+
init: (0, import_estree_util_value_to_estree.valueToEstree)(value)
|
|
597
|
+
}
|
|
598
|
+
]
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
]
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
};
|
|
605
|
+
}
|
|
606
|
+
function flattenNode2(node) {
|
|
607
|
+
if ("children" in node)
|
|
608
|
+
return node.children.map((child) => flattenNode2(child)).join("");
|
|
609
|
+
if ("value" in node) return node.value;
|
|
610
|
+
return "";
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
// src/loaders/mdx/build-mdx.ts
|
|
614
|
+
var cache2 = /* @__PURE__ */ new Map();
|
|
615
|
+
async function buildMDX(cacheKey, source, options) {
|
|
616
|
+
const { filePath, frontmatter, data, _compiler, ...rest } = options;
|
|
617
|
+
function getProcessor(format) {
|
|
618
|
+
const key = `${cacheKey}:${format}`;
|
|
619
|
+
let processor = cache2.get(key);
|
|
620
|
+
if (!processor) {
|
|
621
|
+
processor = (0, import_mdx.createProcessor)({
|
|
622
|
+
outputFormat: "program",
|
|
623
|
+
...rest,
|
|
624
|
+
remarkPlugins: [
|
|
625
|
+
remarkInclude,
|
|
626
|
+
...rest.remarkPlugins ?? [],
|
|
627
|
+
[
|
|
628
|
+
remarkPostprocess,
|
|
629
|
+
{
|
|
630
|
+
_format: format,
|
|
631
|
+
...options.postprocess,
|
|
632
|
+
valueToExport: [
|
|
633
|
+
...options.postprocess?.valueToExport ?? [],
|
|
634
|
+
"structuredData",
|
|
635
|
+
"extractedReferences",
|
|
636
|
+
"frontmatter",
|
|
637
|
+
"lastModified",
|
|
638
|
+
"_markdown",
|
|
639
|
+
"_mdast"
|
|
640
|
+
]
|
|
641
|
+
}
|
|
642
|
+
]
|
|
643
|
+
],
|
|
644
|
+
format
|
|
645
|
+
});
|
|
646
|
+
cache2.set(key, processor);
|
|
647
|
+
}
|
|
648
|
+
return processor;
|
|
649
|
+
}
|
|
650
|
+
return getProcessor(
|
|
651
|
+
options.format ?? (filePath.endsWith(".mdx") ? "mdx" : "md")
|
|
652
|
+
).process({
|
|
653
|
+
value: source,
|
|
654
|
+
path: filePath,
|
|
655
|
+
data: {
|
|
656
|
+
...data,
|
|
657
|
+
frontmatter,
|
|
658
|
+
_compiler,
|
|
659
|
+
_getProcessor: getProcessor
|
|
660
|
+
}
|
|
661
|
+
});
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
// src/loaders/mdx/index.ts
|
|
665
|
+
var import_zod = require("zod");
|
|
666
|
+
var import_promises = __toESM(require("fs/promises"), 1);
|
|
667
|
+
var import_node_path2 = __toESM(require("path"), 1);
|
|
668
|
+
var import_node_crypto = require("crypto");
|
|
669
|
+
var querySchema = import_zod.z.object({
|
|
670
|
+
only: import_zod.z.literal(["frontmatter", "all"]).default("all"),
|
|
671
|
+
collection: import_zod.z.string().optional()
|
|
672
|
+
}).loose();
|
|
673
|
+
var cacheEntry = import_zod.z.object({
|
|
674
|
+
code: import_zod.z.string(),
|
|
675
|
+
map: import_zod.z.any().optional(),
|
|
676
|
+
hash: import_zod.z.string().optional()
|
|
677
|
+
});
|
|
678
|
+
function createMdxLoader(configLoader) {
|
|
679
|
+
return async ({
|
|
680
|
+
source: value,
|
|
681
|
+
development: isDevelopment,
|
|
682
|
+
query,
|
|
683
|
+
compiler,
|
|
684
|
+
filePath
|
|
685
|
+
}) => {
|
|
686
|
+
const matter = fumaMatter(value);
|
|
687
|
+
const parsed = querySchema.parse(query);
|
|
688
|
+
const config = await configLoader.getConfig();
|
|
689
|
+
let after;
|
|
690
|
+
if (!isDevelopment && config.global.experimentalBuildCache) {
|
|
691
|
+
const cacheDir = config.global.experimentalBuildCache;
|
|
692
|
+
const cacheKey = `${parsed.hash}_${parsed.collection ?? "global"}_${generateCacheHash(filePath)}`;
|
|
693
|
+
const cached = await import_promises.default.readFile(import_node_path2.default.join(cacheDir, cacheKey)).then((content) => cacheEntry.parse(JSON.parse(content.toString()))).catch(() => null);
|
|
694
|
+
if (cached && cached.hash === generateCacheHash(value)) return cached;
|
|
695
|
+
after = async () => {
|
|
696
|
+
await import_promises.default.mkdir(cacheDir, { recursive: true });
|
|
697
|
+
await import_promises.default.writeFile(
|
|
698
|
+
import_node_path2.default.join(cacheDir, cacheKey),
|
|
699
|
+
JSON.stringify({
|
|
700
|
+
...out,
|
|
701
|
+
hash: generateCacheHash(value)
|
|
702
|
+
})
|
|
703
|
+
);
|
|
704
|
+
};
|
|
705
|
+
}
|
|
706
|
+
const collection = parsed.collection ? config.collections.get(parsed.collection) : void 0;
|
|
707
|
+
let docCollection;
|
|
708
|
+
switch (collection?.type) {
|
|
709
|
+
case "doc":
|
|
710
|
+
docCollection = collection;
|
|
711
|
+
break;
|
|
712
|
+
case "docs":
|
|
713
|
+
docCollection = collection.docs;
|
|
714
|
+
break;
|
|
715
|
+
}
|
|
716
|
+
if (docCollection?.schema) {
|
|
717
|
+
matter.data = await validate(
|
|
718
|
+
docCollection.schema,
|
|
719
|
+
matter.data,
|
|
720
|
+
{
|
|
721
|
+
source: value,
|
|
722
|
+
path: filePath
|
|
723
|
+
},
|
|
724
|
+
`invalid frontmatter in ${filePath}`
|
|
725
|
+
);
|
|
726
|
+
}
|
|
727
|
+
if (parsed.only === "frontmatter") {
|
|
728
|
+
return {
|
|
729
|
+
code: `export const frontmatter = ${JSON.stringify(matter.data)}`,
|
|
730
|
+
map: null
|
|
731
|
+
};
|
|
732
|
+
}
|
|
733
|
+
const data = {};
|
|
734
|
+
if (config.global.lastModifiedTime === "git") {
|
|
735
|
+
data.lastModified = (await getGitTimestamp(filePath))?.getTime();
|
|
736
|
+
}
|
|
737
|
+
const lineOffset = isDevelopment ? countLines(matter.matter) : 0;
|
|
738
|
+
const compiled = await buildMDX(
|
|
739
|
+
`${getConfigHash(config)}:${parsed.collection ?? "global"}`,
|
|
740
|
+
"\n".repeat(lineOffset) + matter.content,
|
|
741
|
+
{
|
|
742
|
+
development: isDevelopment,
|
|
743
|
+
...docCollection?.mdxOptions ?? await config.getDefaultMDXOptions(),
|
|
744
|
+
postprocess: docCollection?.postprocess,
|
|
745
|
+
data,
|
|
746
|
+
filePath,
|
|
747
|
+
frontmatter: matter.data,
|
|
748
|
+
_compiler: compiler
|
|
749
|
+
}
|
|
750
|
+
);
|
|
751
|
+
const out = {
|
|
752
|
+
code: String(compiled.value),
|
|
753
|
+
map: compiled.map
|
|
754
|
+
};
|
|
755
|
+
await after?.();
|
|
756
|
+
return out;
|
|
757
|
+
};
|
|
758
|
+
}
|
|
759
|
+
var hashes = /* @__PURE__ */ new WeakMap();
|
|
760
|
+
function getConfigHash(config) {
|
|
761
|
+
let hash = hashes.get(config);
|
|
762
|
+
if (hash) return hash;
|
|
763
|
+
hash = Date.now().toString();
|
|
764
|
+
hashes.set(config, hash);
|
|
765
|
+
return hash;
|
|
766
|
+
}
|
|
767
|
+
function generateCacheHash(input) {
|
|
768
|
+
return (0, import_node_crypto.createHash)("md5").update(input).digest("hex");
|
|
769
|
+
}
|
|
770
|
+
function countLines(s) {
|
|
771
|
+
let num = 0;
|
|
772
|
+
for (const c of s) {
|
|
773
|
+
if (c === "\n") num++;
|
|
774
|
+
}
|
|
775
|
+
return num;
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
// src/loaders/config/index.ts
|
|
779
|
+
var import_node_path3 = __toESM(require("path"), 1);
|
|
780
|
+
var import_promises2 = __toESM(require("fs/promises"), 1);
|
|
781
|
+
function findConfigFile() {
|
|
782
|
+
return import_node_path3.default.resolve("source.config.ts");
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
// src/loaders/adapter.ts
|
|
786
|
+
var import_node_url = require("url");
|
|
787
|
+
var import_promises3 = __toESM(require("fs/promises"), 1);
|
|
788
|
+
var import_node_querystring = require("querystring");
|
|
789
|
+
var import_node_path4 = __toESM(require("path"), 1);
|
|
790
|
+
function toVite(loader) {
|
|
791
|
+
return async function(file, query, value) {
|
|
792
|
+
const result = await loader({
|
|
793
|
+
filePath: file,
|
|
794
|
+
query: (0, import_node_querystring.parse)(query),
|
|
795
|
+
source: value,
|
|
796
|
+
development: this.environment.mode === "dev",
|
|
797
|
+
compiler: {
|
|
798
|
+
addDependency: (file2) => {
|
|
799
|
+
this.addWatchFile(file2);
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
});
|
|
803
|
+
return {
|
|
804
|
+
code: result.code,
|
|
805
|
+
map: result.map
|
|
806
|
+
};
|
|
807
|
+
};
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
// src/utils/import-formatter.ts
|
|
811
|
+
var import_node_path5 = __toESM(require("path"), 1);
|
|
812
|
+
function toImportPath(file, config) {
|
|
813
|
+
const ext = import_node_path5.default.extname(file);
|
|
814
|
+
let filename;
|
|
815
|
+
if (ext === ".ts" && config.jsExtension) {
|
|
816
|
+
filename = file.substring(0, file.length - ext.length) + ".js";
|
|
817
|
+
} else if (ext === ".ts") {
|
|
818
|
+
filename = file.substring(0, file.length - ext.length);
|
|
819
|
+
} else {
|
|
820
|
+
filename = file;
|
|
821
|
+
}
|
|
822
|
+
let importPath;
|
|
823
|
+
if ("relativeTo" in config) {
|
|
824
|
+
importPath = import_node_path5.default.relative(config.relativeTo, filename);
|
|
825
|
+
if (!import_node_path5.default.isAbsolute(importPath) && !importPath.startsWith(".")) {
|
|
826
|
+
importPath = `./${importPath}`;
|
|
827
|
+
}
|
|
828
|
+
} else {
|
|
829
|
+
importPath = import_node_path5.default.resolve(filename);
|
|
830
|
+
}
|
|
831
|
+
return importPath.replaceAll(import_node_path5.default.sep, "/");
|
|
832
|
+
}
|
|
833
|
+
function ident(code, tab = 1) {
|
|
834
|
+
return code.split("\n").map((v) => " ".repeat(tab) + v).join("\n");
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
// src/utils/collections.ts
|
|
838
|
+
var import_picomatch = __toESM(require("picomatch"), 1);
|
|
839
|
+
var import_tinyglobby = require("tinyglobby");
|
|
840
|
+
var import_node_path6 = __toESM(require("path"), 1);
|
|
841
|
+
var SupportedFormats = {
|
|
842
|
+
doc: ["mdx", "md"],
|
|
843
|
+
meta: ["json", "yaml"]
|
|
844
|
+
};
|
|
845
|
+
function getGlobPatterns(collection) {
|
|
846
|
+
if (collection.files) return collection.files;
|
|
847
|
+
return [`**/*.{${SupportedFormats[collection.type].join(",")}}`];
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
// src/utils/glob-import.ts
|
|
851
|
+
var import_tinyglobby2 = require("tinyglobby");
|
|
852
|
+
var import_node_path7 = __toESM(require("path"), 1);
|
|
853
|
+
var import_node_url2 = require("url");
|
|
854
|
+
function generateGlobImport(patterns, options) {
|
|
855
|
+
let code = "{";
|
|
856
|
+
const result = (0, import_tinyglobby2.globSync)(patterns, {
|
|
857
|
+
cwd: options.base
|
|
858
|
+
});
|
|
859
|
+
for (const item of result) {
|
|
860
|
+
const fullPath = import_node_path7.default.join(options.base, item);
|
|
861
|
+
const url = (0, import_node_url2.pathToFileURL)(fullPath);
|
|
862
|
+
for (const [k, v] of Object.entries(options.query ?? {})) {
|
|
863
|
+
url.searchParams.set(k, v);
|
|
864
|
+
}
|
|
865
|
+
let line = `${JSON.stringify(item)}: () => import(${JSON.stringify(url.href)})`;
|
|
866
|
+
if (options.import) {
|
|
867
|
+
line += `.then(mod => mod[${JSON.stringify(options.import)}])`;
|
|
868
|
+
}
|
|
869
|
+
code += `${line}, `;
|
|
870
|
+
}
|
|
871
|
+
code += "}";
|
|
872
|
+
return code;
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
// src/plugins/vite.ts
|
|
876
|
+
var import_node_path8 = __toESM(require("path"), 1);
|
|
877
|
+
function vite(options) {
|
|
878
|
+
let config;
|
|
879
|
+
return {
|
|
880
|
+
config(v) {
|
|
881
|
+
config = v;
|
|
882
|
+
},
|
|
883
|
+
emit() {
|
|
884
|
+
return [
|
|
885
|
+
{
|
|
886
|
+
path: "index.ts",
|
|
887
|
+
content: indexFile(this.configPath, this.outDir, config, options)
|
|
888
|
+
}
|
|
889
|
+
];
|
|
890
|
+
}
|
|
891
|
+
};
|
|
892
|
+
}
|
|
893
|
+
function indexFile(configPath, outDir, config, options) {
|
|
894
|
+
const { addJsExtension = false, runtime } = options;
|
|
895
|
+
const lines = [
|
|
896
|
+
'/// <reference types="vite/client" />',
|
|
897
|
+
`import { fromConfig } from 'fumadocs-mdx/runtime/vite';`,
|
|
898
|
+
`import type * as Config from '${toImportPath(configPath, {
|
|
899
|
+
relativeTo: outDir,
|
|
900
|
+
jsExtension: addJsExtension
|
|
901
|
+
})}';`,
|
|
902
|
+
"",
|
|
903
|
+
`export const create = fromConfig<typeof Config>();`
|
|
904
|
+
];
|
|
905
|
+
function docs(name, collection) {
|
|
906
|
+
const obj = [
|
|
907
|
+
ident(`doc: ${doc(name, collection.docs)}`),
|
|
908
|
+
ident(`meta: ${meta(name, collection.meta)}`)
|
|
909
|
+
].join(",\n");
|
|
910
|
+
return `{
|
|
911
|
+
${obj}
|
|
912
|
+
}`;
|
|
913
|
+
}
|
|
914
|
+
function doc(name, collection) {
|
|
915
|
+
const patterns = getGlobPatterns(collection);
|
|
916
|
+
const base = getGlobBase(collection);
|
|
917
|
+
const docGlob = generateGlob(patterns, {
|
|
918
|
+
query: {
|
|
919
|
+
collection: name
|
|
920
|
+
},
|
|
921
|
+
base
|
|
922
|
+
});
|
|
923
|
+
if (collection.async) {
|
|
924
|
+
const headBlob = generateGlob(patterns, {
|
|
925
|
+
query: {
|
|
926
|
+
only: "frontmatter",
|
|
927
|
+
collection: name
|
|
928
|
+
},
|
|
929
|
+
import: "frontmatter",
|
|
930
|
+
base
|
|
931
|
+
});
|
|
932
|
+
return `create.docLazy("${name}", "${base}", ${headBlob}, ${docGlob})`;
|
|
933
|
+
}
|
|
934
|
+
return `create.doc("${name}", "${base}", ${docGlob})`;
|
|
935
|
+
}
|
|
936
|
+
function meta(name, collection) {
|
|
937
|
+
const patterns = getGlobPatterns(collection);
|
|
938
|
+
const base = getGlobBase(collection);
|
|
939
|
+
return `create.meta("${name}", "${base}", ${generateGlob(patterns, {
|
|
940
|
+
import: "default",
|
|
941
|
+
base,
|
|
942
|
+
query: {
|
|
943
|
+
collection: name
|
|
944
|
+
}
|
|
945
|
+
})})`;
|
|
946
|
+
}
|
|
947
|
+
function generateGlob(patterns, options2) {
|
|
948
|
+
patterns = mapGlobPatterns(patterns);
|
|
949
|
+
if (runtime === "node" || runtime === "bun") {
|
|
950
|
+
return generateGlobImport(patterns, options2);
|
|
951
|
+
} else {
|
|
952
|
+
return `import.meta.glob(${JSON.stringify(patterns)}, ${JSON.stringify(
|
|
953
|
+
{
|
|
954
|
+
...options2,
|
|
955
|
+
base: import_node_path8.default.relative(outDir, options2.base)
|
|
956
|
+
},
|
|
957
|
+
null,
|
|
958
|
+
2
|
|
959
|
+
)})`;
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
for (const [name, collection] of config.collections.entries()) {
|
|
963
|
+
let body;
|
|
964
|
+
if (collection.type === "docs") {
|
|
965
|
+
body = docs(name, collection);
|
|
966
|
+
} else if (collection.type === "meta") {
|
|
967
|
+
body = meta(name, collection);
|
|
968
|
+
} else {
|
|
969
|
+
body = doc(name, collection);
|
|
970
|
+
}
|
|
971
|
+
lines.push("");
|
|
972
|
+
lines.push(`export const ${name} = ${body};`);
|
|
973
|
+
}
|
|
974
|
+
return lines.join("\n");
|
|
975
|
+
}
|
|
976
|
+
function mapGlobPatterns(patterns) {
|
|
977
|
+
return patterns.map(enforceRelative);
|
|
978
|
+
}
|
|
979
|
+
function enforceRelative(file) {
|
|
980
|
+
if (file.startsWith("./")) return file;
|
|
981
|
+
if (file.startsWith("/")) return `.${file}`;
|
|
982
|
+
return `./${file}`;
|
|
983
|
+
}
|
|
984
|
+
function getGlobBase(collection) {
|
|
985
|
+
let dir = collection.dir;
|
|
986
|
+
if (Array.isArray(dir)) {
|
|
987
|
+
if (dir.length !== 1)
|
|
988
|
+
throw new Error(
|
|
989
|
+
`[Fumadocs MDX] Vite Plugin doesn't support multiple \`dir\` for a collection at the moment.`
|
|
990
|
+
);
|
|
991
|
+
dir = dir[0];
|
|
992
|
+
}
|
|
993
|
+
return enforceRelative(dir);
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
// src/core.ts
|
|
997
|
+
var import_node_path9 = __toESM(require("path"), 1);
|
|
998
|
+
var import_promises4 = __toESM(require("fs/promises"), 1);
|
|
999
|
+
function createCore(options, defaultPlugins = []) {
|
|
1000
|
+
let config;
|
|
1001
|
+
let plugins2;
|
|
1002
|
+
return {
|
|
1003
|
+
_options: options,
|
|
1004
|
+
getPluginContext() {
|
|
1005
|
+
return {
|
|
1006
|
+
core: this,
|
|
1007
|
+
...options
|
|
1008
|
+
};
|
|
1009
|
+
},
|
|
1010
|
+
/**
|
|
1011
|
+
* Convenient cache store, reset when config changes
|
|
1012
|
+
*/
|
|
1013
|
+
cache: /* @__PURE__ */ new Map(),
|
|
1014
|
+
async init({ config: newConfig }) {
|
|
1015
|
+
config = await newConfig;
|
|
1016
|
+
this.cache.clear();
|
|
1017
|
+
plugins2 = [];
|
|
1018
|
+
for await (const option of [
|
|
1019
|
+
...defaultPlugins,
|
|
1020
|
+
...config.global.plugins ?? []
|
|
1021
|
+
]) {
|
|
1022
|
+
if (!option) continue;
|
|
1023
|
+
if (Array.isArray(option)) plugins2.push(...option);
|
|
1024
|
+
else plugins2.push(option);
|
|
1025
|
+
}
|
|
1026
|
+
for (const plugin of plugins2) {
|
|
1027
|
+
const out = await plugin.config?.call(this.getPluginContext(), config);
|
|
1028
|
+
if (out) config = out;
|
|
1029
|
+
}
|
|
1030
|
+
return this;
|
|
1031
|
+
},
|
|
1032
|
+
getConfig() {
|
|
1033
|
+
return config;
|
|
1034
|
+
},
|
|
1035
|
+
creatConfigLoader() {
|
|
1036
|
+
return {
|
|
1037
|
+
getConfig() {
|
|
1038
|
+
return config;
|
|
1039
|
+
}
|
|
1040
|
+
};
|
|
1041
|
+
},
|
|
1042
|
+
async initServer(server) {
|
|
1043
|
+
for (const plugin of plugins2) {
|
|
1044
|
+
await plugin.configureServer?.call(this.getPluginContext(), server);
|
|
1045
|
+
}
|
|
1046
|
+
},
|
|
1047
|
+
async emitAndWrite({
|
|
1048
|
+
filterPlugin = () => true
|
|
1049
|
+
} = {}) {
|
|
1050
|
+
const start = performance.now();
|
|
1051
|
+
const out = await Promise.all(
|
|
1052
|
+
plugins2.map((plugin) => {
|
|
1053
|
+
if (!filterPlugin(plugin) || !plugin.emit) return [];
|
|
1054
|
+
return plugin.emit.call(this.getPluginContext());
|
|
1055
|
+
})
|
|
1056
|
+
);
|
|
1057
|
+
await Promise.all(
|
|
1058
|
+
out.flat().map(async (entry) => {
|
|
1059
|
+
const file = import_node_path9.default.join(options.outDir, entry.path);
|
|
1060
|
+
await import_promises4.default.mkdir(import_node_path9.default.dirname(file), { recursive: true });
|
|
1061
|
+
await import_promises4.default.writeFile(file, entry.content);
|
|
1062
|
+
})
|
|
1063
|
+
);
|
|
1064
|
+
console.log(`[MDX] generated files in ${performance.now() - start}ms`);
|
|
1065
|
+
}
|
|
1066
|
+
};
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
// src/vite/index.ts
|
|
1070
|
+
var FumadocsDeps = ["fumadocs-core", "fumadocs-ui", "fumadocs-openapi"];
|
|
1071
|
+
async function mdx(config, pluginOptions = {}) {
|
|
1072
|
+
const options = applyDefaults(pluginOptions);
|
|
1073
|
+
const core = await createViteCore(options).init({
|
|
1074
|
+
config: buildConfig(config)
|
|
1075
|
+
});
|
|
1076
|
+
const mdxLoader = toVite(createMdxLoader(core.creatConfigLoader()));
|
|
1077
|
+
async function transformMeta(path13, query, value) {
|
|
1078
|
+
const isJson = path13.endsWith(".json");
|
|
1079
|
+
const parsed = (0, import_node_querystring2.parse)(query);
|
|
1080
|
+
const collection = parsed.collection ? core.getConfig().collections.get(parsed.collection) : void 0;
|
|
1081
|
+
if (!collection) return null;
|
|
1082
|
+
let schema;
|
|
1083
|
+
switch (collection.type) {
|
|
1084
|
+
case "meta":
|
|
1085
|
+
schema = collection.schema;
|
|
1086
|
+
break;
|
|
1087
|
+
case "docs":
|
|
1088
|
+
schema = collection.meta.schema;
|
|
1089
|
+
break;
|
|
1090
|
+
}
|
|
1091
|
+
if (!schema) return null;
|
|
1092
|
+
let data;
|
|
1093
|
+
try {
|
|
1094
|
+
data = isJson ? JSON.parse(value) : (0, import_js_yaml2.load)(value);
|
|
1095
|
+
} catch {
|
|
1096
|
+
return null;
|
|
1097
|
+
}
|
|
1098
|
+
const out = await validate(
|
|
1099
|
+
schema,
|
|
1100
|
+
data,
|
|
1101
|
+
{ path: path13, source: value },
|
|
1102
|
+
`invalid data in ${path13}`
|
|
1103
|
+
);
|
|
1104
|
+
return {
|
|
1105
|
+
code: isJson ? JSON.stringify(out) : `export default ${JSON.stringify(out)}`,
|
|
1106
|
+
map: null
|
|
1107
|
+
};
|
|
1108
|
+
}
|
|
1109
|
+
return {
|
|
1110
|
+
name: "fumadocs-mdx",
|
|
1111
|
+
// needed, otherwise other plugins will be executed before our `transform`.
|
|
1112
|
+
enforce: "pre",
|
|
1113
|
+
config(config2) {
|
|
1114
|
+
if (!options.updateViteConfig) return config2;
|
|
1115
|
+
return (0, import_vite.mergeConfig)(config2, {
|
|
1116
|
+
optimizeDeps: {
|
|
1117
|
+
exclude: FumadocsDeps
|
|
1118
|
+
},
|
|
1119
|
+
resolve: {
|
|
1120
|
+
noExternal: FumadocsDeps,
|
|
1121
|
+
dedupe: FumadocsDeps
|
|
1122
|
+
}
|
|
1123
|
+
});
|
|
1124
|
+
},
|
|
1125
|
+
async buildStart() {
|
|
1126
|
+
await core.emitAndWrite();
|
|
1127
|
+
},
|
|
1128
|
+
async configureServer(server) {
|
|
1129
|
+
await core.initServer({
|
|
1130
|
+
watcher: server.watcher
|
|
1131
|
+
});
|
|
1132
|
+
},
|
|
1133
|
+
async transform(value, id) {
|
|
1134
|
+
const [file, query = ""] = id.split("?");
|
|
1135
|
+
const ext = path12.extname(file);
|
|
1136
|
+
try {
|
|
1137
|
+
if ([".yaml", ".json"].includes(ext))
|
|
1138
|
+
return await transformMeta(file, query, value);
|
|
1139
|
+
if ([".md", ".mdx"].includes(ext))
|
|
1140
|
+
return await mdxLoader.call(this, file, query, value);
|
|
1141
|
+
} catch (e) {
|
|
1142
|
+
if (e instanceof ValidationError) {
|
|
1143
|
+
throw new Error(e.toStringFormatted());
|
|
1144
|
+
}
|
|
1145
|
+
throw e;
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
};
|
|
1149
|
+
}
|
|
1150
|
+
async function postInstall(configPath = findConfigFile(), pluginOptions = {}) {
|
|
1151
|
+
const { loadConfig: loadConfig2 } = await Promise.resolve().then(() => (init_load(), load_exports));
|
|
1152
|
+
const options = applyDefaults(pluginOptions);
|
|
1153
|
+
const core = await createViteCore(options).init({
|
|
1154
|
+
config: loadConfig2(configPath, options.outDir, true)
|
|
1155
|
+
});
|
|
1156
|
+
await core.emitAndWrite();
|
|
1157
|
+
}
|
|
1158
|
+
function createViteCore({
|
|
1159
|
+
configPath,
|
|
1160
|
+
outDir,
|
|
1161
|
+
generateIndexFile
|
|
1162
|
+
}) {
|
|
1163
|
+
return createCore(
|
|
1164
|
+
{
|
|
1165
|
+
environment: "vite",
|
|
1166
|
+
configPath,
|
|
1167
|
+
outDir
|
|
1168
|
+
},
|
|
1169
|
+
[
|
|
1170
|
+
generateIndexFile !== false && vite(typeof generateIndexFile === "object" ? generateIndexFile : {})
|
|
1171
|
+
]
|
|
1172
|
+
);
|
|
1173
|
+
}
|
|
1174
|
+
function applyDefaults(options) {
|
|
1175
|
+
return {
|
|
1176
|
+
updateViteConfig: options.updateViteConfig ?? true,
|
|
1177
|
+
generateIndexFile: options.generateIndexFile ?? true,
|
|
1178
|
+
configPath: options.configPath ?? "source.config.ts",
|
|
1179
|
+
outDir: options.outDir ?? ".source"
|
|
1180
|
+
};
|
|
1181
|
+
}
|
|
1182
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1183
|
+
0 && (module.exports = {
|
|
1184
|
+
postInstall
|
|
1185
|
+
});
|