fumadocs-mdx 13.0.2 → 13.0.4

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 (67) hide show
  1. package/dist/bin.cjs +381 -279
  2. package/dist/{build-mdx-D-r3_eQL.d.cts → build-mdx-BjXOmv0b.d.cts} +1 -1
  3. package/dist/{build-mdx-CCNr86q6.d.ts → build-mdx-CY5UldCO.d.ts} +1 -1
  4. package/dist/bun/index.cjs +177 -57
  5. package/dist/bun/index.d.cts +2 -2
  6. package/dist/bun/index.d.ts +2 -2
  7. package/dist/bun/index.js +26 -34
  8. package/dist/chunk-2HXTGJBI.js +101 -0
  9. package/dist/chunk-4JSFLXXT.js +8 -0
  10. package/dist/{chunk-JVZFH6ND.js → chunk-QXHN25N3.js} +2 -2
  11. package/dist/{chunk-EELYB2XC.js → chunk-TZ5EQBFW.js} +9 -103
  12. package/dist/chunk-VUEZTR2H.js +26 -0
  13. package/dist/{chunk-XQ5O7IPO.js → chunk-XYGORKQA.js} +5 -3
  14. package/dist/chunk-YAIPHUCZ.js +56 -0
  15. package/dist/chunk-ZNVPB2IR.js +170 -0
  16. package/dist/config/index.d.cts +2 -2
  17. package/dist/config/index.d.ts +2 -2
  18. package/dist/{core-B6j6Fxse.d.cts → core-DB7TdlyC.d.cts} +23 -7
  19. package/dist/{core-B6j6Fxse.d.ts → core-DB7TdlyC.d.ts} +23 -7
  20. package/dist/index-D7s7kCc2.d.cts +7 -0
  21. package/dist/index-D7s7kCc2.d.ts +7 -0
  22. package/dist/index.d.cts +4 -4
  23. package/dist/index.d.ts +4 -4
  24. package/dist/load-from-file-AVYOFOI7.js +7 -0
  25. package/dist/next/index.cjs +147 -142
  26. package/dist/next/index.js +58 -39
  27. package/dist/node/loader.cjs +242 -128
  28. package/dist/node/loader.d.cts +2 -2
  29. package/dist/node/loader.d.ts +2 -2
  30. package/dist/node/loader.js +29 -16
  31. package/dist/plugins/json-schema.cjs +22 -70
  32. package/dist/plugins/json-schema.d.cts +2 -2
  33. package/dist/plugins/json-schema.d.ts +2 -2
  34. package/dist/plugins/json-schema.js +19 -14
  35. package/dist/runtime/next/async.cjs +56 -11
  36. package/dist/runtime/next/async.d.cts +4 -6
  37. package/dist/runtime/next/async.d.ts +4 -6
  38. package/dist/runtime/next/async.js +3 -6
  39. package/dist/runtime/next/index.d.cts +5 -5
  40. package/dist/runtime/next/index.d.ts +5 -5
  41. package/dist/runtime/vite/browser.d.cts +3 -3
  42. package/dist/runtime/vite/browser.d.ts +3 -3
  43. package/dist/runtime/vite/server.d.cts +3 -3
  44. package/dist/runtime/vite/server.d.ts +3 -3
  45. package/dist/{types-DKGMoay5.d.cts → types-Bnh9n7mj.d.cts} +2 -2
  46. package/dist/{types-AGzTfBmf.d.ts → types-ey1AZqrg.d.ts} +2 -2
  47. package/dist/vite/index.cjs +265 -163
  48. package/dist/vite/index.d.cts +4 -0
  49. package/dist/vite/index.d.ts +4 -0
  50. package/dist/vite/index.js +140 -122
  51. package/dist/webpack/{index.cjs → mdx.cjs} +126 -104
  52. package/dist/webpack/mdx.d.cts +6 -0
  53. package/dist/webpack/mdx.d.ts +6 -0
  54. package/dist/webpack/{index.js → mdx.js} +12 -14
  55. package/dist/webpack/meta.cjs +528 -0
  56. package/dist/webpack/meta.d.cts +6 -0
  57. package/dist/webpack/meta.d.ts +6 -0
  58. package/dist/webpack/meta.js +42 -0
  59. package/loader-mdx.cjs +1 -1
  60. package/loader-meta.cjs +7 -0
  61. package/package.json +11 -8
  62. package/dist/chunk-U4MQ44TS.js +0 -53
  63. package/dist/chunk-XZY2AWJI.js +0 -81
  64. package/dist/chunk-YVCR6FUH.js +0 -82
  65. package/dist/load-MNG3CLET.js +0 -7
  66. package/dist/webpack/index.d.cts +0 -15
  67. package/dist/webpack/index.d.ts +0 -15
