fumadocs-mdx 13.0.8 → 14.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/dist/bin.cjs +924 -901
  2. package/dist/bin.js +3 -3
  3. package/dist/build-mdx-6UAK5FF5.js +8 -0
  4. package/dist/bun/index.cjs +578 -471
  5. package/dist/bun/index.d.cts +3 -2
  6. package/dist/bun/index.d.ts +3 -2
  7. package/dist/bun/index.js +12 -12
  8. package/dist/chunk-4JSFLXXT.js +8 -0
  9. package/dist/chunk-5UMZCWKV.js +17 -0
  10. package/dist/chunk-5YXP7JLN.js +138 -0
  11. package/dist/{chunk-2E2JCOSO.js → chunk-6NISOLQ6.js} +16 -44
  12. package/dist/chunk-7L2KNF6B.js +180 -0
  13. package/dist/chunk-E5DJTSIM.js +86 -0
  14. package/dist/{chunk-K5ZLPEIQ.js → chunk-FBLMK4RS.js} +9 -6
  15. package/dist/{chunk-QXHN25N3.js → chunk-OXSRIWQW.js} +7 -8
  16. package/dist/chunk-PKI7ZDA5.js +29 -0
  17. package/dist/{chunk-3J3WL7WN.js → chunk-SLY7WXTX.js} +71 -58
  18. package/dist/{chunk-5FTSWCB4.js → chunk-SRSRFOVI.js} +8 -10
  19. package/dist/chunk-TYJDYTKH.js +85 -0
  20. package/dist/chunk-XHJCLBZ4.js +406 -0
  21. package/dist/{chunk-2HXTGJBI.js → chunk-ZY6UZ7NH.js} +22 -19
  22. package/dist/config/index.cjs +79 -71
  23. package/dist/config/index.d.cts +2 -1
  24. package/dist/config/index.d.ts +2 -1
  25. package/dist/config/index.js +5 -5
  26. package/dist/index-BlVBvy-z.d.ts +8 -0
  27. package/dist/{core-DB7TdlyC.d.cts → index-D7JdSMpp.d.cts} +99 -61
  28. package/dist/{core-DB7TdlyC.d.ts → index-D7JdSMpp.d.ts} +99 -61
  29. package/dist/index-P2NNUkHn.d.cts +8 -0
  30. package/dist/index.d.cts +3 -74
  31. package/dist/index.d.ts +3 -74
  32. package/dist/load-from-file-I3ALLIVB.js +8 -0
  33. package/dist/next/index.cjs +698 -476
  34. package/dist/next/index.d.cts +11 -1
  35. package/dist/next/index.d.ts +11 -1
  36. package/dist/next/index.js +78 -281
  37. package/dist/node/loader.cjs +704 -602
  38. package/dist/node/loader.js +10 -11
  39. package/dist/plugins/index-file.cjs +471 -0
  40. package/dist/plugins/index-file.d.cts +29 -0
  41. package/dist/plugins/index-file.d.ts +29 -0
  42. package/dist/plugins/index-file.js +8 -0
  43. package/dist/plugins/json-schema.d.cts +3 -2
  44. package/dist/plugins/json-schema.d.ts +3 -2
  45. package/dist/plugins/last-modified.cjs +75 -0
  46. package/dist/plugins/last-modified.d.cts +27 -0
  47. package/dist/plugins/last-modified.d.ts +27 -0
  48. package/dist/plugins/last-modified.js +44 -0
  49. package/dist/runtime/{vite/browser.cjs → browser.cjs} +40 -53
  50. package/dist/runtime/browser.d.cts +50 -0
  51. package/dist/runtime/browser.d.ts +50 -0
  52. package/dist/runtime/browser.js +68 -0
  53. package/dist/runtime/dynamic.cjs +985 -0
  54. package/dist/runtime/dynamic.d.cts +27 -0
  55. package/dist/runtime/dynamic.d.ts +27 -0
  56. package/dist/runtime/dynamic.js +78 -0
  57. package/dist/runtime/server.cjs +173 -0
  58. package/dist/runtime/server.d.cts +161 -0
  59. package/dist/runtime/server.d.ts +161 -0
  60. package/dist/runtime/server.js +8 -0
  61. package/dist/vite/index.cjs +934 -638
  62. package/dist/vite/index.d.cts +12 -22
  63. package/dist/vite/index.d.ts +12 -22
  64. package/dist/vite/index.js +30 -221
  65. package/dist/webpack/mdx.cjs +613 -515
  66. package/dist/webpack/mdx.d.cts +9 -1
  67. package/dist/webpack/mdx.d.ts +9 -1
  68. package/dist/webpack/mdx.js +12 -17
  69. package/dist/webpack/meta.cjs +327 -233
  70. package/dist/webpack/meta.d.cts +9 -1
  71. package/dist/webpack/meta.d.ts +9 -1
  72. package/dist/webpack/meta.js +13 -15
  73. package/package.json +15 -32
  74. package/dist/build-mdx-BjXOmv0b.d.cts +0 -53
  75. package/dist/build-mdx-CY5UldCO.d.ts +0 -53
  76. package/dist/chunk-2AQRQXSO.js +0 -119
  77. package/dist/chunk-CXA4JO4Z.js +0 -45
  78. package/dist/chunk-DMJ6I4C3.js +0 -76
  79. package/dist/chunk-FSZMKRVH.js +0 -80
  80. package/dist/chunk-II3H5ZVZ.js +0 -77
  81. package/dist/chunk-KILFIBVW.js +0 -75
  82. package/dist/chunk-NVRDCY6Z.js +0 -30
  83. package/dist/chunk-VUEZTR2H.js +0 -26
  84. package/dist/index-D7s7kCc2.d.cts +0 -7
  85. package/dist/index-D7s7kCc2.d.ts +0 -7
  86. package/dist/load-from-file-AVYOFOI7.js +0 -7
  87. package/dist/preset-ZMP6U62C.js +0 -6
  88. package/dist/runtime/next/async.cjs +0 -760
  89. package/dist/runtime/next/async.d.cts +0 -19
  90. package/dist/runtime/next/async.d.ts +0 -19
  91. package/dist/runtime/next/async.js +0 -86
  92. package/dist/runtime/next/index.cjs +0 -136
  93. package/dist/runtime/next/index.d.cts +0 -33
  94. package/dist/runtime/next/index.d.ts +0 -33
  95. package/dist/runtime/next/index.js +0 -11
  96. package/dist/runtime/vite/browser.d.cts +0 -59
  97. package/dist/runtime/vite/browser.d.ts +0 -59
  98. package/dist/runtime/vite/browser.js +0 -11
  99. package/dist/runtime/vite/server.cjs +0 -243
  100. package/dist/runtime/vite/server.d.cts +0 -30
  101. package/dist/runtime/vite/server.d.ts +0 -30
  102. package/dist/runtime/vite/server.js +0 -111
  103. package/dist/types-Bnh9n7mj.d.cts +0 -45
  104. package/dist/types-ey1AZqrg.d.ts +0 -45
