fumadocs-mdx 13.0.0 → 13.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 (55) hide show
  1. package/dist/bin.cjs +294 -257
  2. package/dist/{build-mdx-CzrQDBRZ.d.ts → build-mdx-CCNr86q6.d.ts} +1 -1
  3. package/dist/{build-mdx-BHG-_uxo.d.cts → build-mdx-D-r3_eQL.d.cts} +1 -1
  4. package/dist/bun/index.cjs +114 -34
  5. package/dist/bun/index.d.cts +8 -3
  6. package/dist/bun/index.d.ts +8 -3
  7. package/dist/bun/index.js +20 -10
  8. package/dist/{chunk-6Y5JDZHD.js → chunk-CXA4JO4Z.js} +1 -21
  9. package/dist/chunk-EELYB2XC.js +207 -0
  10. package/dist/{chunk-CEA6MYJU.js → chunk-XQ5O7IPO.js} +29 -27
  11. package/dist/chunk-XZY2AWJI.js +81 -0
  12. package/dist/{chunk-XV5Z4BFL.js → chunk-YVCR6FUH.js} +1 -1
  13. package/dist/config/index.d.cts +2 -2
  14. package/dist/config/index.d.ts +2 -2
  15. package/dist/{define-BCNh3n4O.d.cts → core-B6j6Fxse.d.cts} +101 -38
  16. package/dist/{define-bck_EB4t.d.ts → core-B6j6Fxse.d.ts} +101 -38
  17. package/dist/index.d.cts +7 -2
  18. package/dist/index.d.ts +7 -2
  19. package/dist/next/index.cjs +195 -163
  20. package/dist/next/index.js +79 -71
  21. package/dist/node/loader.cjs +120 -35
  22. package/dist/node/loader.js +10 -5
  23. package/dist/plugins/json-schema.cjs +103 -2
  24. package/dist/plugins/json-schema.d.cts +12 -4
  25. package/dist/plugins/json-schema.d.ts +12 -4
  26. package/dist/plugins/json-schema.js +40 -2
  27. package/dist/runtime/next/async.d.cts +4 -4
  28. package/dist/runtime/next/async.d.ts +4 -4
  29. package/dist/runtime/next/async.js +3 -3
  30. package/dist/runtime/next/index.d.cts +5 -5
  31. package/dist/runtime/next/index.d.ts +5 -5
  32. package/dist/runtime/vite/browser.d.cts +3 -3
  33. package/dist/runtime/vite/browser.d.ts +3 -3
  34. package/dist/runtime/vite/server.d.cts +3 -3
  35. package/dist/runtime/vite/server.d.ts +3 -3
  36. package/dist/{types-1cCFEzWt.d.ts → types-AGzTfBmf.d.ts} +1 -1
  37. package/dist/{types-D5NhXTJY.d.cts → types-DKGMoay5.d.cts} +1 -1
  38. package/dist/vite/index.cjs +145 -108
  39. package/dist/vite/index.js +53 -45
  40. package/dist/{loader-mdx.cjs → webpack/index.cjs} +151 -58
  41. package/dist/webpack/index.js +44 -0
  42. package/loader-mdx.cjs +1 -1
  43. package/package.json +4 -3
  44. package/dist/chunk-4MAYA5QX.js +0 -44
  45. package/dist/chunk-HI62EXSB.js +0 -127
  46. package/dist/loader-mdx.js +0 -39
  47. package/dist/plugins/index.cjs +0 -78
  48. package/dist/plugins/index.d.cts +0 -7
  49. package/dist/plugins/index.d.ts +0 -7
  50. package/dist/plugins/index.js +0 -6
  51. package/dist/remark-postprocess-K233ZVBK.d.cts +0 -22
  52. package/dist/remark-postprocess-K233ZVBK.d.ts +0 -22
  53. package/dist/watcher-WXJDWRZY.js +0 -22
  54. /package/dist/{loader-mdx.d.cts → webpack/index.d.cts} +0 -0
  55. /package/dist/{loader-mdx.d.ts → webpack/index.d.ts} +0 -0