@@ -13,6 +13,10 @@ interface IndexFileOptions {
13
13
  * add `.js` extensions to imports, needed for ESM without bundler resolution
14
14
  */
15
15
  addJsExtension?: boolean;
16
+ /**
17
+ * Generate entry point for browser environment
18
+ */
19
+ browser?: boolean;
16
20
  }
17
21
 
18
22
  interface PluginOptions {
@@ -4,33 +4,33 @@ import {
4
4
  } from "../chunk-CXA4JO4Z.js";
5
5
  import {
6
6
  buildConfig
7
- } from "../chunk-U4MQ44TS.js";
8
- import {
9
- toVite
10
- } from "../chunk-YVCR6FUH.js";
7
+ } from "../chunk-2HXTGJBI.js";
11
8
  import {
12
9
  createMdxLoader
13
- } from "../chunk-XQ5O7IPO.js";
10
+ } from "../chunk-XYGORKQA.js";
11
+ import {
12
+ createMetaLoader
13
+ } from "../chunk-YAIPHUCZ.js";
14
+ import {
15
+ createIntegratedConfigLoader,
16
+ toVite
17
+ } from "../chunk-ZNVPB2IR.js";
14
18
  import "../chunk-3J3WL7WN.js";
15
19
  import "../chunk-K5ZLPEIQ.js";
20
+ import {
21
+ mdxLoaderGlob,
22
+ metaLoaderGlob
23
+ } from "../chunk-4JSFLXXT.js";
24
+ import "../chunk-VUEZTR2H.js";
16
25
  import {
17
26
  ValidationError,
18
27
  createCore,
19
- findConfigFile,
20
- validate
21
- } from "../chunk-EELYB2XC.js";
28
+ findConfigFile
29
+ } from "../chunk-TZ5EQBFW.js";
22
30
  import "../chunk-VWJKRQZR.js";
23
- import {
24
- getGlobPatterns
25
- } from "../chunk-XZY2AWJI.js";
26
31
 
27
32
  // src/vite/index.ts
28
- import {
29
- mergeConfig
30
- } from "vite";
31
- import { parse } from "querystring";
32
- import * as path3 from "path";
33
- import { load } from "js-yaml";
33
+ import { mergeConfig } from "vite";
34
34
 
35
35
  // src/utils/glob-import.ts
36
36
  import { globSync } from "tinyglobby";