@@ -0,0 +1,29 @@
1
+ // src/utils/codegen/cache.ts
2
+ import { LRUCache } from "lru-cache";
3
+ import fs from "fs/promises";
4
+ import path from "path";
5
+ var map = new LRUCache({
6
+ max: 100
7
+ });
8
+ function toFullPath(file) {
9
+ if (path.isAbsolute(file)) {
10
+ return path.relative(process.cwd(), file);
11
+ }
12
+ return file;
13
+ }
14
+ async function readFileWithCache(file) {
15
+ const fullPath = toFullPath(file);
16
+ const cached = map.get(fullPath);
17
+ if (cached) return cached;
18
+ const read = fs.readFile(fullPath).then((s) => s.toString());
19
+ map.set(fullPath, read);
20
+ return read;
21
+ }
22
+ function removeFileCache(file) {
23
+ map.delete(toFullPath(file));
24
+ }
25
+
26
+ export {
27
+ readFileWithCache,
28
+ removeFileCache
29
+ };
@@ -1,9 +1,11 @@
1
1
  import {
2
+ flattenNode,
2
3
  remarkInclude
3
- } from "./chunk-K5ZLPEIQ.js";
4
+ } from "./chunk-FBLMK4RS.js";
4
5
 
5
6
  // src/loaders/mdx/build-mdx.ts