@@ -195,53 +195,23 @@ async function compileConfig(configPath, outDir) {
195
195
  }
196
196
  async function loadConfig(configPath, outDir, build = false) {
197
197
  if (build) await compileConfig(configPath, outDir);
198
- const url = (0, import_node_url.pathToFileURL)(path3.resolve(outDir, "source.config.mjs"));
198
+ const url = (0, import_node_url.pathToFileURL)(path2.resolve(outDir, "source.config.mjs"));
199
199
  url.searchParams.set("hash", Date.now().toString());
200
200
  const config = import(url.href).then(
201
201
  (loaded) => buildConfig(loaded)
202
202
  );
203
203
  return await config;
204
204
  }
205
- var path3, import_node_url;
205
+ var path2, import_node_url;
206
206
  var init_load = __esm({
207
207
  "src/loaders/config/load.ts"() {
208
208
  "use strict";
209
- path3 = __toESM(require("path"), 1);
209
+ path2 = __toESM(require("path"), 1);
210
210
  import_node_url = require("url");
211
211
  init_build();
212
212
  }
213
213
  });
214
214
 
215
- // src/next/watcher.ts
216
- var watcher_exports = {};
217
- __export(watcher_exports, {
218
- watcher: () => watcher
219
- });
220
- function watcher(configPath, config, ignored) {
221
- const watcher2 = new import_chokidar.FSWatcher({
222
- ignoreInitial: true,
223
- persistent: true,
224
- ignored
225
- });
226
- watcher2.add(configPath);
227
- for (const collection of config.collections.values()) {
228
- if (collection.type === "docs") {
229
- watcher2.add(collection.docs.dir);
230
- watcher2.add(collection.meta.dir);
231
- } else {
232
- watcher2.add(collection.dir);
233
- }
234
- }
235
- return watcher2;
236
- }
237
- var import_chokidar;
238
- var init_watcher = __esm({
239
- "src/next/watcher.ts"() {
240
- "use strict";
241
- import_chokidar = require("chokidar");
242
- }
243
- });
244
-
245
215
  // src/next/index.ts
246
216
  var next_exports = {};