@@ -59,27 +59,73 @@ function generateGlobImport(patterns, options) {
59
59
 
60
60
  // src/plugins/vite.ts
61
61
  import path2 from "path";
62
- function vite(options) {
62
+ function vite({
63
+ index
64
+ }) {
63
65
  let config;
66
+ let indexOptions;
67
+ if (index === false) indexOptions = false;
68
+ else indexOptions = applyDefaults(index === true ? {} : index);
64
69
  return {
70
+ name: "vite",
65
71
  config(v) {
66
72
  config = v;
67
73
  },
68
- emit() {
69
- return [
70
- {
71
- path: "index.ts",
72
- content: indexFile(this.configPath, this.outDir, config, options)
74
+ configureServer(server) {
75
+ if (!server.watcher || indexOptions === false || indexOptions.runtime === false)
76
+ return;
77
+ server.watcher.on("all", (event, file) => {
78
+ if (event === "change") return;
79
+ const isUpdated = config.collectionList.some((collection) => {
80
+ if (collection.type === "docs")
81
+ return collection.docs.hasFile(file) || collection.meta.hasFile(file);
82
+ return collection.hasFile(file);
83
+ });
84
+ if (isUpdated) {
85
+ this.core.emitAndWrite({
86
+ filterPlugin: (plugin) => plugin.name === "vite"
87
+ });
73
88
  }
74
- ];
89
+ });
90
+ },
91
+ emit() {
92
+ const out = [];
93
+ if (indexOptions === false) return out;
94
+ if (indexOptions.browser) {
95
+ out.push({
96
+ path: "browser.ts",
97
+ content: indexFile(this, config, indexOptions, "browser")
98
+ });
99
+ }
100
+ out.push({
101
+ path: "index.ts",
102
+ content: indexFile(
103
+ this,
104
+ config,
105
+ indexOptions,
106
+ indexOptions.browser ? "server" : "all"
107
+ )
108
+ });
109
+ return out;
75
110
  }
76
111
  };
77
112
  }
78
- function indexFile(configPath, outDir, config, options) {
79
- const { addJsExtension = false, runtime } = options;
113
+ function applyDefaults(options) {
114
+ return {
115
+ addJsExtension: options.addJsExtension ?? false,
116
+ browser: options.browser ?? false,
117
+ runtime: options.runtime ?? false
118
+ };
119
+ }
120
+ function indexFile({ configPath, outDir }, config, { addJsExtension, runtime }, environment) {
121
+ const runtimePath = {
122
+ all: "fumadocs-mdx/runtime/vite",
123
+ server: "fumadocs-mdx/runtime/vite.server",
124
+ browser: "fumadocs-mdx/runtime/vite.browser"
125
+ }[environment];
80
126
  const lines = [
81
127
  '/// <reference types="vite/client" />',
82
- `import { fromConfig } from 'fumadocs-mdx/runtime/vite';`,
128
+ `import { fromConfig } from '${runtimePath}';`,
83
129
  `import type * as Config from '${toImportPath(configPath, {
84
130
  relativeTo: outDir,
85
131
  jsExtension: addJsExtension
@@ -87,74 +133,68 @@ function indexFile(configPath, outDir, config, options) {
87
133
  "",
88
134
  `export const create = fromConfig<typeof Config>();`
89
135
  ];
90
- function docs(name, collection) {
91
- const obj = [
92
- ident(`doc: ${doc(name, collection.docs)}`),
93
- ident(`meta: ${meta(name, collection.meta)}`)
94
- ].join(",\n");
95
- return `{
136
+ function generateCollectionGlob(collection) {
137
+ if (collection.type === "docs") {
138
+ const obj = [
139
+ ident(`doc: ${generateCollectionGlob(collection.docs)}`),
140
+ ident(`meta: ${generateCollectionGlob(collection.meta)}`)
141
+ ].join(",\n");
142
+ return `{
96
143
  ${obj}
97
144
  }`;
98
- }
99
- function doc(name, collection) {
100
- const patterns = getGlobPatterns(collection);
145
+ }
101
146
  const dir = getCollectionDir(collection);
102
- const docGlob = generateGlob(patterns, {
103
- query: {
104
- collection: name
105
- },
106
- base: dir
107
- });
108
- if (collection.async) {
109
- const headBlob = generateGlob(patterns, {
147
+ if (collection.type === "doc") {
148
+ const docGlob = generateGlob(collection.patterns, {
110
149
  query: {
111
- only: "frontmatter",
112
- collection: name
150
+ collection: collection.name
113
151
  },
114
- import: "frontmatter",
115
152
  base: dir
116
153
  });
117
- return `create.docLazy("${name}", "${dir}", ${headBlob}, ${docGlob})`;
154
+ if (collection.async) {
155
+ const headBlob = generateGlob(collection.patterns, {
156
+ query: {
157
+ only: "frontmatter",
158
+ collection: collection.name
159
+ },
160
+ import: "frontmatter",
161
+ base: dir
162
+ });
163
+ return `create.docLazy("${collection.name}", "${dir}", ${headBlob}, ${docGlob})`;
164
+ }
165
+ return `create.doc("${collection.name}", "${dir}", ${docGlob})`;
118
166
  }
119
- return `create.doc("${name}", "${dir}", ${docGlob})`;
120
- }
121
- function meta(name, collection) {
122
- const patterns = getGlobPatterns(collection);
123
- const dir = getCollectionDir(collection);
124
- return `create.meta("${name}", "${dir}", ${generateGlob(patterns, {
125
- import: "default",
126
- base: dir,
127
- query: {
128
- collection: name
167
+ return `create.meta("${collection.name}", "${dir}", ${generateGlob(
168
+ collection.patterns,
169
+ {
170
+ import: "default",
171
+ base: dir,
172
+ query: {
173
+ collection: collection.name
174
+ }
129
175
  }
130
- })})`;
176
+ )})`;
131
177
  }
132
- function generateGlob(patterns, options2) {
178
+ function generateGlob(patterns, options) {
133
179
  patterns = patterns.map(normalizeGlobPath);
134
180
  if (runtime === "node" || runtime === "bun") {
135
- return generateGlobImport(patterns, options2);
181
+ return generateGlobImport(patterns, options);
136
182
  } else {
137
183
  return `import.meta.glob(${JSON.stringify(patterns)}, ${JSON.stringify(
138
184
  {
139
- ...options2,
140
- base: normalizeGlobPath(path2.relative(outDir, options2.base))
185
+ ...options,
186
+ base: normalizeGlobPath(path2.relative(outDir, options.base))
141
187
  },
142
188
  null,
143
189
  2
144
190
  )})`;
145
191
  }
146
192
  }
147
- for (const [name, collection] of config.collections.entries()) {
148
- let body;
149
- if (collection.type === "docs") {
150
- body = docs(name, collection);
151
- } else if (collection.type === "meta") {
152
- body = meta(name, collection);
153
- } else {
154
- body = doc(name, collection);
155
- }
193
+ for (const collection of config.collectionList) {
156
194
  lines.push("");
157
- lines.push(`export const ${name} = ${body};`);
195
+ lines.push(
196
+ `export const ${collection.name} = ${generateCollectionGlob(collection)};`
197
+ );
158
198
  }
159
199
  return lines.join("\n");
160
200
  }
@@ -164,8 +204,7 @@ function normalizeGlobPath(file) {
164
204
  if (file.startsWith("/")) return `.${file}`;
165
205
  return `./${file}`;
166
206
  }
167
- function getCollectionDir(collection) {
168
- const dir = collection.dir;
207
+ function getCollectionDir({ dir }) {
169
208
  if (Array.isArray(dir)) {
170
209
  if (dir.length !== 1)
171
210
  throw new Error(
@@ -175,54 +214,29 @@ function getCollectionDir(collection) {
175
214
  }
176
215
  return dir;
177
216
  }
178
- function slash(path4) {
179
- const isExtendedLengthPath = path4.startsWith("\\\\?\\");
217
+ function slash(path3) {
218
+ const isExtendedLengthPath = path3.startsWith("\\\\?\\");
180
219
  if (isExtendedLengthPath) {
181
- return path4;
220
+ return path3;
182
221
  }
183
- return path4.replaceAll("\\", "/");
222
+ return path3.replaceAll("\\", "/");
184
223
  }
185
224
 
186
225
  // src/vite/index.ts
187
226
  var FumadocsDeps = ["fumadocs-core", "fumadocs-ui", "fumadocs-openapi"];
188
227
  async function mdx(config, pluginOptions = {}) {
189
- const options = applyDefaults(pluginOptions);
228
+ const options = applyDefaults2(pluginOptions);
190
229
  const core = await createViteCore(options).init({
191
230
  config: buildConfig(config)
192
231
  });
193
- const mdxLoader = toVite(createMdxLoader(core.creatConfigLoader()));
194
- async function transformMeta(path4, query, value) {
195
- const isJson = path4.endsWith(".json");
196
- const parsed = parse(query);
197
- const collection = parsed.collection ? core.getConfig().collections.get(parsed.collection) : void 0;
198
- if (!collection) return null;
199
- let schema;
200
- switch (collection.type) {
201
- case "meta":
202
- schema = collection.schema;
203
- break;
204
- case "docs":
205
- schema = collection.meta.schema;
206
- break;
207
- }
208
- if (!schema) return null;
209
- let data;
210
- try {
211
- data = isJson ? JSON.parse(value) : load(value);
212
- } catch {
213
- return null;
214
- }
215
- const out = await validate(
216
- schema,
217
- data,
218
- { path: path4, source: value },
219
- `invalid data in ${path4}`
220
- );
221
- return {
222
- code: isJson ? JSON.stringify(out) : `export default ${JSON.stringify(out)}`,
223
- map: null
224
- };
225
- }
232
+ const configLoader = createIntegratedConfigLoader(core);
233
+ const mdxLoader = toVite(createMdxLoader(configLoader));
234
+ const metaLoader = toVite(
235
+ createMetaLoader(configLoader, {
236
+ // vite has built-in plugin for JSON files
237
+ json: "json"
238
+ })
239
+ );
226
240
  return {
227
241
  name: "fumadocs-mdx",
228
242
  // needed, otherwise other plugins will be executed before our `transform`.
@@ -248,13 +262,15 @@ async function mdx(config, pluginOptions = {}) {
248
262
  });
249
263
  },
250
264
  async transform(value, id) {
251
- const [file, query = ""] = id.split("?");
252
- const ext = path3.extname(file);
253
265
  try {
254
- if ([".yaml", ".json"].includes(ext))
255
- return await transformMeta(file, query, value);
256
- if ([".md", ".mdx"].includes(ext))
266
+ if (metaLoaderGlob.test(id)) {
267
+ const [file, query = ""] = id.split("?", 2);
268
+ return await metaLoader.call(this, file, query, value);
269
+ }
270
+ if (mdxLoaderGlob.test(id)) {
271
+ const [file, query = ""] = id.split("?", 2);
257
272
  return await mdxLoader.call(this, file, query, value);
273
+ }
258
274
  } catch (e) {
259
275
  if (e instanceof ValidationError) {
260
276
  throw new Error(e.toStringFormatted());
@@ -265,8 +281,8 @@ async function mdx(config, pluginOptions = {}) {
265
281
  };
266
282
  }
267
283
  async function postInstall(configPath = findConfigFile(), pluginOptions = {}) {
268
- const { loadConfig } = await import("../load-MNG3CLET.js");
269
- const options = applyDefaults(pluginOptions);
284
+ const { loadConfig } = await import("../load-from-file-AVYOFOI7.js");
285
+ const options = applyDefaults2(pluginOptions);
270
286
  const core = await createViteCore(options).init({
271
287
  config: loadConfig(configPath, options.outDir, true)
272
288
  });
@@ -284,11 +300,13 @@ function createViteCore({
284
300
  outDir
285
301
  },
286
302
  [
287
- generateIndexFile !== false && vite(typeof generateIndexFile === "object" ? generateIndexFile : {})
303
+ vite({
304
+ index: generateIndexFile
305
+ })
288
306
  ]
289
307
  );
290
308
  }
291
- function applyDefaults(options) {
309
+ function applyDefaults2(options) {
292
310
  return {
293
311
  updateViteConfig: options.updateViteConfig ?? true,
294
312
  generateIndexFile: options.generateIndexFile ?? true,