fumadocs-mdx 12.0.0 → 12.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 (56) hide show
  1. package/dist/bin.cjs +103 -85
  2. package/dist/bin.js +1 -1
  3. package/dist/{browser-D5lvL8vv.d.ts → browser-BupUnhpC.d.ts} +1 -1
  4. package/dist/{browser-DjWADqp8.d.cts → browser-R0x9IPaQ.d.cts} +1 -1
  5. package/dist/bun/index.cjs +156 -130
  6. package/dist/bun/index.js +10 -10
  7. package/dist/chunk-ADR6R7HM.js +29 -0
  8. package/dist/{chunk-3M4SHY6K.js → chunk-FSZMKRVH.js} +1 -1
  9. package/dist/{chunk-POXTQZ4D.js → chunk-LGYVNESJ.js} +2 -6
  10. package/dist/chunk-LMG6UWCL.js +167 -0
  11. package/dist/{chunk-SWNOXPYJ.js → chunk-QAUWMR5D.js} +6 -6
  12. package/dist/{chunk-KGUBBRL6.js → chunk-RMDXSZYE.js} +11 -39
  13. package/dist/{chunk-YC25YEBF.js → chunk-U4MQ44TS.js} +1 -1
  14. package/dist/chunk-XMFLD5J6.js +30 -0
  15. package/dist/{chunk-TLD6JMT6.js → chunk-ZX7TM4AR.js} +4 -2
  16. package/dist/config/index.cjs +84 -56
  17. package/dist/config/index.d.cts +2 -6
  18. package/dist/config/index.d.ts +2 -6
  19. package/dist/config/index.js +2 -2
  20. package/dist/{define--6HQ1ehX.d.cts → define-DJbJduHy.d.cts} +15 -15
  21. package/dist/{define--6HQ1ehX.d.ts → define-DJbJduHy.d.ts} +15 -15
  22. package/dist/index.cjs +127 -0
  23. package/dist/index.d.cts +13 -0
  24. package/dist/index.d.ts +13 -0
  25. package/dist/index.js +11 -0
  26. package/dist/load-UUXLUBHL.js +9 -0
  27. package/dist/loader-mdx.cjs +217 -174
  28. package/dist/loader-mdx.js +7 -7
  29. package/dist/next/index.cjs +129 -109
  30. package/dist/next/index.js +40 -54
  31. package/dist/node/loader.cjs +152 -109
  32. package/dist/node/loader.js +6 -7
  33. package/dist/postinstall-SCSXM4IM.js +10 -0
  34. package/dist/{preset-WFEORCAB.js → preset-ZMP6U62C.js} +1 -1
  35. package/dist/runtime/next/async.cjs +117 -65
  36. package/dist/runtime/next/async.d.cts +3 -3
  37. package/dist/runtime/next/async.d.ts +3 -3
  38. package/dist/runtime/next/async.js +15 -8
  39. package/dist/runtime/next/index.d.cts +8 -8
  40. package/dist/runtime/next/index.d.ts +8 -8
  41. package/dist/runtime/vite/browser.d.cts +2 -2
  42. package/dist/runtime/vite/browser.d.ts +2 -2
  43. package/dist/runtime/vite/server.d.cts +5 -5
  44. package/dist/runtime/vite/server.d.ts +5 -5
  45. package/dist/{shared-CqgMnt9h.d.cts → shared-Cnm1afPW.d.cts} +3 -2
  46. package/dist/{shared-0QIuV0XZ.d.ts → shared-DpXxElc1.d.ts} +3 -2
  47. package/dist/{types-DLIAvrgC.d.ts → types-CbLt05Gc.d.cts} +7 -8
  48. package/dist/{types-Dl8HLbm5.d.cts → types-Ci6BA1tA.d.ts} +7 -8
  49. package/dist/vite/index.cjs +177 -145
  50. package/dist/vite/index.d.cts +1 -0
  51. package/dist/vite/index.d.ts +1 -0
  52. package/dist/vite/index.js +15 -20
  53. package/dist/{watcher-4NDMOH4R.js → watcher-HGOH3APP.js} +1 -1
  54. package/package.json +23 -15
  55. package/dist/chunk-KTDVTBMH.js +0 -139
  56. package/dist/postinstall-U7VROOY7.js +0 -9