247
217
  __export(next_exports, {
@@ -257,61 +227,20 @@ function findConfigFile() {
257
227
  return import_node_path.default.resolve("source.config.ts");
258
228
  }
259
229
 
260
- // src/plugins/index.ts
261
- var import_node_path2 = __toESM(require("path"), 1);
262
- var import_promises2 = __toESM(require("fs/promises"), 1);
263
- function createPluginHandler(context, defaultPlugins = []) {
264
- const plugins2 = [];
265
- async function write(entry) {
266
- const file = import_node_path2.default.join(context.outDir, entry.path);
267
- await import_promises2.default.mkdir(import_node_path2.default.dirname(file), { recursive: true });
268
- await import_promises2.default.writeFile(file, entry.content);
269
- }
270
- return {
271
- async init(config) {
272
- if (config.global.plugins) {
273
- defaultPlugins.push(...config.global.plugins);
274
- }
275
- for await (const option of defaultPlugins) {
276
- if (!option) continue;
277
- if (Array.isArray(option)) plugins2.push(...option);
278
- else plugins2.push(option);
279
- }
280
- for (const plugin of plugins2) {
281
- const out = await plugin.config?.call(context, config);
282
- if (out) config = out;
283
- }
284
- return config;
285
- },
286
- async emit() {
287
- const out = await Promise.all(
288
- plugins2.map((plugin) => {
289
- return plugin.emit?.call(context) ?? [];
290
- })
291
- );
292
- return out.flat();
293
- },
294
- async emitAndWrite() {
295
- const entries = await this.emit();
296
- await Promise.all(entries.map(write));
297
- }
298
- };
299
- }
300
-
301
230
  // src/next/index.ts
302
- var path8 = __toESM(require("path"), 1);
231
+ var path9 = __toESM(require("path"), 1);
303
232
  init_load();
304
233
 
305
234
  // src/next/file-cache.ts
306
235
  var import_lru_cache = require("lru-cache");
307
- var import_promises3 = __toESM(require("fs/promises"), 1);
308
- var import_node_path3 = __toESM(require("path"), 1);
236
+ var import_promises2 = __toESM(require("fs/promises"), 1);
237
+ var import_node_path2 = __toESM(require("path"), 1);
309
238
  var map = new import_lru_cache.LRUCache({
310
239
  max: 100
311
240
  });
312
241
  function toFullPath(file) {
313
- if (import_node_path3.default.isAbsolute(file)) {
314
- return import_node_path3.default.relative(process.cwd(), file);
242
+ if (import_node_path2.default.isAbsolute(file)) {
243
+ return import_node_path2.default.relative(process.cwd(), file);
315
244
  }
316
245
  return file;
317
246
  }
@@ -319,7 +248,7 @@ async function readFileWithCache(file) {
319
248
  const fullPath = toFullPath(file);
320
249
  const cached = map.get(fullPath);
321
250
  if (cached) return cached;
322
- const read = import_promises3.default.readFile(fullPath).then((s) => s.toString());
251
+ const read = import_promises2.default.readFile(fullPath).then((s) => s.toString());
323
252
  map.set(fullPath, read);
324
253
  return read;
325
254
  }
@@ -368,11 +297,10 @@ async function validate(schema, data, context, errorMessage) {
368
297
  // src/plugins/next.ts
369
298
  var path7 = __toESM(require("path"), 1);
370
299
  var import_node_crypto = require("crypto");
371
- var import_tinyglobby = require("tinyglobby");
372
300
  var import_js_yaml2 = require("js-yaml");
373
301
 
374
302
  // src/utils/git-timestamp.ts
375
- var import_node_path4 = __toESM(require("path"), 1);
303
+ var import_node_path3 = __toESM(require("path"), 1);
376
304
  var import_tinyexec = require("tinyexec");
377
305
  var cache = /* @__PURE__ */ new Map();
378
306
  async function getGitTimestamp(file) {
@@ -381,7 +309,7 @@ async function getGitTimestamp(file) {
381
309
  try {
382
310
  const out = await (0, import_tinyexec.x)(
383
311
  "git",
384
- ["log", "-1", '--pretty="%ai"', import_node_path4.default.relative(process.cwd(), file)],
312
+ ["log", "-1", '--pretty="%ai"', import_node_path3.default.relative(process.cwd(), file)],
385
313
  {
386
314
  throwOnError: true
387
315
  }
@@ -411,7 +339,7 @@ function fumaMatter(input) {
411
339
  }
412
340
 
413
341
  // src/utils/import-formatter.ts
414
- var import_node_path5 = __toESM(require("path"), 1);
342
+ var import_node_path4 = __toESM(require("path"), 1);
415
343
  function getImportCode(info) {
416
344
  const specifier = JSON.stringify(info.specifier);
417
345
  if (info.type === "default") return `import ${info.name} from ${specifier}`;
@@ -426,7 +354,7 @@ function getImportCode(info) {
426
354
  return `import ${specifier}`;
427
355
  }
428
356
  function toImportPath(file, config) {
429
- const ext = import_node_path5.default.extname(file);
357
+ const ext = import_node_path4.default.extname(file);
430
358
  let filename;
431
359
  if (ext === ".ts" && config.jsExtension) {
432
360
  filename = file.substring(0, file.length - ext.length) + ".js";
@@ -437,40 +365,78 @@ function toImportPath(file, config) {
437
365
  }
438
366
  let importPath;
439
367
  if ("relativeTo" in config) {
440
- importPath = import_node_path5.default.relative(config.relativeTo, filename);
441
- if (!import_node_path5.default.isAbsolute(importPath) && !importPath.startsWith(".")) {
368
+ importPath = import_node_path4.default.relative(config.relativeTo, filename);
369
+ if (!import_node_path4.default.isAbsolute(importPath) && !importPath.startsWith(".")) {
442
370
  importPath = `./${importPath}`;
443
371
  }
444
372
  } else {
445
- importPath = import_node_path5.default.resolve(filename);
373
+ importPath = import_node_path4.default.resolve(filename);
446
374
  }
447
- return importPath.replaceAll(import_node_path5.default.sep, "/");
375
+ return importPath.replaceAll(import_node_path4.default.sep, "/");
448
376
  }
449
377
 
450
378
  // src/utils/collections.ts
451
- function getSupportedFormats(collection) {
452
- return {
453
- doc: ["mdx", "md"],
454
- meta: ["json", "yaml"]
455
- }[collection.type];
456
- }
379
+ var import_picomatch = __toESM(require("picomatch"), 1);
380
+ var import_tinyglobby = require("tinyglobby");
381
+ var import_node_path5 = __toESM(require("path"), 1);
382
+ var SupportedFormats = {
383
+ doc: ["mdx", "md"],
384
+ meta: ["json", "yaml"]
385
+ };
457
386
  function getGlobPatterns(collection) {
458
387
  if (collection.files) return collection.files;
459
- return [`**/*.{${getSupportedFormats(collection).join(",")}}`];
388
+ return [`**/*.{${SupportedFormats[collection.type].join(",")}}`];
460
389
  }
461
390
  function isFileSupported(filePath, collection) {
462
- for (const format of getSupportedFormats(collection)) {
463
- if (filePath.endsWith(`.${format}`)) return true;
464
- }
465
- return false;
391
+ return SupportedFormats[collection.type].some(
392
+ (format) => filePath.endsWith(`.${format}`)
393
+ );
394
+ }
395
+ async function getCollectionFiles(collection) {
396
+ const files = /* @__PURE__ */ new Map();
397
+ const dirs = Array.isArray(collection.dir) ? collection.dir : [collection.dir];
398
+ const patterns = getGlobPatterns(collection);
399
+ await Promise.all(
400
+ dirs.map(async (dir) => {
401
+ const result = await (0, import_tinyglobby.glob)(patterns, {
402
+ cwd: import_node_path5.default.resolve(dir)
403
+ });
404
+ for (const item of result) {
405
+ if (!isFileSupported(item, collection)) continue;
406
+ const fullPath = import_node_path5.default.join(dir, item);
407
+ files.set(fullPath, {
408
+ path: item,
409
+ fullPath
410
+ });
411
+ }
412
+ })
413
+ );
414
+ return Array.from(files.values());
466
415
  }
467
416
 
468
417
  // src/plugins/next.ts
469
418
  function next() {
470
419
  let config;
420
+ let shouldEmitOnChange = false;
471
421
  return {
422
+ name: "next",
472
423
  config(v) {
473
424
  config = v;
425
+ shouldEmitOnChange = false;
426
+ for (const collection of config.collections.values()) {
427
+ if (collection.type === "doc" && collection.async || collection.type === "docs" && collection.docs.async) {
428
+ shouldEmitOnChange = true;
429
+ }
430
+ }
431
+ },
432
+ configureServer(server) {
433
+ if (!server.watcher) return;
434
+ server.watcher.on("all", async () => {
435
+ if (!shouldEmitOnChange) return;
436
+ await this.core.emitAndWrite({
437
+ filterPlugin: (plugin) => plugin.name === "next"
438
+ });
439
+ });
474
440
  },
475
441
  async emit() {
476
442
  return [
@@ -613,27 +579,6 @@ async function indexFile(configPath, config, importPath, configHash = false) {
613
579
  ...resolvedDeclares
614
580
  ].join("\n");
615
581
  }
616
- async function getCollectionFiles(collection) {
617
- const files = /* @__PURE__ */ new Map();
618
- const dirs = Array.isArray(collection.dir) ? collection.dir : [collection.dir];
619
- const patterns = getGlobPatterns(collection);
620
- await Promise.all(
621
- dirs.map(async (dir) => {
622
- const result = await (0, import_tinyglobby.glob)(patterns, {
623
- cwd: path7.resolve(dir)
624
- });
625
- for (const item of result) {
626
- if (!isFileSupported(item, collection)) continue;
627
- const fullPath = path7.join(dir, item);
628
- files.set(fullPath, {
629
- path: item,
630
- fullPath
631
- });
632
- }
633
- })
634
- );
635
- return Array.from(files.values());
636
- }
637
582
  function parseMetaEntry(file, content) {
638
583
  const extname2 = path7.extname(file);
639
584
  try {
@@ -647,6 +592,79 @@ function parseMetaEntry(file, content) {
647
592
  throw new Error(`Unknown meta file format: ${extname2}, in ${file}.`);
648
593
  }
649
594
 
595
+ // src/core.ts
596
+ var import_node_path6 = __toESM(require("path"), 1);
597
+ var import_promises3 = __toESM(require("fs/promises"), 1);
598
+ function createCore(options, defaultPlugins = []) {
599
+ let config;
600
+ let plugins2;
601
+ return {
602
+ _options: options,
603
+ getPluginContext() {
604
+ return {
605
+ core: this,
606
+ ...options
607
+ };
608
+ },
609
+ /**
610
+ * Convenient cache store, reset when config changes
611
+ */
612
+ cache: /* @__PURE__ */ new Map(),
613
+ async init({ config: newConfig }) {
614
+ config = await newConfig;
615
+ this.cache.clear();
616
+ plugins2 = [];
617
+ for await (const option of [
618
+ ...defaultPlugins,
619
+ ...config.global.plugins ?? []
620
+ ]) {
621
+ if (!option) continue;
622
+ if (Array.isArray(option)) plugins2.push(...option);
623
+ else plugins2.push(option);
624
+ }
625
+ for (const plugin of plugins2) {
626
+ const out = await plugin.config?.call(this.getPluginContext(), config);
627
+ if (out) config = out;
628
+ }
629
+ return this;
630
+ },
631
+ getConfig() {
632
+ return config;
633
+ },
634
+ creatConfigLoader() {
635
+ return {
636
+ getConfig() {
637
+ return config;
638
+ }
639
+ };
640
+ },
641
+ async initServer(server) {
642
+ for (const plugin of plugins2) {
643
+ await plugin.configureServer?.call(this.getPluginContext(), server);
644
+ }
645
+ },
646
+ async emitAndWrite({
647
+ filterPlugin = () => true
648
+ } = {}) {
649
+ const start = performance.now();
650
+ const out = await Promise.all(
651
+ plugins2.map((plugin) => {
652
+ if (!filterPlugin(plugin) || !plugin.emit) return [];
653
+ return plugin.emit.call(this.getPluginContext());
654
+ })
655
+ );
656
+ await Promise.all(
657
+ out.flat().map(async (entry) => {
658
+ const file = import_node_path6.default.join(options.outDir, entry.path);
659
+ await import_promises3.default.mkdir(import_node_path6.default.dirname(file), { recursive: true });
660
+ await import_promises3.default.writeFile(file, entry.content);
661
+ })
662
+ );
663
+ console.log(`[MDX] generated files in ${performance.now() - start}ms`);
664
+ }
665
+ };
666
+ }
667
+
650
668
  // src/next/index.ts
651
669
  var defaultPageExtensions = ["mdx", "md", "jsx", "js", "tsx", "ts"];
652
670
  function createMDX(createOptions = {}) {
@@ -701,63 +719,63 @@ function createMDX(createOptions = {}) {
701
719
  };
702
720
  }
703
721
  async function init(dev, options) {
704
- const pluginHandler = createNextPluginHandler(options);
705
- let config;
706
- async function updateConfig() {
707
- config = await pluginHandler.init(
708
- await loadConfig(options.configPath, options.outDir, true)
709
- );
722
+ const core = createNextCore(options);
723
+ async function initOrReload() {
724
+ await core.init({
725
+ config: loadConfig(options.configPath, options.outDir, true)
726
+ });
727
+ await core.emitAndWrite();
710
728
  }
711
- async function emitFiles() {
712
- const start = performance.now();
713
- try {
714
- await pluginHandler.emitAndWrite();
715
- } catch (err) {
716
- if (err instanceof ValidationError) {
717
- console.error(err.toStringFormatted());
729
+ async function devServer() {
730
+ const { FSWatcher } = await import("chokidar");
731
+ const watcher = new FSWatcher({
732
+ ignoreInitial: true,
733
+ persistent: true,
734
+ ignored: [options.outDir]
735
+ });
736
+ watcher.add(options.configPath);
737
+ for (const collection of core.getConfig().collections.values()) {
738
+ if (collection.type === "docs") {
739
+ watcher.add(collection.docs.dir);
740
+ watcher.add(collection.meta.dir);
718
741
  } else {
719
- console.error(err);
742
+ watcher.add(collection.dir);
720
743
  }
721
744
  }
722
- console.log(`[MDX] updated map file in ${performance.now() - start}ms`);
723
- }
724
- async function devServer() {
725
- const { watcher: watcher2 } = await Promise.resolve().then(() => (init_watcher(), watcher_exports));
726
- const instance = watcher2(options.configPath, config, [options.outDir]);
727
- async function onUpdate(event, file) {
728
- const absolutePath = path8.resolve(file);
745
+ watcher.on("ready", () => {
746
+ console.log("[MDX] started dev server");
747
+ });
748
+ watcher.on("all", async (event, file) => {
749
+ const absolutePath = path9.resolve(file);
729
750
  if (event === "change") removeFileCache(absolutePath);
730
- if (absolutePath === path8.resolve(options.configPath)) {
731
- await updateConfig();
751
+ if (absolutePath === path9.resolve(options.configPath)) {
752
+ watcher.removeAllListeners();
753
+ await watcher.close();
754
+ await initOrReload();
732
755
  console.log("[MDX] restarting dev server");
733
- await instance.close();
734
- void devServer();
756
+ await devServer();
735
757
  }
736
- await emitFiles();
737
- }
738
- instance.on("ready", () => {
739
- console.log("[MDX] started dev server");
740
- });
741
- instance.on("all", (event, file) => {
742
- void onUpdate(event, file);
743
758
  });
744
759
  process.on("exit", () => {
760
+ if (watcher.closed) return;
745
761
  console.log("[MDX] closing dev server");
746
- void instance.close();
762
+ void watcher.close();
747
763
  });
764
+ await core.initServer({ watcher });
765
+ }
766
+ await initOrReload();
767
+ if (dev) {
768
+ await devServer();
748
769
  }
749
- await updateConfig();
750
- await emitFiles();
751
- if (dev) void devServer();
752
770
  }
753
771
  async function postInstall(configPath = findConfigFile(), outDir = ".source") {
754
- const pluginHandler = createNextPluginHandler({
772
+ const core = await createNextCore({
755
773
  outDir,
756
774
  configPath
775
+ }).init({
776
+ config: loadConfig(configPath, outDir, true)
757
777
  });
758
- await pluginHandler.init(await loadConfig(configPath, outDir, true));
759
- await pluginHandler.emitAndWrite();
760
- console.log("[MDX] types generated");
778
+ await core.emitAndWrite();
761
779
  }
762
780
  function applyDefaults(options) {
763
781
  return {
@@ -765,11 +783,11 @@ function applyDefaults(options) {
765
783
  configPath: options.configPath ?? findConfigFile()
766
784
  };
767
785
  }
768
- function createNextPluginHandler({
786
+ function createNextCore({
769
787
  outDir,
770
788
  configPath
771
789
  }) {
772
- return createPluginHandler(
790
+ const core = createCore(
773
791
  {
774
792
  environment: "next",
775
793
  outDir,
@@ -777,6 +795,20 @@ function createNextPluginHandler({
777
795
  },
778
796
  [next()]
779
797
  );
798
+ return {
799
+ ...core,
800
+ async emitAndWrite(...args) {
801
+ try {
802
+ await core.emitAndWrite(...args);
803
+ } catch (err) {
804
+ if (err instanceof ValidationError) {
805
+ console.error(err.toStringFormatted());
806
+ } else {
807
+ console.error(err);
808
+ }
809
+ }
810
+ }
811
+ };
780
812
  }
781
813
  // Annotate the CommonJS export names for ESM import in node:
782
814
  0 && (module.exports = {