6
7
  import { createProcessor } from "@mdx-js/mdx";
8
+ import { VFile } from "vfile";
7
9
 
8
10
  // src/loaders/mdx/remark-postprocess.ts
9
11
  import { visit } from "unist-util-visit";
@@ -15,48 +17,66 @@ function remarkPostprocess({
15
17
  _format,
16
18
  includeProcessedMarkdown = false,
17
19
  includeMDAST = false,
20
+ extractLinkReferences = false,
18
21
  valueToExport = []
19
22
  }) {
20
23
  let _stringifyProcessor;
21
24
  const getStringifyProcessor = () => {
22
- if (_format === "mdx") return this;
23
- return _stringifyProcessor ??= this().use(remarkMdx).freeze();
25
+ return _stringifyProcessor ??= _format === "mdx" ? this : (
26
+ // force Markdown processor to stringify MDX nodes
27
+ this().use(remarkMdx).freeze()
28
+ );
24
29
  };
25
30
  return (tree, file) => {
26
- let title;
27
- const urls = [];
28
- visit(tree, ["heading", "link"], (node) => {
29
- if (node.type === "heading" && node.depth === 1) {
30
- title = flattenNode(node);
31
- }
32
- if (node.type !== "link") return;
33
- urls.push({
34
- href: node.url
31
+ const frontmatter = file.data.frontmatter ??= {};
32
+ if (!frontmatter.title) {
33
+ visit(tree, "heading", (node) => {
34
+ if (node.depth === 1) {
35
+ frontmatter.title = flattenNode(node);
36
+ return false;
37
+ }
38
+ });
39
+ }
40
+ file.data["mdx-export"] ??= [];
41
+ if (extractLinkReferences) {
42
+ const urls = [];
43
+ visit(tree, "link", (node) => {
44
+ urls.push({
45
+ href: node.url
46
+ });
47
+ return "skip";
48
+ });
49
+ file.data["mdx-export"].push({
50
+ name: "extractedReferences",
51
+ value: urls
35
52
  });
36
- return "skip";
37
- });
38
- if (title) {
39
- file.data.frontmatter ??= {};
40
- if (!file.data.frontmatter.title) file.data.frontmatter.title = title;
41
53
  }
42
- file.data.extractedReferences = urls;
43
54
  if (includeProcessedMarkdown) {
44
55
  const processor = getStringifyProcessor();
45
- file.data._markdown = toMarkdown(tree, {
56
+ const markdown = toMarkdown(tree, {
46
57
  ...processor.data("settings"),
47
58
  // from https://github.com/remarkjs/remark/blob/main/packages/remark-stringify/lib/index.js
48
59
  extensions: processor.data("toMarkdownExtensions") || []
49
60
  });
61
+ file.data["mdx-export"].push({
62
+ name: "_markdown",
63
+ value: markdown
64
+ });
50
65
  }
51
66
  if (includeMDAST) {
52
67
  const options = includeMDAST === true ? {} : includeMDAST;
53
- file.data._mdast = JSON.stringify(
68
+ const mdast = JSON.stringify(
54
69
  options.removePosition ? removePosition(structuredClone(tree)) : tree
55
70
  );
71
+ file.data["mdx-export"].push({
72
+ name: "_mdast",
73
+ value: mdast
74
+ });
56
75
  }
57
- for (const { name, value } of file.data["mdx-export"] ?? []) {
76
+ for (const { name, value } of file.data["mdx-export"]) {
58
77
  tree.children.unshift(getMdastExport(name, value));
59
78
  }
79
+ file.data["mdx-export"] = [];
60
80
  for (const name of valueToExport) {
61
81
  if (!(name in file.data)) continue;
62
82
  tree.children.unshift(getMdastExport(name, file.data[name]));
@@ -97,43 +117,39 @@ function getMdastExport(name, value) {
97
117
  }
98
118
  };
99
119
  }
100
- function flattenNode(node) {
101
- if ("children" in node)
102
- return node.children.map((child) => flattenNode(child)).join("");
103
- if ("value" in node) return node.value;
104
- return "";
105
- }
106
120
 
107
121
  // src/loaders/mdx/build-mdx.ts
108
- var cache = /* @__PURE__ */ new Map();
109
- async function buildMDX(cacheKey, source, options) {
110
- const { filePath, frontmatter, data, _compiler, ...rest } = options;
122
+ async function buildMDX(core, collection, {
123
+ filePath,
124
+ frontmatter,
125
+ source,
126
+ _compiler,
127
+ environment,
128
+ isDevelopment
129
+ }) {
130
+ const mdxOptions = await core.getConfig().getMDXOptions(collection, environment);
111
131
  function getProcessor(format) {
112
- const key = `${cacheKey}:${format}`;
132
+ const cache = core.cache;
133
+ const key = `build-mdx:${collection?.name ?? "global"}:${format}`;
113
134
  let processor = cache.get(key);
114
135
  if (!processor) {
136
+ const postprocessOptions = {
137
+ _format: format,
138
+ ...collection?.postprocess,
139
+ valueToExport: [
140
+ ...collection?.postprocess?.valueToExport ?? [],
141
+ "structuredData",
142
+ "frontmatter"
143
+ ]
144
+ };
115
145
  processor = createProcessor({
116
146
  outputFormat: "program",
117
- ...rest,
147
+ development: isDevelopment,
148
+ ...mdxOptions,
118
149
  remarkPlugins: [
119
150
  remarkInclude,
120
- ...rest.remarkPlugins ?? [],
121
- [
122
- remarkPostprocess,
123
- {
124
- _format: format,
125
- ...options.postprocess,
126
- valueToExport: [
127
- ...options.postprocess?.valueToExport ?? [],
128
- "structuredData",
129
- "extractedReferences",
130
- "frontmatter",
131
- "lastModified",
132
- "_markdown",
133
- "_mdast"
134
- ]
135
- }
136
- ]
151
+ ...mdxOptions.remarkPlugins ?? [],
152
+ [remarkPostprocess, postprocessOptions]
137
153
  ],
138
154
  format
139
155
  });
@@ -141,18 +157,15 @@ async function buildMDX(cacheKey, source, options) {
141
157
  }
142
158
  return processor;
143
159
  }
144
- return getProcessor(
145
- options.format ?? (filePath.endsWith(".mdx") ? "mdx" : "md")
146
- ).process({
160
+ let vfile = new VFile({
147
161
  value: source,
148
162
  path: filePath,
149
- data: {
150
- ...data,
151
- frontmatter,
152
- _compiler,
153
- _getProcessor: getProcessor
154
- }
163
+ data: { frontmatter, _compiler, _getProcessor: getProcessor }
155
164
  });
165
+ if (collection) {
166
+ vfile = await core.transformVFile({ collection, filePath, source }, vfile);
167
+ }
168
+ return getProcessor(filePath.endsWith(".mdx") ? "mdx" : "md").process(vfile);
156
169
  }
157
170
 
158
171
  export {
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  ValidationError
3
- } from "./chunk-2AQRQXSO.js";
3
+ } from "./chunk-7L2KNF6B.js";
4
4
 
5
5
  // src/loaders/adapter.ts
6
6
  import { fileURLToPath } from "url";
@@ -83,7 +83,7 @@ function toWebpack(loader) {
83
83
  }
84
84
  } catch (error) {
85
85
  if (error instanceof ValidationError) {
86
- return callback(new Error(error.toStringFormatted()));
86
+ return callback(new Error(await error.toStringFormatted()));
87
87
  }
88
88
  if (!(error instanceof Error)) throw error;
89
89
  const fpath = path.relative(this.context, this.resourcePath);
@@ -115,8 +115,8 @@ function toBun(loader) {
115
115
  }
116
116
  }
117
117
  };
118
- if (loader.bun?.loadSync) {
119
- return loader.bun.loadSync(readFileSync(filePath).toString(), input);
118
+ if (loader.bun?.load) {
119
+ return loader.bun.load(readFileSync(filePath).toString(), input);
120
120
  }
121
121
  const result = loader.load(input);
122
122
  if (result instanceof Promise) {
@@ -143,17 +143,14 @@ function createStandaloneConfigLoader({
143
143
  return stats.mtime.getTime().toString();
144
144
  }
145
145
  async function newConfig() {
146
- const { loadConfig } = await import("./load-from-file-AVYOFOI7.js");
146
+ const { loadConfig } = await import("./load-from-file-I3ALLIVB.js");
147
147
  await core.init({
148
- config: loadConfig(
149
- core._options.configPath,
150
- core._options.outDir,
151
- buildConfig
152
- )
148
+ config: loadConfig(core, buildConfig)
153
149
  });
154
150
  return core.getConfig();
155
151
  }
156
152
  return {
153
+ core,
157
154
  async getConfig() {
158
155
  const hash = await getConfigHash();
159
156
  if (loaded && loaded.hash === hash) return loaded.config;
@@ -167,6 +164,7 @@ function createStandaloneConfigLoader({
167
164
  }
168
165
  function createIntegratedConfigLoader(core) {
169
166
  return {
167
+ core,
170
168
  getConfig() {
171
169
  return core.getConfig();
172
170
  }
@@ -0,0 +1,85 @@
1
+ import {
2
+ metaLoaderGlob
3
+ } from "./chunk-4JSFLXXT.js";
4
+
5
+ // src/loaders/meta.ts
6
+ import { dump, load } from "js-yaml";
7
+ import { z } from "zod";
8
+ var querySchema = z.object({
9
+ collection: z.string().optional()
10
+ }).loose();
11
+ function createMetaLoader(configLoader, resolve = {}) {
12
+ const { json: resolveJson = "js", yaml: resolveYaml = "js" } = resolve;
13
+ function parse(filePath, source) {
14
+ try {
15
+ if (filePath.endsWith(".json")) return JSON.parse(source);
16
+ if (filePath.endsWith(".yaml")) return load(source);
17
+ } catch (e) {
18
+ throw new Error(`invalid data in ${filePath}`, { cause: e });
19
+ }
20
+ throw new Error("Unknown file type " + filePath);
21
+ }
22
+ function onMeta(source, { filePath, query }) {
23
+ const parsed = querySchema.safeParse(query);
24
+ if (!parsed.success || !parsed.data.collection) return null;
25
+ const collectionName = parsed.data.collection;
26
+ return async () => {
27
+ const config = await configLoader.getConfig();
28
+ const collection = config.getCollection(collectionName);
29
+ let metaCollection;
30
+ switch (collection?.type) {
31
+ case "meta":
32
+ metaCollection = collection;
33
+ break;
34
+ case "docs":
35
+ metaCollection = collection.meta;
36
+ break;
37
+ }
38
+ const data = parse(filePath, source);
39
+ if (!metaCollection) return data;
40
+ return configLoader.core.transformMeta(
41
+ {
42
+ collection: metaCollection,
43
+ filePath,
44
+ source
45
+ },
46
+ data
47
+ );
48
+ };
49
+ }
50
+ return {
51
+ test: metaLoaderGlob,
52
+ async load(input) {
53
+ const result = onMeta(await input.getSource(), input);
54
+ if (result === null) return null;
55
+ const data = await result();
56
+ if (input.filePath.endsWith(".json")) {
57
+ return {
58
+ code: resolveJson === "json" ? JSON.stringify(data) : `export default ${JSON.stringify(data)}`
59
+ };
60
+ } else {
61
+ return {
62
+ code: resolveYaml === "yaml" ? dump(data) : `export default ${JSON.stringify(data)}`
63
+ };
64
+ }
65
+ },
66
+ bun: {
67
+ load(source, input) {
68
+ const result = onMeta(source, input);
69
+ if (result === null)
70
+ return {
71
+ loader: "object",
72
+ exports: parse(input.filePath, source)
73
+ };
74
+ return result().then((data) => ({
75
+ loader: "object",
76
+ exports: { default: data }
77
+ }));
78
+ }
79
+ }
80
+ };
81
+ }
82
+
83
+ export {
84
+ createMetaLoader
85
+ };