@@ -30,7 +30,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
30
30
  ));
31
31
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
32
32
 
33
- // src/mdx/preset.ts
33
+ // src/loaders/mdx/preset.ts
34
34
  var preset_exports = {};
35
35
  __export(preset_exports, {
36
36
  getDefaultMDXOptions: () => getDefaultMDXOptions
@@ -111,24 +111,12 @@ function getDefaultMDXOptions({
111
111
  }
112
112
  var plugins;
113
113
  var init_preset = __esm({
114
- "src/mdx/preset.ts"() {
114
+ "src/loaders/mdx/preset.ts"() {
115
115
  "use strict";
116
116
  plugins = __toESM(require("fumadocs-core/mdx-plugins"), 1);
117
117
  }
118
118
  });
119
119
 
120
- // src/node/loader.ts
121
- var loader_exports = {};
122
- __export(loader_exports, {
123
- load: () => load2
124
- });
125
- module.exports = __toCommonJS(loader_exports);
126
-
127
- // src/utils/config.ts
128
- var fs = __toESM(require("fs/promises"), 1);
129
- var path = __toESM(require("path"), 1);
130
- var import_node_url = require("url");
131
-
132
120
  // src/config/build.ts
133
121
  function buildConfig(config) {
134
122
  const collections = /* @__PURE__ */ new Map();
@@ -178,12 +166,18 @@ function buildConfig(config) {
178
166
  }
179
167
  };
180
168
  }
169
+ var init_build = __esm({
170
+ "src/config/build.ts"() {
171
+ "use strict";
172
+ }
173
+ });
181
174
 
182
- // src/utils/config.ts
183
- function findConfigFile() {
184
- return path.resolve("source.config.ts");
185
- }
186
- var cache = null;
175
+ // src/loaders/config/load.ts
176
+ var load_exports = {};
177
+ __export(load_exports, {
178
+ getConfigHash: () => getConfigHash,
179
+ loadConfig: () => loadConfig
180
+ });
187
181
  async function compileConfig(configPath, outDir) {
188
182
  const { build } = await import("esbuild");
189
183
  const transformed = await build({
@@ -226,6 +220,42 @@ async function getConfigHash(configPath) {
226
220
  }
227
221
  throw new Error("Cannot find config file");
228
222
  }
223
+ var fs, path, import_node_url, cache;
224
+ var init_load = __esm({
225
+ "src/loaders/config/load.ts"() {
226
+ "use strict";
227
+ fs = __toESM(require("fs/promises"), 1);
228
+ path = __toESM(require("path"), 1);
229
+ import_node_url = require("url");
230
+ init_build();
231
+ cache = null;
232
+ }
233
+ });
234
+
235
+ // src/node/loader.ts
236
+ var loader_exports = {};
237
+ __export(loader_exports, {
238
+ load: () => load2
239
+ });
240
+ module.exports = __toCommonJS(loader_exports);
241
+
242
+ // src/loaders/config/index.ts
243
+ var import_node_path = __toESM(require("path"), 1);
244
+ function findConfigFile() {
245
+ return import_node_path.default.resolve("source.config.ts");
246
+ }
247
+ function dynamicConfig(configPath, outDir) {
248
+ return {
249
+ async getConfig(hash) {
250
+ const { loadConfig: loadConfig2, getConfigHash: getConfigHash2 } = await Promise.resolve().then(() => (init_load(), load_exports));
251
+ return loadConfig2(
252
+ configPath,
253
+ outDir,
254
+ hash ?? await getConfigHash2(configPath)
255
+ );
256
+ }
257
+ };
258
+ }
229
259
 
230
260
  // src/utils/fuma-matter.ts
231
261
  var import_js_yaml = require("js-yaml");
@@ -282,7 +312,7 @@ async function validate(schema, data, context, errorMessage) {
282
312
  }
283
313
 
284
314
  // src/utils/git-timestamp.ts
285
- var import_node_path = __toESM(require("path"), 1);
315
+ var import_node_path2 = __toESM(require("path"), 1);
286
316
  var import_tinyexec = require("tinyexec");
287
317
  var cache2 = /* @__PURE__ */ new Map();
288
318
  async function getGitTimestamp(file) {
@@ -291,7 +321,7 @@ async function getGitTimestamp(file) {
291
321
  try {
292
322
  const out = await (0, import_tinyexec.x)(
293
323
  "git",
294
- ["log", "-1", '--pretty="%ai"', import_node_path.default.relative(process.cwd(), file)],
324
+ ["log", "-1", '--pretty="%ai"', import_node_path2.default.relative(process.cwd(), file)],
295
325
  {
296
326
  throwOnError: true
297
327
  }
@@ -304,27 +334,39 @@ async function getGitTimestamp(file) {
304
334
  }
305
335
  }
306
336
 
307
- // src/utils/count-lines.ts
308
- function countLines(s) {
309
- let num = 0;
310
- for (const c of s) {
311
- if (c === "\n") num++;
312
- }
313
- return num;
314
- }
315
-
316
- // src/mdx/build-mdx.ts
337
+ // src/loaders/mdx/build-mdx.ts
317
338
  var import_mdx = require("@mdx-js/mdx");
318
339
 
319
- // src/mdx/remark-include.ts
340
+ // src/loaders/mdx/remark-include.ts
320
341
  var import_unified = require("unified");
321
342
  var import_unist_util_visit = require("unist-util-visit");
322
- var path3 = __toESM(require("path"), 1);
343
+ var path4 = __toESM(require("path"), 1);
323
344
  var fs2 = __toESM(require("fs/promises"), 1);
324
345
  var import_remark_parse = __toESM(require("remark-parse"), 1);
325
346
  var import_remark_mdx = __toESM(require("remark-mdx"), 1);
326
347
  var import_mdx_plugins = require("fumadocs-core/mdx-plugins");
327
- var baseProcessor = (0, import_unified.unified)().use(import_mdx_plugins.remarkHeading);
348
+ var ElementLikeTypes = [
349
+ "mdxJsxFlowElement",
350
+ "mdxJsxTextElement",
351
+ "containerDirective",
352
+ "textDirective",
353
+ "leafDirective"
354
+ ];
355
+ function isElementLike(node) {
356
+ return ElementLikeTypes.includes(node.type);
357
+ }
358
+ function parseElementAttributes(element) {
359
+ if (Array.isArray(element.attributes)) {
360
+ const attributes = {};
361
+ for (const attr of element.attributes) {
362
+ if (attr.type === "mdxJsxAttribute" && (typeof attr.value === "string" || attr.value === null)) {
363
+ attributes[attr.name] = attr.value;
364
+ }
365
+ }
366
+ return attributes;
367
+ }
368
+ return element.attributes ?? {};
369
+ }
328
370
  function flattenNode(node) {
329
371
  if ("children" in node)
330
372
  return node.children.map((child) => flattenNode(child)).join("");
@@ -341,21 +383,31 @@ function parseSpecifier(specifier) {
341
383
  }
342
384
  function extractSection(root, section) {
343
385
  let nodes;
344
- for (const node of root.children) {
345
- if (node.type === "mdxJsxFlowElement" && node.name === "section" && node.attributes.some(
346
- (attr) => attr.type === "mdxJsxAttribute" && attr.name === "id" && attr.value === section
347
- )) {
348
- nodes = node.children;
349
- break;
386
+ let capturingHeadingContent = false;
387
+ (0, import_unist_util_visit.visit)(root, (node) => {
388
+ if (node.type === "heading") {
389
+ if (capturingHeadingContent) {
390
+ return false;
391
+ }
392
+ if (node.data?.hProperties?.id === section) {
393
+ capturingHeadingContent = true;
394
+ nodes = [node];
395
+ return "skip";
396
+ }
397
+ return;
350
398
  }
351
- if (node.type === "heading" && node.data?.hProperties?.id === section) {
352
- nodes = [node];
353
- continue;
399
+ if (capturingHeadingContent) {
400
+ nodes?.push(node);
401
+ return "skip";
354
402
  }
355
- if (!nodes) continue;
356
- if (node.type === "heading") break;
357
- nodes.push(node);
358
- }
403
+ if (isElementLike(node) && node.name === "section") {
404
+ const attributes = parseElementAttributes(node);
405
+ if (attributes.id === section) {
406
+ nodes = node.children;
407
+ return false;
408
+ }
409
+ }
410
+ });
359
411
  if (nodes)
360
412
  return {
361
413
  type: "root",
@@ -375,7 +427,7 @@ ${e instanceof Error ? e.message : String(e)}`,
375
427
  { cause: e }
376
428
  );
377
429
  }
378
- const ext = path3.extname(file);
430
+ const ext = path4.extname(file);
379
431
  data._compiler?.addDependency(file);
380
432
  if (params.lang || ext !== ".md" && ext !== ".mdx") {
381
433
  const lang = params.lang ?? ext.slice(1);
@@ -387,59 +439,56 @@ ${e instanceof Error ? e.message : String(e)}`,
387
439
  data: {}
388
440
  };
389
441
  }
390
- const processor = (data._getProcessor ?? getDefaultProcessor)(
442
+ const parser = (data._getProcessor ?? getDefaultProcessor)(
391
443
  ext === ".mdx" ? "mdx" : "md"
392
444
  );
393
- let parsed = await baseProcessor.run(
394
- processor.parse(fumaMatter(content).content)
395
- );
445
+ const parsed = fumaMatter(content);
446
+ let mdast = parser.parse({
447
+ path: file,
448
+ value: parsed.content,
449
+ data: { frontmatter: parsed.data }
450
+ });
396
451
  if (heading) {
397
- const extracted = extractSection(parsed, heading);
452
+ const extracted = extractSection(
453
+ await (0, import_unified.unified)().use(import_mdx_plugins.remarkHeading).run(mdast),
454
+ heading
455
+ );
398
456
  if (!extracted)
399
457
  throw new Error(
400
- `Cannot find section ${heading} in ${file}, make sure you have encapsulated the section in a <section id="${heading}"> tag.`
458
+ `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.`
401
459
  );
402
- parsed = extracted;
460
+ mdast = extracted;
403
461
  }
404
- await update(parsed, path3.dirname(file), data);
405
- return parsed;
462
+ await update(mdast, path4.dirname(file), data);
463
+ return mdast;
406
464
  }
407
465
  async function update(tree, directory, data) {
408
466
  const queue = [];
409
- (0, import_unist_util_visit.visit)(
410
- tree,
411
- ["mdxJsxFlowElement", "mdxJsxTextElement"],
412
- (_node, _, parent) => {
413
- const node = _node;
414
- if (node.name !== TagName) return;
415
- const params = {};
416
- const specifier = flattenNode(node);
417
- if (specifier.length === 0) return "skip";
418
- for (const attr of node.attributes) {
419
- if (attr.type === "mdxJsxAttribute" && (typeof attr.value === "string" || attr.value === null)) {
420
- params[attr.name] = attr.value;
421
- }
422
- }
423
- const { file: relativePath, section } = parseSpecifier(specifier);
424
- const file = path3.resolve(
425
- "cwd" in params ? process.cwd() : directory,
426
- relativePath
427
- );
428
- queue.push(
429
- embedContent(file, section, params, data).then((replace) => {
430
- Object.assign(
431
- parent && parent.type === "paragraph" ? parent : node,
432
- replace
433
- );
434
- })
435
- );
436
- return "skip";
437
- }
438
- );
467
+ (0, import_unist_util_visit.visit)(tree, ElementLikeTypes, (_node, _, parent) => {
468
+ const node = _node;
469
+ if (node.name !== TagName) return;
470
+ const specifier = flattenNode(node);
471
+ if (specifier.length === 0) return "skip";
472
+ const attributes = parseElementAttributes(node);
473
+ const { file: relativePath, section } = parseSpecifier(specifier);
474
+ const file = path4.resolve(
475
+ "cwd" in attributes ? process.cwd() : directory,
476
+ relativePath
477
+ );
478
+ queue.push(
479
+ embedContent(file, section, attributes, data).then((replace) => {
480
+ Object.assign(
481
+ parent && parent.type === "paragraph" ? parent : node,
482
+ replace
483
+ );
484
+ })
485
+ );
486
+ return "skip";
487
+ });
439
488
  await Promise.all(queue);
440
489
  }
441
490
  return async (tree, file) => {
442
- await update(tree, path3.dirname(file.path), file.data);
491
+ await update(tree, path4.dirname(file.path), file.data);
443
492
  };
444
493
  }
445
494
  function getDefaultProcessor(format) {
@@ -448,7 +497,7 @@ function getDefaultProcessor(format) {
448
497
  return mdProcessor.use(import_remark_mdx.default);
449
498
  }
450
499
 
451
- // src/mdx/remark-postprocess.ts
500
+ // src/loaders/mdx/remark-postprocess.ts
452
501
  var import_unist_util_visit2 = require("unist-util-visit");
453
502
  var import_mdast_util_to_markdown = require("mdast-util-to-markdown");
454
503
  var import_estree_util_value_to_estree = require("estree-util-value-to-estree");
@@ -477,7 +526,7 @@ function remarkPostprocess({
477
526
  if (includeProcessedMarkdown) {
478
527
  file.data._markdown = (0, import_mdast_util_to_markdown.toMarkdown)(tree, {
479
528
  ...this.data("settings"),
480
- // @ts-expect-error - from https://github.com/remarkjs/remark/blob/main/packages/remark-stringify/lib/index.js
529
+ // from https://github.com/remarkjs/remark/blob/main/packages/remark-stringify/lib/index.js
481
530
  extensions: this.data("toMarkdownExtensions") || []
482
531
  });
483
532
  }
@@ -531,7 +580,7 @@ function flattenNode2(node) {
531
580
  return "";
532
581
  }
533
582
 
534
- // src/mdx/build-mdx.ts
583
+ // src/loaders/mdx/build-mdx.ts
535
584
  var cache3 = /* @__PURE__ */ new Map();
536
585
  async function buildMDX(cacheKey, source, options) {
537
586
  const { filePath, frontmatter, data, _compiler, ...rest } = options;
@@ -567,7 +616,7 @@ async function buildMDX(cacheKey, source, options) {
567
616
  return processor;
568
617
  }
569
618
  return getProcessor(
570
- options.format ?? filePath.endsWith(".mdx") ? "mdx" : "md"
619
+ options.format ?? (filePath.endsWith(".mdx") ? "mdx" : "md")
571
620
  ).process({
572
621
  value: source,
573
622
  path: filePath,
@@ -580,10 +629,10 @@ async function buildMDX(cacheKey, source, options) {
580
629
  });
581
630
  }
582
631
 
583
- // src/loaders/mdx.ts
632
+ // src/loaders/mdx/index.ts
584
633
  var import_zod = require("zod");
585
634
  var import_promises = __toESM(require("fs/promises"), 1);
586
- var import_node_path2 = __toESM(require("path"), 1);
635
+ var import_node_path3 = __toESM(require("path"), 1);
587
636
  var import_node_crypto = require("crypto");
588
637
  var querySchema = import_zod.z.object({
589
638
  only: import_zod.z.literal(["frontmatter", "all"]).default("all"),
@@ -611,7 +660,7 @@ function createMdxLoader(configLoader) {
611
660
  const cacheDir = isDevelopment ? void 0 : loaded.global.experimentalBuildCache;
612
661
  const cacheKey = `${parsed.hash}_${parsed.collection ?? "global"}_${generateCacheHash(filePath)}`;
613
662
  if (cacheDir) {
614
- const cached = await import_promises.default.readFile(import_node_path2.default.join(cacheDir, cacheKey)).then((content) => cacheEntry.parse(JSON.parse(content.toString()))).catch(() => null);
663
+ const cached = await import_promises.default.readFile(import_node_path3.default.join(cacheDir, cacheKey)).then((content) => cacheEntry.parse(JSON.parse(content.toString()))).catch(() => null);
615
664
  if (cached && cached.hash === generateCacheHash(value)) return cached;
616
665
  }
617
666
  const collection = parsed.collection ? loaded.collections.get(parsed.collection) : void 0;
@@ -666,7 +715,7 @@ function createMdxLoader(configLoader) {
666
715
  if (cacheDir) {
667
716
  await import_promises.default.mkdir(cacheDir, { recursive: true });
668
717
  await import_promises.default.writeFile(
669
- import_node_path2.default.join(cacheDir, cacheKey),
718
+ import_node_path3.default.join(cacheDir, cacheKey),
670
719
  JSON.stringify({
671
720
  ...out,
672
721
  hash: generateCacheHash(value)
@@ -679,25 +728,19 @@ function createMdxLoader(configLoader) {
679
728
  function generateCacheHash(input) {
680
729
  return (0, import_node_crypto.createHash)("md5").update(input).digest("hex");
681
730
  }
682
-
683
- // src/loaders/config-loader.ts
684
- function dynamicConfig(configPath, outDir) {
685
- return {
686
- async getConfig(hash) {
687
- return loadConfig(
688
- configPath,
689
- outDir,
690
- hash ?? await getConfigHash(configPath)
691
- );
692
- }
693
- };
731
+ function countLines(s) {
732
+ let num = 0;
733
+ for (const c of s) {
734
+ if (c === "\n") num++;
735
+ }
736
+ return num;
694
737
  }
695
738
 
696
739
  // src/loaders/adapter.ts
697
740
  var import_node_url2 = require("url");
698
741
  var import_promises2 = __toESM(require("fs/promises"), 1);
699
742
  var import_node_querystring = require("querystring");
700
- var import_node_path3 = __toESM(require("path"), 1);
743
+ var import_node_path4 = __toESM(require("path"), 1);
701
744
  function toNode(loader, filterByPath) {
702
745
  return async (url, _context, nextLoad) => {
703
746
  if (!url.startsWith("file:///")) return nextLoad(url);
@@ -2,16 +2,15 @@ import {
2
2
  toNode
3
3
  } from "../chunk-VXEBLM4X.js";
4
4
  import {
5
- createMdxLoader,
6
- dynamicConfig
7
- } from "../chunk-KGUBBRL6.js";
5
+ createMdxLoader
6
+ } from "../chunk-RMDXSZYE.js";
7
+ import "../chunk-QAUWMR5D.js";
8
+ import "../chunk-LMG6UWCL.js";
8
9
  import "../chunk-IQAEAI4P.js";
9
10
  import {
11
+ dynamicConfig,
10
12
  findConfigFile
11
- } from "../chunk-POXTQZ4D.js";
12
- import "../chunk-SWNOXPYJ.js";
13
- import "../chunk-KTDVTBMH.js";
14
- import "../chunk-YC25YEBF.js";
13
+ } from "../chunk-XMFLD5J6.js";
15
14
  import "../chunk-VWJKRQZR.js";
16
15
 
17
16
  // src/node/loader.ts
@@ -0,0 +1,10 @@
1
+ import {
2
+ postInstall
3
+ } from "./chunk-ZX7TM4AR.js";
4
+ import "./chunk-6Y5JDZHD.js";
5
+ import "./chunk-LGYVNESJ.js";
6
+ import "./chunk-U4MQ44TS.js";
7
+ import "./chunk-XMFLD5J6.js";
8
+ export {
9
+ postInstall
10
+ };
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  getDefaultMDXOptions
3
- } from "./chunk-3M4SHY6K.js";
3
+ } from "./chunk-FSZMKRVH.js";
4
4
  export {
5
5
  getDefaultMDXOptions
6
6
  };