fumadocs-mdx 14.0.0 → 14.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/dist/{build-mdx-6UAK5FF5.js → build-mdx-W3233QBZ.js} +1 -1
  2. package/dist/bun/index.d.ts +7 -1
  3. package/dist/bun/index.js +9 -8
  4. package/dist/{chunk-5YXP7JLN.js → chunk-5OBUOALK.js} +8 -5
  5. package/dist/{chunk-ZY6UZ7NH.js → chunk-6RPNS75C.js} +1 -1
  6. package/dist/{chunk-XHJCLBZ4.js → chunk-ED3ON275.js} +86 -162
  7. package/dist/{chunk-7L2KNF6B.js → chunk-GB4W3YCZ.js} +51 -7
  8. package/dist/{chunk-SLY7WXTX.js → chunk-KOPLIEVQ.js} +5 -6
  9. package/dist/{chunk-OXSRIWQW.js → chunk-LPX7ZO66.js} +4 -3
  10. package/dist/{chunk-SRSRFOVI.js → chunk-OLL7FM7W.js} +3 -3
  11. package/dist/{chunk-5UMZCWKV.js → chunk-Q5OSGLJL.js} +1 -1
  12. package/dist/{chunk-E5DJTSIM.js → chunk-S7KOJHHO.js} +4 -1
  13. package/dist/{chunk-6NISOLQ6.js → chunk-USWQVJWR.js} +1 -1
  14. package/dist/chunk-WBIHDYMN.js +126 -0
  15. package/dist/config/index.d.ts +6 -1
  16. package/dist/config/index.js +1 -1
  17. package/dist/{index-D7JdSMpp.d.cts → core-C3QZSdEx.d.ts} +73 -4
  18. package/dist/{index-BlVBvy-z.d.ts → index-RVIZxMZG.d.ts} +1 -1
  19. package/dist/index.d.ts +56 -3
  20. package/dist/load-from-file-OZ5N7DXU.js +8 -0
  21. package/dist/next/index.cjs +263 -193
  22. package/dist/next/index.d.ts +7 -2
  23. package/dist/next/index.js +12 -10
  24. package/dist/node/loader.js +4 -4
  25. package/dist/plugins/index-file.d.ts +7 -22
  26. package/dist/plugins/index-file.js +2 -2
  27. package/dist/plugins/json-schema.d.ts +7 -1
  28. package/dist/plugins/last-modified.d.ts +15 -2
  29. package/dist/plugins/last-modified.js +43 -6
  30. package/dist/runtime/browser.d.ts +22 -19
  31. package/dist/runtime/browser.js +5 -6
  32. package/dist/runtime/dynamic.d.ts +12 -12
  33. package/dist/runtime/dynamic.js +12 -11
  34. package/dist/runtime/server.d.ts +10 -157
  35. package/dist/runtime/server.js +3 -3
  36. package/dist/runtime/types.d.ts +61 -0
  37. package/dist/runtime/types.js +0 -0
  38. package/dist/vite/index.d.ts +7 -2
  39. package/dist/vite/index.js +10 -9
  40. package/dist/webpack/mdx.d.ts +8 -2
  41. package/dist/webpack/mdx.js +5 -5
  42. package/dist/webpack/meta.d.ts +8 -2
  43. package/dist/webpack/meta.js +4 -4
  44. package/package.json +4 -6
  45. package/dist/bin.cjs +0 -1889
  46. package/dist/bin.d.cts +0 -1
  47. package/dist/bun/index.cjs +0 -1112
  48. package/dist/bun/index.d.cts +0 -19
  49. package/dist/chunk-PKI7ZDA5.js +0 -29
  50. package/dist/config/index.cjs +0 -400
  51. package/dist/config/index.d.cts +0 -13
  52. package/dist/index-D7JdSMpp.d.ts +0 -272
  53. package/dist/index-P2NNUkHn.d.cts +0 -8
  54. package/dist/index.cjs +0 -18
  55. package/dist/index.d.cts +0 -8
  56. package/dist/load-from-file-I3ALLIVB.js +0 -8
  57. package/dist/next/index.d.cts +0 -28
  58. package/dist/node/loader.cjs +0 -1165
  59. package/dist/node/loader.d.cts +0 -5
  60. package/dist/plugins/index-file.cjs +0 -471
  61. package/dist/plugins/index-file.d.cts +0 -29
  62. package/dist/plugins/json-schema.cjs +0 -114
  63. package/dist/plugins/json-schema.d.cts +0 -25
  64. package/dist/plugins/last-modified.cjs +0 -75
  65. package/dist/plugins/last-modified.d.cts +0 -27
  66. package/dist/runtime/browser.cjs +0 -94
  67. package/dist/runtime/browser.d.cts +0 -50
  68. package/dist/runtime/dynamic.cjs +0 -985
  69. package/dist/runtime/dynamic.d.cts +0 -27
  70. package/dist/runtime/server.cjs +0 -173
  71. package/dist/runtime/server.d.cts +0 -161
  72. package/dist/vite/index.cjs +0 -1620
  73. package/dist/vite/index.d.cts +0 -39
  74. package/dist/webpack/mdx.cjs +0 -1089
  75. package/dist/webpack/mdx.d.cts +0 -14
  76. package/dist/webpack/meta.cjs +0 -649
  77. package/dist/webpack/meta.d.cts +0 -14
package/dist/bin.cjs DELETED
@@ -1,1889 +0,0 @@
1
- #!/usr/bin/env node
2
- "use strict";
3
- var __create = Object.create;
4
- var __defProp = Object.defineProperty;
5
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
- var __getOwnPropNames = Object.getOwnPropertyNames;
7
- var __getProtoOf = Object.getPrototypeOf;
8
- var __hasOwnProp = Object.prototype.hasOwnProperty;
9
- var __esm = (fn, res) => function __init() {
10
- return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
11
- };
12
- var __export = (target, all) => {
13
- for (var name in all)
14
- __defProp(target, name, { get: all[name], enumerable: true });
15
- };
16
- var __copyProps = (to, from, except, desc) => {
17
- if (from && typeof from === "object" || typeof from === "function") {
18
- for (let key of __getOwnPropNames(from))
19
- if (!__hasOwnProp.call(to, key) && key !== except)
20
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
21
- }
22
- return to;
23
- };
24
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
25
- // If the importer is in node compatibility mode or this is not an ESM
26
- // file that has been converted to a CommonJS file using a Babel-
27
- // compatible transform (i.e. "__esModule" has not been set), then set
28
- // "default" to the CommonJS "module.exports" for node compatibility.
29
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
30
- mod
31
- ));
32
-
33
- // src/config/preset.ts
34
- function pluginOption(def, options = []) {
35
- const list = def(Array.isArray(options) ? options : []).filter(
36
- Boolean
37
- );
38
- if (typeof options === "function") {
39
- return options(list);
40
- }
41
- return list;
42
- }
43
- function applyMdxPreset(options = {}) {
44
- return async (environment = "bundler") => {
45
- if (options.preset === "minimal") return options;
46
- const plugins = await import("fumadocs-core/mdx-plugins");
47
- const {
48
- valueToExport = [],
49
- rehypeCodeOptions,
50
- remarkImageOptions,
51
- remarkHeadingOptions,
52
- remarkStructureOptions,
53
- remarkCodeTabOptions,
54
- remarkNpmOptions,
55
- ...mdxOptions
56
- } = options;
57
- const remarkPlugins = pluginOption(
58
- (v) => [
59
- plugins.remarkGfm,
60
- [
61
- plugins.remarkHeading,
62
- {
63
- generateToc: false,
64
- ...remarkHeadingOptions
65
- }
66
- ],
67
- remarkImageOptions !== false && [
68
- plugins.remarkImage,
69
- {
70
- ...remarkImageOptions,
71
- useImport: remarkImageOptions?.useImport ?? environment === "bundler"
72
- }
73
- ],
74
- "remarkCodeTab" in plugins && remarkCodeTabOptions !== false && [
75
- plugins.remarkCodeTab,
76
- remarkCodeTabOptions
77
- ],
78
- "remarkNpm" in plugins && remarkNpmOptions !== false && [plugins.remarkNpm, remarkNpmOptions],
79
- ...v,
80
- remarkStructureOptions !== false && [
81
- plugins.remarkStructure,
82
- remarkStructureOptions
83
- ],
84
- valueToExport.length > 0 && (() => {
85
- return (_, file) => {
86
- file.data["mdx-export"] ??= [];
87
- for (const name of valueToExport) {
88
- if (!(name in file.data)) continue;
89
- file.data["mdx-export"].push({
90
- name,
91
- value: file.data[name]
92
- });
93
- }
94
- };
95
- })
96
- ],
97
- mdxOptions.remarkPlugins
98
- );
99
- const rehypePlugins = pluginOption(
100
- (v) => [
101
- rehypeCodeOptions !== false && [plugins.rehypeCode, rehypeCodeOptions],
102
- ...v,
103
- plugins.rehypeToc
104
- ],
105
- mdxOptions.rehypePlugins
106
- );
107
- return {
108
- ...mdxOptions,
109
- outputFormat: environment === "runtime" ? "function-body" : mdxOptions.outputFormat,
110
- remarkPlugins,
111
- rehypePlugins
112
- };
113
- };
114
- }
115
- var init_preset = __esm({
116
- "src/config/preset.ts"() {
117
- "use strict";
118
- }
119
- });
120
-
121
- // src/config/build.ts
122
- function buildCollection(name, config) {
123
- if (config.type === "docs") {
124
- return {
125
- ...config,
126
- name,
127
- meta: buildPrimitiveCollection(name, config.meta),
128
- docs: buildPrimitiveCollection(name, config.docs),
129
- hasFile(filePath) {
130
- return this.docs.hasFile(filePath) || this.meta.hasFile(filePath);
131
- }
132
- };
133
- }
134
- return buildPrimitiveCollection(name, config);
135
- }
136
- function buildPrimitiveCollection(name, { files, ...config }) {
137
- const supportedFormats = SupportedFormats[config.type];
138
- const patterns = files ?? [`**/*.{${supportedFormats.join(",")}}`];
139
- let matchers;
140
- return {
141
- ...config,
142
- name,
143
- patterns,
144
- isFileSupported(filePath) {
145
- return supportedFormats.some((format) => filePath.endsWith(`.${format}`));
146
- },
147
- hasFile(filePath) {
148
- matchers ??= (Array.isArray(config.dir) ? config.dir : [config.dir]).map(
149
- (dir) => (0, import_picomatch.default)(patterns, {
150
- cwd: dir
151
- })
152
- );
153
- return this.isFileSupported(filePath) && matchers.some((matcher) => matcher(filePath));
154
- }
155
- };
156
- }
157
- function buildConfig(config) {
158
- const collections = /* @__PURE__ */ new Map();
159
- const loaded = {};
160
- for (const [k, v] of Object.entries(config)) {
161
- if (!v) {
162
- continue;
163
- }
164
- if (typeof v === "object" && "type" in v) {
165
- if (v.type === "docs") {
166
- collections.set(k, buildCollection(k, v));
167
- continue;
168
- }
169
- if (v.type === "doc" || v.type === "meta") {
170
- collections.set(
171
- k,
172
- buildCollection(k, v)
173
- );
174
- continue;
175
- }
176
- }
177
- if (k === "default" && v) {
178
- Object.assign(loaded, v);
179
- continue;
180
- }
181
- throw new Error(
182
- `Unknown export "${k}", you can only export collections from source configuration file.`
183
- );
184
- }
185
- const mdxOptionsCache = /* @__PURE__ */ new Map();
186
- return {
187
- global: loaded,
188
- collectionList: Array.from(collections.values()),
189
- getCollection(name) {
190
- return collections.get(name);
191
- },
192
- getMDXOptions(collection, environment = "bundler") {
193
- const key = collection ? `${environment}:${collection.name}` : environment;
194
- const cached = mdxOptionsCache.get(key);
195
- if (cached) return cached;
196
- let result;
197
- if (collection?.mdxOptions) {
198
- const optionsFn = collection.mdxOptions;
199
- result = typeof optionsFn === "function" ? optionsFn(environment) : optionsFn;
200
- } else {
201
- result = (async () => {
202
- const optionsFn = this.global.mdxOptions;
203
- const options = typeof optionsFn === "function" ? await optionsFn() : optionsFn;
204
- return applyMdxPreset(options)(environment);
205
- })();
206
- }
207
- mdxOptionsCache.set(key, result);
208
- return result;
209
- }
210
- };
211
- }
212
- var import_picomatch, SupportedFormats;
213
- var init_build = __esm({
214
- "src/config/build.ts"() {
215
- "use strict";
216
- import_picomatch = __toESM(require("picomatch"), 1);
217
- init_preset();
218
- SupportedFormats = {
219
- doc: ["mdx", "md"],
220
- meta: ["json", "yaml"]
221
- };
222
- }
223
- });
224
-
225
- // src/config/load-from-file.ts
226
- var load_from_file_exports = {};
227
- __export(load_from_file_exports, {
228
- loadConfig: () => loadConfig
229
- });
230
- async function compileConfig(core) {
231
- const { build } = await import("esbuild");
232
- const transformed = await build({
233
- entryPoints: [{ in: core._options.configPath, out: "source.config" }],
234
- bundle: true,
235
- outdir: core._options.outDir,
236
- target: "node20",
237
- write: true,
238
- platform: "node",
239
- format: "esm",
240
- packages: "external",
241
- outExtension: {
242
- ".js": ".mjs"
243
- },
244
- allowOverwrite: true
245
- });
246
- if (transformed.errors.length > 0) {
247
- throw new Error("failed to compile configuration file");
248
- }
249
- }
250
- async function loadConfig(core, build = false) {
251
- if (build) await compileConfig(core);
252
- const url = (0, import_node_url.pathToFileURL)(core.getCompiledConfigPath());
253
- url.searchParams.set("hash", Date.now().toString());
254
- const config = import(url.href).then(
255
- (loaded) => buildConfig(loaded)
256
- );
257
- return await config;
258
- }
259
- var import_node_url;
260
- var init_load_from_file = __esm({
261
- "src/config/load-from-file.ts"() {
262
- "use strict";
263
- import_node_url = require("url");
264
- init_build();
265
- }
266
- });
267
-
268
- // src/utils/validation.ts
269
- async function validate(schema, data, context, errorMessage) {
270
- if (typeof schema === "function" && !("~standard" in schema)) {
271
- schema = schema(context);
272
- }
273
- if ("~standard" in schema) {
274
- const result = await schema["~standard"].validate(
275
- data
276
- );
277
- if (result.issues) {
278
- throw new ValidationError(errorMessage, result.issues);
279
- }
280
- return result.value;
281
- }
282
- return data;
283
- }
284
- var ValidationError;
285
- var init_validation = __esm({
286
- "src/utils/validation.ts"() {
287
- "use strict";
288
- ValidationError = class extends Error {
289
- constructor(message, issues) {
290
- super(
291
- `${message}:
292
- ${issues.map((issue) => ` ${issue.path}: ${issue.message}`).join("\n")}`
293
- );
294
- this.title = message;
295
- this.issues = issues;
296
- }
297
- async toStringFormatted() {
298
- const picocolors = await import("picocolors");
299
- return [
300
- picocolors.bold(`[MDX] ${this.title}:`),
301
- ...this.issues.map(
302
- (issue) => picocolors.redBright(
303
- `- ${picocolors.bold(issue.path?.join(".") ?? "*")}: ${issue.message}`
304
- )
305
- )
306
- ].join("\n");
307
- }
308
- };
309
- }
310
- });
311
-
312
- // src/utils/codegen/cache.ts
313
- function toFullPath(file) {
314
- if (import_node_path.default.isAbsolute(file)) {
315
- return import_node_path.default.relative(process.cwd(), file);
316
- }
317
- return file;
318
- }
319
- async function readFileWithCache(file) {
320
- const fullPath = toFullPath(file);
321
- const cached = map.get(fullPath);
322
- if (cached) return cached;
323
- const read = import_promises.default.readFile(fullPath).then((s) => s.toString());
324
- map.set(fullPath, read);
325
- return read;
326
- }
327
- function removeFileCache(file) {
328
- map.delete(toFullPath(file));
329
- }
330
- var import_lru_cache, import_promises, import_node_path, map;
331
- var init_cache = __esm({
332
- "src/utils/codegen/cache.ts"() {
333
- "use strict";
334
- import_lru_cache = require("lru-cache");
335
- import_promises = __toESM(require("fs/promises"), 1);
336
- import_node_path = __toESM(require("path"), 1);
337
- map = new import_lru_cache.LRUCache({
338
- max: 100
339
- });
340
- }
341
- });
342
-
343
- // src/core.ts
344
- async function getPlugins(pluginOptions) {
345
- const plugins = [];
346
- for await (const option of pluginOptions) {
347
- if (!option) continue;
348
- if (Array.isArray(option)) plugins.push(...await getPlugins(option));
349
- else plugins.push(option);
350
- }
351
- return plugins;
352
- }
353
- function createCore(options, defaultPlugins = []) {
354
- let config;
355
- let plugins;
356
- async function transformMetadata({
357
- collection,
358
- filePath,
359
- source
360
- }, data) {
361
- if (collection.schema) {
362
- data = await validate(
363
- collection.schema,
364
- data,
365
- { path: filePath, source },
366
- collection.type === "doc" ? `invalid frontmatter in ${filePath}` : `invalid data in ${filePath}`
367
- );
368
- }
369
- return data;
370
- }
371
- const core = {
372
- _options: options,
373
- /**
374
- * Convenient cache store, reset when config changes
375
- */
376
- cache: /* @__PURE__ */ new Map(),
377
- async init({ config: newConfig }) {
378
- config = await newConfig;
379
- this.cache.clear();
380
- plugins = await getPlugins([
381
- ...defaultPlugins,
382
- ...config.global.plugins ?? []
383
- ]);
384
- for (const plugin of plugins) {
385
- const out = await plugin.config?.call(pluginContext, config);
386
- if (out) config = out;
387
- }
388
- return this;
389
- },
390
- getConfig() {
391
- return config;
392
- },
393
- /**
394
- * The file path of compiled config file, the file may not exist (e.g. on Vite, or still compiling)
395
- */
396
- getCompiledConfigPath() {
397
- return import_node_path2.default.join(options.outDir, "source.config.mjs");
398
- },
399
- async initServer(server) {
400
- server.watcher?.on("all", async (event, file) => {
401
- if (event === "change") removeFileCache(file);
402
- });
403
- for (const plugin of plugins) {
404
- await plugin.configureServer?.call(pluginContext, server);
405
- }
406
- },
407
- async emit({ filterPlugin = () => true } = {}) {
408
- return (await Promise.all(
409
- plugins.map((plugin) => {
410
- if (!filterPlugin(plugin) || !plugin.emit) return [];
411
- return plugin.emit.call(pluginContext);
412
- })
413
- )).flat();
414
- },
415
- async emitAndWrite(emitOptions) {
416
- const start2 = performance.now();
417
- const out = await this.emit(emitOptions);
418
- await Promise.all(
419
- out.map(async (entry) => {
420
- const file = import_node_path2.default.join(options.outDir, entry.path);
421
- await import_promises2.default.mkdir(import_node_path2.default.dirname(file), { recursive: true });
422
- await import_promises2.default.writeFile(file, entry.content);
423
- })
424
- );
425
- console.log(`[MDX] generated files in ${performance.now() - start2}ms`);
426
- },
427
- async transformMeta(options2, data) {
428
- const ctx = {
429
- ...pluginContext,
430
- ...options2
431
- };
432
- data = await transformMetadata(options2, data);
433
- for (const plugin of plugins) {
434
- if (plugin.meta?.transform)
435
- data = await plugin.meta.transform.call(ctx, data) ?? data;
436
- }
437
- return data;
438
- },
439
- async transformFrontmatter(options2, data) {
440
- const ctx = {
441
- ...pluginContext,
442
- ...options2
443
- };
444
- data = await transformMetadata(options2, data);
445
- for (const plugin of plugins) {
446
- if (plugin.doc?.frontmatter)
447
- data = await plugin.doc.frontmatter.call(ctx, data) ?? data;
448
- }
449
- return data;
450
- },
451
- async transformVFile(options2, file) {
452
- const ctx = {
453
- ...pluginContext,
454
- ...options2
455
- };
456
- for (const plugin of plugins) {
457
- if (plugin.doc?.vfile)
458
- file = await plugin.doc.vfile.call(ctx, file) ?? file;
459
- }
460
- return file;
461
- }
462
- };
463
- const pluginContext = {
464
- core,
465
- ...options
466
- };
467
- return core;
468
- }
469
- var import_node_path2, import_promises2, _Defaults;
470
- var init_core = __esm({
471
- "src/core.ts"() {
472
- "use strict";
473
- import_node_path2 = __toESM(require("path"), 1);
474
- import_promises2 = __toESM(require("fs/promises"), 1);
475
- init_cache();
476
- init_validation();
477
- _Defaults = {
478
- configPath: "source.config.ts",
479
- outDir: ".source"
480
- };
481
- }
482
- });
483
-
484
- // src/loaders/index.ts
485
- var metaLoaderGlob, mdxLoaderGlob;
486
- var init_loaders = __esm({
487
- "src/loaders/index.ts"() {
488
- "use strict";
489
- metaLoaderGlob = /\.(json|yaml)(\?.+?)?$/;
490
- mdxLoaderGlob = /\.mdx?(\?.+?)?$/;
491
- }
492
- });
493
-
494
- // src/utils/codegen/index.ts
495
- function createCodegen({
496
- target = "default",
497
- outDir = "",
498
- jsExtension = false,
499
- globCache = /* @__PURE__ */ new Map()
500
- }) {
501
- let eagerImportId = 0;
502
- const banner = ["// @ts-nocheck"];
503
- if (target === "vite") {
504
- banner.push('/// <reference types="vite/client" />');
505
- }
506
- return {
507
- options: {
508
- target,
509
- outDir
510
- },
511
- lines: [],
512
- addImport(statement) {
513
- this.lines.unshift(statement);
514
- },
515
- async pushAsync(insert) {
516
- for (const line of await Promise.all(insert)) {
517
- if (line === void 0) continue;
518
- this.lines.push(line);
519
- }
520
- },
521
- async generateGlobImport(patterns, options) {
522
- if (target === "vite") {
523
- return this.generateViteGlobImport(patterns, options);
524
- }
525
- return this.generateNodeGlobImport(patterns, options);
526
- },
527
- generateViteGlobImport(patterns, { base, ...rest }) {
528
- patterns = (typeof patterns === "string" ? [patterns] : patterns).map(
529
- normalizeViteGlobPath
530
- );
531
- return `import.meta.glob(${JSON.stringify(patterns)}, ${JSON.stringify(
532
- {
533
- base: normalizeViteGlobPath(import_node_path3.default.relative(outDir, base)),
534
- ...rest
535
- },
536
- null,
537
- 2
538
- )})`;
539
- },
540
- async generateNodeGlobImport(patterns, {
541
- base,
542
- eager = false,
543
- query = {},
544
- import: importName
545
- }) {
546
- const cacheKey = JSON.stringify({ patterns, base });
547
- let files = globCache.get(cacheKey);
548
- if (!files) {
549
- files = (0, import_tinyglobby.glob)(patterns, {
550
- cwd: base
551
- });
552
- globCache.set(cacheKey, files);
553
- }
554
- let code = "{";
555
- for (const item of await files) {
556
- const fullPath = import_node_path3.default.join(base, item);
557
- const searchParams = new URLSearchParams();
558
- for (const [k, v] of Object.entries(query)) {
559
- searchParams.set(k, v);
560
- }
561
- const importPath = this.formatImportPath(fullPath) + "?" + searchParams.toString();
562
- if (eager) {
563
- const name = `__fd_glob_${eagerImportId++}`;
564
- this.lines.unshift(
565
- importName ? `import { ${importName} as ${name} } from ${JSON.stringify(importPath)}` : `import * as ${name} from ${JSON.stringify(importPath)}`
566
- );
567
- code += `${JSON.stringify(item)}: ${name}, `;
568
- } else {
569
- let line = `${JSON.stringify(item)}: () => import(${JSON.stringify(importPath)})`;
570
- if (importName) {
571
- line += `.then(mod => mod.${importName})`;
572
- }
573
- code += `${line}, `;
574
- }
575
- }
576
- code += "}";
577
- return code;
578
- },
579
- formatImportPath(file) {
580
- const ext = import_node_path3.default.extname(file);
581
- let filename;
582
- if (ext === ".ts" && jsExtension) {
583
- filename = file.substring(0, file.length - ext.length) + ".js";
584
- } else if (ext === ".ts") {
585
- filename = file.substring(0, file.length - ext.length);
586
- } else {
587
- filename = file;
588
- }
589
- const importPath = slash(import_node_path3.default.relative(outDir, filename));
590
- return importPath.startsWith(".") ? importPath : `./${importPath}`;
591
- },
592
- toString() {
593
- return [...banner, ...this.lines].join("\n");
594
- }
595
- };
596
- }
597
- function normalizeViteGlobPath(file) {
598
- file = slash(file);
599
- if (file.startsWith("./")) return file;
600
- if (file.startsWith("/")) return `.${file}`;
601
- return `./${file}`;
602
- }
603
- function slash(path9) {
604
- const isExtendedLengthPath = path9.startsWith("\\\\?\\");
605
- if (isExtendedLengthPath) {
606
- return path9;
607
- }
608
- return path9.replaceAll("\\", "/");
609
- }
610
- function ident(code, tab = 1) {
611
- return code.split("\n").map((v) => " ".repeat(tab) + v).join("\n");
612
- }
613
- var import_node_path3, import_tinyglobby;
614
- var init_codegen = __esm({
615
- "src/utils/codegen/index.ts"() {
616
- "use strict";
617
- import_node_path3 = __toESM(require("path"), 1);
618
- import_tinyglobby = require("tinyglobby");
619
- }
620
- });
621
-
622
- // src/utils/fuma-matter.ts
623
- function fumaMatter(input) {
624
- const output = { matter: "", data: {}, content: input };
625
- const match = regex.exec(input);
626
- if (!match) {
627
- return output;
628
- }
629
- output.matter = match[0];
630
- output.content = input.slice(match[0].length);
631
- const loaded = (0, import_js_yaml.load)(match[1]);
632
- output.data = loaded ?? {};
633
- return output;
634
- }
635
- var import_js_yaml, regex;
636
- var init_fuma_matter = __esm({
637
- "src/utils/fuma-matter.ts"() {
638
- "use strict";
639
- import_js_yaml = require("js-yaml");
640
- regex = /^---\r?\n(.+?)\r?\n---\r?\n/s;
641
- }
642
- });
643
-
644
- // src/plugins/index-file.ts
645
- function indexFile(options = {}) {
646
- const {
647
- target = "default",
648
- addJsExtension,
649
- browser = true,
650
- dynamic = true
651
- } = options;
652
- let config;
653
- let dynamicCollections;
654
- function isDynamic(collection) {
655
- return collection.type === "docs" && collection.docs.dynamic || collection.type === "doc" && collection.dynamic;
656
- }
657
- return {
658
- name: "index-file",
659
- config(v) {
660
- config = v;
661
- dynamicCollections = config.collectionList.filter(isDynamic);
662
- },
663
- configureServer(server) {
664
- if (!server.watcher) return;
665
- server.watcher.on("all", async (event, file) => {
666
- if (dynamicCollections.length === 0) {
667
- if (target === "vite") return;
668
- if (target === "default" && event === "change") return;
669
- }
670
- const updatedCollection = config.collectionList.find(
671
- (collection) => collection.hasFile(file)
672
- );
673
- if (!updatedCollection) return;
674
- if (!isDynamic(updatedCollection)) {
675
- if (target === "vite") return;
676
- if (target === "default" && event === "change") return;
677
- }
678
- await this.core.emitAndWrite({
679
- filterPlugin: (plugin) => plugin.name === "index-file"
680
- });
681
- });
682
- },
683
- async emit() {
684
- const globCache = /* @__PURE__ */ new Map();
685
- const makeCodeGen = () => createCodegen({
686
- target,
687
- outDir: this.outDir,
688
- jsExtension: addJsExtension,
689
- globCache
690
- });
691
- async function toEmitEntry(path9, content) {
692
- return {
693
- path: path9,
694
- content: await content
695
- };
696
- }
697
- const out = [
698
- toEmitEntry(
699
- "server.ts",
700
- generateServerIndexFile(makeCodeGen(), config, this.configPath)
701
- )
702
- ];
703
- if (dynamic)
704
- out.push(
705
- toEmitEntry(
706
- "dynamic.ts",
707
- generateDynamicIndexFile(this.core, makeCodeGen())
708
- )
709
- );
710
- if (browser)
711
- out.push(
712
- toEmitEntry(
713
- "browser.ts",
714
- generateBrowserIndexFile(makeCodeGen(), config, this.configPath)
715
- )
716
- );
717
- return await Promise.all(out);
718
- }
719
- };
720
- }
721
- async function generateServerIndexFile(codegen, config, configPath) {
722
- codegen.lines.push(
723
- `import { fromConfig } from 'fumadocs-mdx/runtime/server';`,
724
- `import type * as Config from '${codegen.formatImportPath(configPath)}';`,
725
- "",
726
- `const create = fromConfig<typeof Config>();`
727
- );
728
- async function generateCollectionObject(collection) {
729
- switch (collection.type) {
730
- case "docs": {
731
- if (collection.docs.dynamic) return;
732
- if (collection.docs.async) {
733
- const [metaGlob2, headGlob, bodyGlob] = await Promise.all([
734
- generateMetaCollectionGlob(codegen, collection.meta, true),
735
- generateDocCollectionFrontmatterGlob(
736
- codegen,
737
- collection.docs,
738
- true
739
- ),
740
- generateDocCollectionGlob(codegen, collection.docs)
741
- ]);
742
- return `await create.docsLazy("${collection.name}", "${collection.dir}", ${metaGlob2}, ${headGlob}, ${bodyGlob})`;
743
- }
744
- const [metaGlob, docGlob] = await Promise.all([
745
- generateMetaCollectionGlob(codegen, collection.meta, true),
746
- generateDocCollectionGlob(codegen, collection.docs, true)
747
- ]);
748
- return `await create.docs("${collection.name}", "${collection.dir}", ${metaGlob}, ${docGlob})`;
749
- }
750
- case "doc":
751
- if (collection.dynamic) return;
752
- if (collection.async) {
753
- const [headGlob, bodyGlob] = await Promise.all([
754
- generateDocCollectionFrontmatterGlob(codegen, collection, true),
755
- generateDocCollectionGlob(codegen, collection)
756
- ]);
757
- return `await create.docLazy("${collection.name}", "${collection.dir}", ${headGlob}, ${bodyGlob})`;
758
- }
759
- return `await create.doc("${collection.name}", "${collection.dir}", ${await generateDocCollectionGlob(
760
- codegen,
761
- collection,
762
- true
763
- )})`;
764
- case "meta":
765
- return `await create.meta("${collection.name}", "${collection.dir}", ${await generateMetaCollectionGlob(
766
- codegen,
767
- collection,
768
- true
769
- )})`;
770
- }
771
- }
772
- await codegen.pushAsync(
773
- config.collectionList.map(async (collection) => {
774
- const obj = await generateCollectionObject(collection);
775
- if (!obj) return;
776
- return `
777
- export const ${collection.name} = ${obj};`;
778
- })
779
- );
780
- return codegen.toString();
781
- }
782
- async function generateDynamicIndexFile(core, codegen) {
783
- const { configPath } = core._options;
784
- codegen.lines.push(
785
- `import { fromConfigDynamic } from 'fumadocs-mdx/runtime/dynamic';`,
786
- `import * as Config from '${codegen.formatImportPath(configPath)}';`,
787
- "",
788
- `const create = await fromConfigDynamic(Config);`
789
- );
790
- async function generateCollectionObjectEntry(collection, file) {
791
- const fullPath = import_path.default.join(collection.dir, file);
792
- const content = await readFileWithCache(fullPath).catch(() => "");
793
- const parsed = fumaMatter(content);
794
- const data = await core.transformFrontmatter(
795
- {
796
- collection,
797
- filePath: fullPath,
798
- source: content
799
- },
800
- parsed.data
801
- );
802
- const hash = (0, import_crypto.createHash)("md5").update(content).digest("hex");
803
- const infoStr = [
804
- // make sure it's included in vercel/nft
805
- `absolutePath: path.resolve(${JSON.stringify(fullPath)})`
806
- ];
807
- for (const [k, v] of Object.entries({
808
- info: {
809
- fullPath,
810
- path: file
811
- },
812
- data,
813
- hash
814
- })) {
815
- infoStr.push(`${k}: ${JSON.stringify(v)}`);
816
- }
817
- return `{ ${infoStr.join(", ")} }`;
818
- }
819
- async function generateCollectionObject(parent) {
820
- let collection;
821
- if (parent.type === "doc") collection = parent;
822
- else if (parent.type === "docs") collection = parent.docs;
823
- if (!collection || !collection.dynamic) return;
824
- const files = await (0, import_tinyglobby2.glob)(collection.patterns, {
825
- cwd: collection.dir
826
- });
827
- const entries = await Promise.all(
828
- files.map((file) => generateCollectionObjectEntry(collection, file))
829
- );
830
- switch (parent.type) {
831
- case "docs": {
832
- const metaGlob = await generateMetaCollectionGlob(
833
- codegen,
834
- parent.meta,
835
- true
836
- );
837
- return `await create.docs("${parent.name}", "${parent.dir}", ${metaGlob}, ${entries.join(", ")})`;
838
- }
839
- case "doc":
840
- return `await create.doc("${collection.name}", "${collection.dir}", ${entries.join(", ")})`;
841
- }
842
- }
843
- await codegen.pushAsync(
844
- core.getConfig().collectionList.map(async (collection) => {
845
- const obj = await generateCollectionObject(collection);
846
- if (!obj) return;
847
- return `
848
- export const ${collection.name} = ${obj};`;
849
- })
850
- );
851
- return codegen.toString();
852
- }
853
- async function generateBrowserIndexFile(codegen, config, configPath) {
854
- codegen.lines.push(
855
- `import { fromConfig } from 'fumadocs-mdx/runtime/browser';`,
856
- `import type * as Config from '${codegen.formatImportPath(configPath)}';`,
857
- "",
858
- `const create = fromConfig<typeof Config>();`
859
- );
860
- async function generateCollectionObject(collection) {
861
- switch (collection.type) {
862
- case "docs": {
863
- if (collection.docs.dynamic) return;
864
- return generateCollectionObject(collection.docs);
865
- }
866
- case "doc":
867
- if (collection.dynamic) return;
868
- return `create.doc("${collection.name}", ${await generateDocCollectionGlob(codegen, collection)})`;
869
- }
870
- }
871
- codegen.lines.push("const browserCollections = {");
872
- await codegen.pushAsync(
873
- config.collectionList.map(async (collection) => {
874
- const obj = await generateCollectionObject(collection);
875
- if (!obj) return;
876
- return ident(`${collection.name}: ${obj},`);
877
- })
878
- );
879
- codegen.lines.push("};", "export default browserCollections;");
880
- return codegen.toString();
881
- }
882
- function generateDocCollectionFrontmatterGlob(codegen, collection, eager = false) {
883
- return codegen.generateGlobImport(collection.patterns, {
884
- query: {
885
- collection: collection.name,
886
- only: "frontmatter"
887
- },
888
- import: "frontmatter",
889
- base: collection.dir,
890
- eager
891
- });
892
- }
893
- function generateDocCollectionGlob(codegen, collection, eager = false) {
894
- return codegen.generateGlobImport(collection.patterns, {
895
- query: {
896
- collection: collection.name
897
- },
898
- base: collection.dir,
899
- eager
900
- });
901
- }
902
- function generateMetaCollectionGlob(codegen, collection, eager = false) {
903
- return codegen.generateGlobImport(collection.patterns, {
904
- query: {
905
- collection: collection.name
906
- },
907
- import: "default",
908
- base: collection.dir,
909
- eager
910
- });
911
- }
912
- var import_path, import_tinyglobby2, import_crypto;
913
- var init_index_file = __esm({
914
- "src/plugins/index-file.ts"() {
915
- "use strict";
916
- import_path = __toESM(require("path"), 1);
917
- init_codegen();
918
- import_tinyglobby2 = require("tinyglobby");
919
- init_cache();
920
- import_crypto = require("crypto");
921
- init_fuma_matter();
922
- }
923
- });
924
-
925
- // src/next/index.ts
926
- var next_exports = {};
927
- __export(next_exports, {
928
- createMDX: () => createMDX,
929
- postInstall: () => postInstall
930
- });
931
- function createMDX(createOptions = {}) {
932
- const core = createNextCore(applyDefaults(createOptions));
933
- const isDev = process.env.NODE_ENV === "development";
934
- if (process.env._FUMADOCS_MDX !== "1") {
935
- process.env._FUMADOCS_MDX = "1";
936
- void init(isDev, core);
937
- }
938
- return (nextConfig = {}) => {
939
- const loaderOptions = {
940
- ...core._options,
941
- compiledConfigPath: core.getCompiledConfigPath(),
942
- isDev
943
- };
944
- const turbopack = {
945
- ...nextConfig.turbopack,
946
- rules: {
947
- ...nextConfig.turbopack?.rules,
948
- "*.{md,mdx}": {
949
- loaders: [
950
- {
951
- loader: "fumadocs-mdx/loader-mdx",
952
- options: loaderOptions
953
- }
954
- ],
955
- as: "*.js"
956
- },
957
- "*.json": {
958
- loaders: [
959
- {
960
- loader: "fumadocs-mdx/loader-meta",
961
- options: loaderOptions
962
- }
963
- ],
964
- as: "*.json"
965
- },
966
- "*.yaml": {
967
- loaders: [
968
- {
969
- loader: "fumadocs-mdx/loader-meta",
970
- options: loaderOptions
971
- }
972
- ],
973
- as: "*.js"
974
- }
975
- }
976
- };
977
- return {
978
- ...nextConfig,
979
- turbopack,
980
- pageExtensions: nextConfig.pageExtensions ?? defaultPageExtensions,
981
- webpack: (config, options) => {
982
- config.resolve ||= {};
983
- config.module ||= {};
984
- config.module.rules ||= [];
985
- config.module.rules.push(
986
- {
987
- test: mdxLoaderGlob,
988
- use: [
989
- options.defaultLoaders.babel,
990
- {
991
- loader: "fumadocs-mdx/loader-mdx",
992
- options: loaderOptions
993
- }
994
- ]
995
- },
996
- {
997
- test: metaLoaderGlob,
998
- use: [
999
- options.defaultLoaders.babel,
1000
- {
1001
- loader: "fumadocs-mdx/loader-meta",
1002
- options: loaderOptions
1003
- }
1004
- ]
1005
- }
1006
- );
1007
- config.plugins ||= [];
1008
- return nextConfig.webpack?.(config, options) ?? config;
1009
- }
1010
- };
1011
- };
1012
- }
1013
- async function init(dev, core) {
1014
- async function initOrReload() {
1015
- await core.init({
1016
- config: loadConfig(core, true)
1017
- });
1018
- await core.emitAndWrite();
1019
- }
1020
- async function devServer() {
1021
- const { FSWatcher } = await import("chokidar");
1022
- const watcher = new FSWatcher({
1023
- ignoreInitial: true,
1024
- persistent: true,
1025
- ignored: [core._options.outDir]
1026
- });
1027
- watcher.add(core._options.configPath);
1028
- for (const collection of core.getConfig().collectionList) {
1029
- if (collection.type === "docs") {
1030
- watcher.add(collection.docs.dir);
1031
- watcher.add(collection.meta.dir);
1032
- } else {
1033
- watcher.add(collection.dir);
1034
- }
1035
- }
1036
- watcher.on("ready", () => {
1037
- console.log("[MDX] started dev server");
1038
- });
1039
- watcher.on("all", async (_event, file) => {
1040
- if (path5.resolve(file) === path5.resolve(core._options.configPath)) {
1041
- watcher.removeAllListeners();
1042
- await watcher.close();
1043
- await initOrReload();
1044
- console.log("[MDX] restarting dev server");
1045
- await devServer();
1046
- }
1047
- });
1048
- process.on("exit", () => {
1049
- if (watcher.closed) return;
1050
- console.log("[MDX] closing dev server");
1051
- void watcher.close();
1052
- });
1053
- await core.initServer({ watcher });
1054
- }
1055
- await initOrReload();
1056
- if (dev) {
1057
- await devServer();
1058
- }
1059
- }
1060
- async function postInstall(options) {
1061
- const core = createNextCore(applyDefaults(options));
1062
- await core.init({
1063
- config: loadConfig(core, true)
1064
- });
1065
- await core.emitAndWrite();
1066
- }
1067
- function applyDefaults(options) {
1068
- return {
1069
- index: {},
1070
- outDir: options.outDir ?? _Defaults.outDir,
1071
- configPath: options.configPath ?? _Defaults.configPath
1072
- };
1073
- }
1074
- function createNextCore(options) {
1075
- const core = createCore(
1076
- {
1077
- environment: "next",
1078
- outDir: options.outDir,
1079
- configPath: options.configPath
1080
- },
1081
- [options.index && indexFile(options.index)]
1082
- );
1083
- return {
1084
- ...core,
1085
- async emitAndWrite(...args) {
1086
- try {
1087
- await core.emitAndWrite(...args);
1088
- } catch (err) {
1089
- if (err instanceof ValidationError) {
1090
- console.error(await err.toStringFormatted());
1091
- } else {
1092
- console.error(err);
1093
- }
1094
- }
1095
- }
1096
- };
1097
- }
1098
- var path5, defaultPageExtensions;
1099
- var init_next = __esm({
1100
- "src/next/index.ts"() {
1101
- "use strict";
1102
- path5 = __toESM(require("path"), 1);
1103
- init_load_from_file();
1104
- init_validation();
1105
- init_core();
1106
- init_loaders();
1107
- init_index_file();
1108
- defaultPageExtensions = ["mdx", "md", "jsx", "js", "tsx", "ts"];
1109
- }
1110
- });
1111
-
1112
- // src/loaders/mdx/remark-unravel.ts
1113
- function remarkMarkAndUnravel() {
1114
- return (tree) => {
1115
- (0, import_unist_util_visit.visit)(tree, function(node, index, parent) {
1116
- let offset = -1;
1117
- let all = true;
1118
- let oneOrMore = false;
1119
- if (parent && typeof index === "number" && node.type === "paragraph") {
1120
- const children = node.children;
1121
- while (++offset < children.length) {
1122
- const child = children[offset];
1123
- if (child.type === "mdxJsxTextElement" || child.type === "mdxTextExpression") {
1124
- oneOrMore = true;
1125
- } else if (child.type === "text" && child.value.trim().length === 0) {
1126
- } else {
1127
- all = false;
1128
- break;
1129
- }
1130
- }
1131
- if (all && oneOrMore) {
1132
- offset = -1;
1133
- const newChildren = [];
1134
- while (++offset < children.length) {
1135
- const child = children[offset];
1136
- if (child.type === "mdxJsxTextElement") {
1137
- child.type = "mdxJsxFlowElement";
1138
- }
1139
- if (child.type === "mdxTextExpression") {
1140
- child.type = "mdxFlowExpression";
1141
- }
1142
- if (child.type === "text" && /^[\t\r\n ]+$/.test(String(child.value))) {
1143
- } else {
1144
- newChildren.push(child);
1145
- }
1146
- }
1147
- parent.children.splice(index, 1, ...newChildren);
1148
- return index;
1149
- }
1150
- }
1151
- });
1152
- };
1153
- }
1154
- var import_unist_util_visit;
1155
- var init_remark_unravel = __esm({
1156
- "src/loaders/mdx/remark-unravel.ts"() {
1157
- "use strict";
1158
- import_unist_util_visit = require("unist-util-visit");
1159
- }
1160
- });
1161
-
1162
- // src/loaders/mdx/mdast-utils.ts
1163
- function flattenNode(node) {
1164
- if ("children" in node)
1165
- return node.children.map((child) => flattenNode(child)).join("");
1166
- if ("value" in node) return node.value;
1167
- return "";
1168
- }
1169
- var init_mdast_utils = __esm({
1170
- "src/loaders/mdx/mdast-utils.ts"() {
1171
- "use strict";
1172
- }
1173
- });
1174
-
1175
- // src/loaders/mdx/remark-include.ts
1176
- function isElementLike(node) {
1177
- return ElementLikeTypes.includes(node.type);
1178
- }
1179
- function parseElementAttributes(element) {
1180
- if (Array.isArray(element.attributes)) {
1181
- const attributes = {};
1182
- for (const attr of element.attributes) {
1183
- if (attr.type === "mdxJsxAttribute" && (typeof attr.value === "string" || attr.value === null)) {
1184
- attributes[attr.name] = attr.value;
1185
- }
1186
- }
1187
- return attributes;
1188
- }
1189
- return element.attributes ?? {};
1190
- }
1191
- function parseSpecifier(specifier) {
1192
- const idx = specifier.lastIndexOf("#");
1193
- if (idx === -1) return { file: specifier };
1194
- return {
1195
- file: specifier.slice(0, idx),
1196
- section: specifier.slice(idx + 1)
1197
- };
1198
- }
1199
- function extractSection(root, section) {
1200
- let nodes;
1201
- let capturingHeadingContent = false;
1202
- (0, import_unist_util_visit2.visit)(root, (node) => {
1203
- if (node.type === "heading") {
1204
- if (capturingHeadingContent) {
1205
- return false;
1206
- }
1207
- if (node.data?.hProperties?.id === section) {
1208
- capturingHeadingContent = true;
1209
- nodes = [node];
1210
- return "skip";
1211
- }
1212
- return;
1213
- }
1214
- if (capturingHeadingContent) {
1215
- nodes?.push(node);
1216
- return "skip";
1217
- }
1218
- if (isElementLike(node) && node.name === "section") {
1219
- const attributes = parseElementAttributes(node);
1220
- if (attributes.id === section) {
1221
- nodes = node.children;
1222
- return false;
1223
- }
1224
- }
1225
- });
1226
- if (nodes)
1227
- return {
1228
- type: "root",
1229
- children: nodes
1230
- };
1231
- }
1232
- function remarkInclude() {
1233
- const TagName = "include";
1234
- const embedContent = async (file, heading, params, data) => {
1235
- let content;
1236
- try {
1237
- content = (await fs3.readFile(file)).toString();
1238
- } catch (e) {
1239
- throw new Error(
1240
- `failed to read file ${file}
1241
- ${e instanceof Error ? e.message : String(e)}`,
1242
- { cause: e }
1243
- );
1244
- }
1245
- const ext = path6.extname(file);
1246
- data._compiler?.addDependency(file);
1247
- if (params.lang || ext !== ".md" && ext !== ".mdx") {
1248
- const lang = params.lang ?? ext.slice(1);
1249
- return {
1250
- type: "code",
1251
- lang,
1252
- meta: params.meta,
1253
- value: content,
1254
- data: {}
1255
- };
1256
- }
1257
- const parser = data._getProcessor ? data._getProcessor(ext === ".mdx" ? "mdx" : "md") : this;
1258
- const parsed = fumaMatter(content);
1259
- let mdast = parser.parse({
1260
- path: file,
1261
- value: parsed.content,
1262
- data: { frontmatter: parsed.data }
1263
- });
1264
- const baseProcessor = (0, import_unified.unified)().use(remarkMarkAndUnravel);
1265
- if (heading) {
1266
- const extracted = extractSection(
1267
- await baseProcessor.use(import_mdx_plugins.remarkHeading).run(mdast),
1268
- heading
1269
- );
1270
- if (!extracted)
1271
- throw new Error(
1272
- `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.`
1273
- );
1274
- mdast = extracted;
1275
- } else {
1276
- mdast = await baseProcessor.run(mdast);
1277
- }
1278
- await update(mdast, path6.dirname(file), data);
1279
- return mdast;
1280
- };
1281
- async function update(tree, directory, data) {
1282
- const queue = [];
1283
- (0, import_unist_util_visit2.visit)(tree, ElementLikeTypes, (_node, _, parent) => {
1284
- const node = _node;
1285
- if (node.name !== TagName) return;
1286
- const specifier = flattenNode(node);
1287
- if (specifier.length === 0) return "skip";
1288
- const attributes = parseElementAttributes(node);
1289
- const { file: relativePath, section } = parseSpecifier(specifier);
1290
- const file = path6.resolve(
1291
- "cwd" in attributes ? process.cwd() : directory,
1292
- relativePath
1293
- );
1294
- queue.push(
1295
- embedContent(file, section, attributes, data).then((replace) => {
1296
- Object.assign(
1297
- parent && parent.type === "paragraph" ? parent : node,
1298
- replace
1299
- );
1300
- })
1301
- );
1302
- return "skip";
1303
- });
1304
- await Promise.all(queue);
1305
- }
1306
- return async (tree, file) => {
1307
- await update(tree, path6.dirname(file.path), file.data);
1308
- };
1309
- }
1310
- var import_unified, import_unist_util_visit2, path6, fs3, import_mdx_plugins, ElementLikeTypes;
1311
- var init_remark_include = __esm({
1312
- "src/loaders/mdx/remark-include.ts"() {
1313
- "use strict";
1314
- import_unified = require("unified");
1315
- import_unist_util_visit2 = require("unist-util-visit");
1316
- path6 = __toESM(require("path"), 1);
1317
- fs3 = __toESM(require("fs/promises"), 1);
1318
- init_fuma_matter();
1319
- import_mdx_plugins = require("fumadocs-core/mdx-plugins");
1320
- init_remark_unravel();
1321
- init_mdast_utils();
1322
- ElementLikeTypes = [
1323
- "mdxJsxFlowElement",
1324
- "mdxJsxTextElement",
1325
- "containerDirective",
1326
- "textDirective",
1327
- "leafDirective"
1328
- ];
1329
- }
1330
- });
1331
-
1332
- // src/loaders/mdx/remark-postprocess.ts
1333
- function remarkPostprocess({
1334
- _format,
1335
- includeProcessedMarkdown = false,
1336
- includeMDAST = false,
1337
- extractLinkReferences = false,
1338
- valueToExport = []
1339
- }) {
1340
- let _stringifyProcessor;
1341
- const getStringifyProcessor = () => {
1342
- return _stringifyProcessor ??= _format === "mdx" ? this : (
1343
- // force Markdown processor to stringify MDX nodes
1344
- this().use(import_remark_mdx.default).freeze()
1345
- );
1346
- };
1347
- return (tree, file) => {
1348
- const frontmatter = file.data.frontmatter ??= {};
1349
- if (!frontmatter.title) {
1350
- (0, import_unist_util_visit3.visit)(tree, "heading", (node) => {
1351
- if (node.depth === 1) {
1352
- frontmatter.title = flattenNode(node);
1353
- return false;
1354
- }
1355
- });
1356
- }
1357
- file.data["mdx-export"] ??= [];
1358
- if (extractLinkReferences) {
1359
- const urls = [];
1360
- (0, import_unist_util_visit3.visit)(tree, "link", (node) => {
1361
- urls.push({
1362
- href: node.url
1363
- });
1364
- return "skip";
1365
- });
1366
- file.data["mdx-export"].push({
1367
- name: "extractedReferences",
1368
- value: urls
1369
- });
1370
- }
1371
- if (includeProcessedMarkdown) {
1372
- const processor = getStringifyProcessor();
1373
- const markdown = (0, import_mdast_util_to_markdown.toMarkdown)(tree, {
1374
- ...processor.data("settings"),
1375
- // from https://github.com/remarkjs/remark/blob/main/packages/remark-stringify/lib/index.js
1376
- extensions: processor.data("toMarkdownExtensions") || []
1377
- });
1378
- file.data["mdx-export"].push({
1379
- name: "_markdown",
1380
- value: markdown
1381
- });
1382
- }
1383
- if (includeMDAST) {
1384
- const options = includeMDAST === true ? {} : includeMDAST;
1385
- const mdast = JSON.stringify(
1386
- options.removePosition ? (0, import_unist_util_remove_position.removePosition)(structuredClone(tree)) : tree
1387
- );
1388
- file.data["mdx-export"].push({
1389
- name: "_mdast",
1390
- value: mdast
1391
- });
1392
- }
1393
- for (const { name, value } of file.data["mdx-export"]) {
1394
- tree.children.unshift(getMdastExport(name, value));
1395
- }
1396
- file.data["mdx-export"] = [];
1397
- for (const name of valueToExport) {
1398
- if (!(name in file.data)) continue;
1399
- tree.children.unshift(getMdastExport(name, file.data[name]));
1400
- }
1401
- };
1402
- }
1403
- function getMdastExport(name, value) {
1404
- return {
1405
- type: "mdxjsEsm",
1406
- value: "",
1407
- data: {
1408
- estree: {
1409
- type: "Program",
1410
- sourceType: "module",
1411
- body: [
1412
- {
1413
- type: "ExportNamedDeclaration",
1414
- attributes: [],
1415
- specifiers: [],
1416
- source: null,
1417
- declaration: {
1418
- type: "VariableDeclaration",
1419
- kind: "let",
1420
- declarations: [
1421
- {
1422
- type: "VariableDeclarator",
1423
- id: {
1424
- type: "Identifier",
1425
- name
1426
- },
1427
- init: (0, import_estree_util_value_to_estree.valueToEstree)(value)
1428
- }
1429
- ]
1430
- }
1431
- }
1432
- ]
1433
- }
1434
- }
1435
- };
1436
- }
1437
- var import_unist_util_visit3, import_mdast_util_to_markdown, import_estree_util_value_to_estree, import_unist_util_remove_position, import_remark_mdx;
1438
- var init_remark_postprocess = __esm({
1439
- "src/loaders/mdx/remark-postprocess.ts"() {
1440
- "use strict";
1441
- import_unist_util_visit3 = require("unist-util-visit");
1442
- import_mdast_util_to_markdown = require("mdast-util-to-markdown");
1443
- import_estree_util_value_to_estree = require("estree-util-value-to-estree");
1444
- import_unist_util_remove_position = require("unist-util-remove-position");
1445
- import_remark_mdx = __toESM(require("remark-mdx"), 1);
1446
- init_mdast_utils();
1447
- }
1448
- });
1449
-
1450
- // src/loaders/mdx/build-mdx.ts
1451
- var build_mdx_exports = {};
1452
- __export(build_mdx_exports, {
1453
- buildMDX: () => buildMDX
1454
- });
1455
- async function buildMDX(core, collection, {
1456
- filePath,
1457
- frontmatter,
1458
- source,
1459
- _compiler,
1460
- environment,
1461
- isDevelopment
1462
- }) {
1463
- const mdxOptions = await core.getConfig().getMDXOptions(collection, environment);
1464
- function getProcessor(format) {
1465
- const cache = core.cache;
1466
- const key = `build-mdx:${collection?.name ?? "global"}:${format}`;
1467
- let processor = cache.get(key);
1468
- if (!processor) {
1469
- const postprocessOptions = {
1470
- _format: format,
1471
- ...collection?.postprocess,
1472
- valueToExport: [
1473
- ...collection?.postprocess?.valueToExport ?? [],
1474
- "structuredData",
1475
- "frontmatter"
1476
- ]
1477
- };
1478
- processor = (0, import_mdx.createProcessor)({
1479
- outputFormat: "program",
1480
- development: isDevelopment,
1481
- ...mdxOptions,
1482
- remarkPlugins: [
1483
- remarkInclude,
1484
- ...mdxOptions.remarkPlugins ?? [],
1485
- [remarkPostprocess, postprocessOptions]
1486
- ],
1487
- format
1488
- });
1489
- cache.set(key, processor);
1490
- }
1491
- return processor;
1492
- }
1493
- let vfile = new import_vfile.VFile({
1494
- value: source,
1495
- path: filePath,
1496
- data: { frontmatter, _compiler, _getProcessor: getProcessor }
1497
- });
1498
- if (collection) {
1499
- vfile = await core.transformVFile({ collection, filePath, source }, vfile);
1500
- }
1501
- return getProcessor(filePath.endsWith(".mdx") ? "mdx" : "md").process(vfile);
1502
- }
1503
- var import_mdx, import_vfile;
1504
- var init_build_mdx = __esm({
1505
- "src/loaders/mdx/build-mdx.ts"() {
1506
- "use strict";
1507
- import_mdx = require("@mdx-js/mdx");
1508
- import_vfile = require("vfile");
1509
- init_remark_include();
1510
- init_remark_postprocess();
1511
- }
1512
- });
1513
-
1514
- // src/loaders/mdx/index.ts
1515
- function createMdxLoader(configLoader) {
1516
- return {
1517
- test: mdxLoaderGlob,
1518
- async load({
1519
- getSource,
1520
- development: isDevelopment,
1521
- query,
1522
- compiler,
1523
- filePath
1524
- }) {
1525
- const config = await configLoader.getConfig();
1526
- const value = await getSource();
1527
- const matter = fumaMatter(value);
1528
- const parsed = querySchema.parse(query);
1529
- let after;
1530
- if (!isDevelopment && config.global.experimentalBuildCache) {
1531
- const cacheDir = config.global.experimentalBuildCache;
1532
- const cacheKey = `${parsed.hash}_${parsed.collection ?? "global"}_${generateCacheHash(filePath)}`;
1533
- const cached = await import_promises3.default.readFile(import_node_path4.default.join(cacheDir, cacheKey)).then((content) => cacheEntry.parse(JSON.parse(content.toString()))).catch(() => null);
1534
- if (cached && cached.hash === generateCacheHash(value)) return cached;
1535
- after = async () => {
1536
- await import_promises3.default.mkdir(cacheDir, { recursive: true });
1537
- await import_promises3.default.writeFile(
1538
- import_node_path4.default.join(cacheDir, cacheKey),
1539
- JSON.stringify({
1540
- ...out,
1541
- hash: generateCacheHash(value)
1542
- })
1543
- );
1544
- };
1545
- }
1546
- const collection = parsed.collection ? config.getCollection(parsed.collection) : void 0;
1547
- let docCollection;
1548
- switch (collection?.type) {
1549
- case "doc":
1550
- docCollection = collection;
1551
- break;
1552
- case "docs":
1553
- docCollection = collection.docs;
1554
- break;
1555
- }
1556
- if (docCollection) {
1557
- matter.data = await configLoader.core.transformFrontmatter(
1558
- { collection: docCollection, filePath, source: value },
1559
- matter.data
1560
- );
1561
- }
1562
- if (parsed.only === "frontmatter") {
1563
- return {
1564
- code: `export const frontmatter = ${JSON.stringify(matter.data)}`,
1565
- map: null
1566
- };
1567
- }
1568
- const lineOffset = isDevelopment ? countLines(matter.matter) : 0;
1569
- const { buildMDX: buildMDX2 } = await Promise.resolve().then(() => (init_build_mdx(), build_mdx_exports));
1570
- const compiled = await buildMDX2(configLoader.core, docCollection, {
1571
- isDevelopment,
1572
- source: "\n".repeat(lineOffset) + matter.content,
1573
- filePath,
1574
- frontmatter: matter.data,
1575
- _compiler: compiler,
1576
- environment: "bundler"
1577
- });
1578
- const out = {
1579
- code: String(compiled.value),
1580
- map: compiled.map
1581
- };
1582
- await after?.();
1583
- return out;
1584
- }
1585
- };
1586
- }
1587
- function generateCacheHash(input) {
1588
- return (0, import_node_crypto.createHash)("md5").update(input).digest("hex");
1589
- }
1590
- function countLines(s) {
1591
- let num = 0;
1592
- for (const c of s) {
1593
- if (c === "\n") num++;
1594
- }
1595
- return num;
1596
- }
1597
- var import_zod, import_promises3, import_node_path4, import_node_crypto, querySchema, cacheEntry;
1598
- var init_mdx = __esm({
1599
- "src/loaders/mdx/index.ts"() {
1600
- "use strict";
1601
- init_fuma_matter();
1602
- import_zod = require("zod");
1603
- import_promises3 = __toESM(require("fs/promises"), 1);
1604
- import_node_path4 = __toESM(require("path"), 1);
1605
- import_node_crypto = require("crypto");
1606
- init_loaders();
1607
- querySchema = import_zod.z.object({
1608
- only: import_zod.z.literal(["frontmatter", "all"]).default("all"),
1609
- collection: import_zod.z.string().optional()
1610
- }).loose();
1611
- cacheEntry = import_zod.z.object({
1612
- code: import_zod.z.string(),
1613
- map: import_zod.z.any().optional(),
1614
- hash: import_zod.z.string().optional()
1615
- });
1616
- }
1617
- });
1618
-
1619
- // src/loaders/adapter.ts
1620
- function toVite(loader) {
1621
- return {
1622
- filter(id) {
1623
- return !loader.test || loader.test.test(id);
1624
- },
1625
- async transform(value, id) {
1626
- const [file, query = ""] = id.split("?", 2);
1627
- const result = await loader.load({
1628
- filePath: file,
1629
- query: (0, import_node_querystring.parse)(query),
1630
- getSource() {
1631
- return value;
1632
- },
1633
- development: this.environment.mode === "dev",
1634
- compiler: {
1635
- addDependency: (file2) => {
1636
- this.addWatchFile(file2);
1637
- }
1638
- }
1639
- });
1640
- if (result === null) return null;
1641
- return {
1642
- code: result.code,
1643
- map: result.map
1644
- };
1645
- }
1646
- };
1647
- }
1648
- var import_node_url2, import_promises4, import_node_querystring, import_node_path5, import_node_fs;
1649
- var init_adapter = __esm({
1650
- "src/loaders/adapter.ts"() {
1651
- "use strict";
1652
- import_node_url2 = require("url");
1653
- import_promises4 = __toESM(require("fs/promises"), 1);
1654
- import_node_querystring = require("querystring");
1655
- init_validation();
1656
- import_node_path5 = __toESM(require("path"), 1);
1657
- import_node_fs = require("fs");
1658
- }
1659
- });
1660
-
1661
- // src/loaders/config.ts
1662
- function createIntegratedConfigLoader(core) {
1663
- return {
1664
- core,
1665
- getConfig() {
1666
- return core.getConfig();
1667
- }
1668
- };
1669
- }
1670
- var import_promises5;
1671
- var init_config = __esm({
1672
- "src/loaders/config.ts"() {
1673
- "use strict";
1674
- import_promises5 = __toESM(require("fs/promises"), 1);
1675
- }
1676
- });
1677
-
1678
- // src/loaders/meta.ts
1679
- function createMetaLoader(configLoader, resolve3 = {}) {
1680
- const { json: resolveJson = "js", yaml: resolveYaml = "js" } = resolve3;
1681
- function parse2(filePath, source) {
1682
- try {
1683
- if (filePath.endsWith(".json")) return JSON.parse(source);
1684
- if (filePath.endsWith(".yaml")) return (0, import_js_yaml2.load)(source);
1685
- } catch (e) {
1686
- throw new Error(`invalid data in ${filePath}`, { cause: e });
1687
- }
1688
- throw new Error("Unknown file type " + filePath);
1689
- }
1690
- function onMeta(source, { filePath, query }) {
1691
- const parsed = querySchema2.safeParse(query);
1692
- if (!parsed.success || !parsed.data.collection) return null;
1693
- const collectionName = parsed.data.collection;
1694
- return async () => {
1695
- const config = await configLoader.getConfig();
1696
- const collection = config.getCollection(collectionName);
1697
- let metaCollection;
1698
- switch (collection?.type) {
1699
- case "meta":
1700
- metaCollection = collection;
1701
- break;
1702
- case "docs":
1703
- metaCollection = collection.meta;
1704
- break;
1705
- }
1706
- const data = parse2(filePath, source);
1707
- if (!metaCollection) return data;
1708
- return configLoader.core.transformMeta(
1709
- {
1710
- collection: metaCollection,
1711
- filePath,
1712
- source
1713
- },
1714
- data
1715
- );
1716
- };
1717
- }
1718
- return {
1719
- test: metaLoaderGlob,
1720
- async load(input) {
1721
- const result = onMeta(await input.getSource(), input);
1722
- if (result === null) return null;
1723
- const data = await result();
1724
- if (input.filePath.endsWith(".json")) {
1725
- return {
1726
- code: resolveJson === "json" ? JSON.stringify(data) : `export default ${JSON.stringify(data)}`
1727
- };
1728
- } else {
1729
- return {
1730
- code: resolveYaml === "yaml" ? (0, import_js_yaml2.dump)(data) : `export default ${JSON.stringify(data)}`
1731
- };
1732
- }
1733
- },
1734
- bun: {
1735
- load(source, input) {
1736
- const result = onMeta(source, input);
1737
- if (result === null)
1738
- return {
1739
- loader: "object",
1740
- exports: parse2(input.filePath, source)
1741
- };
1742
- return result().then((data) => ({
1743
- loader: "object",
1744
- exports: { default: data }
1745
- }));
1746
- }
1747
- }
1748
- };
1749
- }
1750
- var import_js_yaml2, import_zod2, querySchema2;
1751
- var init_meta = __esm({
1752
- "src/loaders/meta.ts"() {
1753
- "use strict";
1754
- import_js_yaml2 = require("js-yaml");
1755
- import_zod2 = require("zod");
1756
- init_loaders();
1757
- querySchema2 = import_zod2.z.object({
1758
- collection: import_zod2.z.string().optional()
1759
- }).loose();
1760
- }
1761
- });
1762
-
1763
- // src/vite/index.ts
1764
- var vite_exports = {};
1765
- __export(vite_exports, {
1766
- default: () => mdx,
1767
- postInstall: () => postInstall2
1768
- });
1769
- async function mdx(config, pluginOptions = {}) {
1770
- const options = applyDefaults2(pluginOptions);
1771
- const core = await createViteCore(options).init({
1772
- config: buildConfig(config)
1773
- });
1774
- const configLoader = createIntegratedConfigLoader(core);
1775
- const mdxLoader = toVite(createMdxLoader(configLoader));
1776
- const metaLoader = toVite(
1777
- createMetaLoader(configLoader, {
1778
- // vite has built-in plugin for JSON files
1779
- json: "json"
1780
- })
1781
- );
1782
- return {
1783
- name: "fumadocs-mdx",
1784
- // needed, otherwise other plugins will be executed before our `transform`.
1785
- enforce: "pre",
1786
- config(config2) {
1787
- if (!options.updateViteConfig) return config2;
1788
- return (0, import_vite.mergeConfig)(config2, {
1789
- optimizeDeps: {
1790
- exclude: FumadocsDeps
1791
- },
1792
- resolve: {
1793
- noExternal: FumadocsDeps,
1794
- dedupe: FumadocsDeps
1795
- }
1796
- });
1797
- },
1798
- async buildStart() {
1799
- await core.emitAndWrite();
1800
- },
1801
- async configureServer(server) {
1802
- await core.initServer({
1803
- watcher: server.watcher
1804
- });
1805
- },
1806
- async transform(value, id) {
1807
- try {
1808
- if (metaLoader.filter(id)) {
1809
- return await metaLoader.transform.call(this, value, id);
1810
- }
1811
- if (mdxLoader.filter(id)) {
1812
- return await mdxLoader.transform.call(this, value, id);
1813
- }
1814
- } catch (e) {
1815
- if (e instanceof ValidationError) {
1816
- throw new Error(await e.toStringFormatted());
1817
- }
1818
- throw e;
1819
- }
1820
- }
1821
- };
1822
- }
1823
- async function postInstall2(pluginOptions = {}) {
1824
- const { loadConfig: loadConfig2 } = await Promise.resolve().then(() => (init_load_from_file(), load_from_file_exports));
1825
- const core = createViteCore(applyDefaults2(pluginOptions));
1826
- await core.init({
1827
- config: loadConfig2(core, true)
1828
- });
1829
- await core.emitAndWrite();
1830
- }
1831
- function createViteCore({
1832
- index,
1833
- configPath,
1834
- outDir
1835
- }) {
1836
- if (index === true) index = {};
1837
- return createCore(
1838
- {
1839
- environment: "vite",
1840
- configPath,
1841
- outDir
1842
- },
1843
- [
1844
- index && indexFile({
1845
- ...index,
1846
- target: index.target ?? "vite"
1847
- })
1848
- ]
1849
- );
1850
- }
1851
- function applyDefaults2(options) {
1852
- return {
1853
- updateViteConfig: options.updateViteConfig ?? true,
1854
- index: options.index ?? true,
1855
- configPath: options.configPath ?? _Defaults.configPath,
1856
- outDir: options.outDir ?? _Defaults.outDir
1857
- };
1858
- }
1859
- var import_vite, FumadocsDeps;
1860
- var init_vite = __esm({
1861
- "src/vite/index.ts"() {
1862
- "use strict";
1863
- import_vite = require("vite");
1864
- init_build();
1865
- init_validation();
1866
- init_mdx();
1867
- init_adapter();
1868
- init_core();
1869
- init_config();
1870
- init_meta();
1871
- init_index_file();
1872
- FumadocsDeps = ["fumadocs-core", "fumadocs-ui", "fumadocs-openapi"];
1873
- }
1874
- });
1875
-
1876
- // src/bin.ts
1877
- var import_node_fs2 = require("fs");
1878
- async function start() {
1879
- const [configPath, outDir] = process.argv.slice(2);
1880
- const isNext = (0, import_node_fs2.existsSync)("next.config.js") || (0, import_node_fs2.existsSync)("next.config.mjs") || (0, import_node_fs2.existsSync)("next.config.mts") || (0, import_node_fs2.existsSync)("next.config.ts");
1881
- if (isNext) {
1882
- const { postInstall: postInstall3 } = await Promise.resolve().then(() => (init_next(), next_exports));
1883
- await postInstall3({ configPath, outDir });
1884
- } else {
1885
- const { postInstall: postInstall3 } = await Promise.resolve().then(() => (init_vite(), vite_exports));
1886
- await postInstall3({ configPath, outDir });
1887
- }
1888
- }
1889
- void start();