fumadocs-mdx 11.10.0 → 11.10.1

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.
package/dist/bin.d.cts ADDED
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node
package/dist/bin.d.ts ADDED
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node
package/dist/bin.js ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/bin.ts
4
+ import { existsSync } from "fs";
5
+ async function start() {
6
+ const args = process.argv.slice(2);
7
+ const isNext = existsSync("next.config.js") || existsSync("next.config.mjs") || existsSync("next.config.ts");
8
+ if (isNext) {
9
+ const { postInstall } = await import("./next/index.js");
10
+ await postInstall(...args);
11
+ } else {
12
+ const { postInstall } = await import("./postinstall-XV4WSHZP.js");
13
+ await postInstall(...args);
14
+ }
15
+ }
16
+ void start();
@@ -630,10 +630,12 @@ function resolvedConfig(loaded) {
630
630
 
631
631
  // src/bun/index.ts
632
632
  var import_node_querystring = require("querystring");
633
+ var import_node_url2 = require("url");
633
634
  function createMdxPlugin(options = {}) {
634
635
  const { configPath = findConfigFile() } = options;
635
636
  async function getMdxLoader() {
636
- const out = buildConfig(await import(configPath));
637
+ const importPath = (0, import_node_url2.pathToFileURL)(configPath).href;
638
+ const out = buildConfig(await import(importPath));
637
639
  return createMdxLoader(resolvedConfig(out));
638
640
  }
639
641
  return {
package/dist/bun/index.js CHANGED
@@ -1,12 +1,13 @@
1
1
  import {
2
2
  createMdxLoader,
3
3
  resolvedConfig
4
- } from "../chunk-5XJM5RPV.js";
5
- import {
6
- findConfigFile
7
- } from "../chunk-2HKRTQYP.js";
4
+ } from "../chunk-QJCCVMBJ.js";
8
5
  import "../chunk-QQWCBFFE.js";
9
6
  import "../chunk-SVTXMVLQ.js";
7
+ import "../chunk-IQAEAI4P.js";
8
+ import {
9
+ findConfigFile
10
+ } from "../chunk-766EAFX6.js";
10
11
  import {
11
12
  buildConfig
12
13
  } from "../chunk-QVZ7JH4H.js";
@@ -14,10 +15,12 @@ import "../chunk-VWJKRQZR.js";
14
15
 
15
16
  // src/bun/index.ts
16
17
  import { parse } from "querystring";
18
+ import { pathToFileURL } from "url";
17
19
  function createMdxPlugin(options = {}) {
18
20
  const { configPath = findConfigFile() } = options;
19
21
  async function getMdxLoader() {
20
- const out = buildConfig(await import(configPath));
22
+ const importPath = pathToFileURL(configPath).href;
23
+ const out = buildConfig(await import(importPath));
21
24
  return createMdxLoader(resolvedConfig(out));
22
25
  }
23
26
  return {
@@ -0,0 +1,65 @@
1
+ // src/utils/import-formatter.ts
2
+ import path from "path";
3
+ function getImportCode(info) {
4
+ const specifier = JSON.stringify(info.specifier);
5
+ if (info.type === "default") return `import ${info.name} from ${specifier}`;
6
+ if (info.type === "namespace")
7
+ return `import * as ${info.name} from ${specifier}`;
8
+ if (info.type === "named") {
9
+ const names = info.names.map(
10
+ (name) => Array.isArray(name) ? `${name[0]} as ${name[1]}` : name
11
+ );
12
+ return `import { ${names.join(", ")} } from ${specifier}`;
13
+ }
14
+ return `import ${specifier}`;
15
+ }
16
+ function toImportPath(file, config) {
17
+ const ext = path.extname(file);
18
+ let filename;
19
+ if (ext === ".ts" && config.jsExtension) {
20
+ filename = file.substring(0, file.length - ext.length) + ".js";
21
+ } else if (ext === ".ts") {
22
+ filename = file.substring(0, file.length - ext.length);
23
+ } else {
24
+ filename = file;
25
+ }
26
+ let importPath;
27
+ if ("relativeTo" in config) {
28
+ importPath = path.relative(config.relativeTo, filename);
29
+ if (!path.isAbsolute(importPath) && !importPath.startsWith(".")) {
30
+ importPath = `./${importPath}`;
31
+ }
32
+ } else {
33
+ importPath = path.resolve(filename);
34
+ }
35
+ return importPath.replaceAll(path.sep, "/");
36
+ }
37
+ function ident(code, tab = 1) {
38
+ return code.split("\n").map((v) => " ".repeat(tab) + v).join("\n");
39
+ }
40
+
41
+ // src/utils/collections.ts
42
+ function getSupportedFormats(collection) {
43
+ return {
44
+ doc: ["mdx", "md"],
45
+ meta: ["json", "yaml"]
46
+ }[collection.type];
47
+ }
48
+ function getGlobPatterns(collection) {
49
+ if (collection.files) return collection.files;
50
+ return [`**/*.{${getSupportedFormats(collection).join(",")}}`];
51
+ }
52
+ function isFileSupported(filePath, collection) {
53
+ for (const format of getSupportedFormats(collection)) {
54
+ if (filePath.endsWith(`.${format}`)) return true;
55
+ }
56
+ return false;
57
+ }
58
+
59
+ export {
60
+ getImportCode,
61
+ toImportPath,
62
+ ident,
63
+ getGlobPatterns,
64
+ isFileSupported
65
+ };
@@ -2,44 +2,6 @@ import {
2
2
  buildConfig
3
3
  } from "./chunk-QVZ7JH4H.js";
4
4
 
5
- // src/utils/validation.ts
6
- import picocolors from "picocolors";
7
- var ValidationError = class extends Error {
8
- constructor(message, issues) {
9
- super(
10
- `${message}:
11
- ${issues.map((issue) => ` ${issue.path}: ${issue.message}`).join("\n")}`
12
- );
13
- this.title = message;
14
- this.issues = issues;
15
- }
16
- toStringFormatted() {
17
- return [
18
- picocolors.bold(`[MDX] ${this.title}:`),
19
- ...this.issues.map(
20
- (issue) => picocolors.redBright(
21
- `- ${picocolors.bold(issue.path?.join(".") ?? "*")}: ${issue.message}`
22
- )
23
- )
24
- ].join("\n");
25
- }
26
- };
27
- async function validate(schema, data, context, errorMessage) {
28
- if (typeof schema === "function" && !("~standard" in schema)) {
29
- schema = schema(context);
30
- }
31
- if ("~standard" in schema) {
32
- const result = await schema["~standard"].validate(
33
- data
34
- );
35
- if (result.issues) {
36
- throw new ValidationError(errorMessage, result.issues);
37
- }
38
- return result.value;
39
- }
40
- return data;
41
- }
42
-
43
5
  // src/utils/config.ts
44
6
  import * as fs from "fs/promises";
45
7
  import * as path from "path";
@@ -121,33 +83,7 @@ async function getConfigHash(configPath) {
121
83
  throw new Error("Cannot find config file");
122
84
  }
123
85
 
124
- // src/utils/git-timestamp.ts
125
- import path2 from "path";
126
- import { x } from "tinyexec";
127
- var cache2 = /* @__PURE__ */ new Map();
128
- async function getGitTimestamp(file) {
129
- const cached = cache2.get(file);
130
- if (cached) return cached;
131
- try {
132
- const out = await x(
133
- "git",
134
- ["log", "-1", '--pretty="%ai"', path2.relative(process.cwd(), file)],
135
- {
136
- throwOnError: true
137
- }
138
- );
139
- const time = new Date(out.stdout);
140
- cache2.set(file, time);
141
- return time;
142
- } catch {
143
- return;
144
- }
145
- }
146
-
147
86
  export {
148
- ValidationError,
149
- validate,
150
- getGitTimestamp,
151
87
  findConfigFile,
152
88
  loadConfig,
153
89
  getConfigHash
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  ValidationError
3
- } from "./chunk-2HKRTQYP.js";
3
+ } from "./chunk-IQAEAI4P.js";
4
4
 
5
5
  // src/loaders/adapter.ts
6
6
  import { fileURLToPath } from "url";
@@ -0,0 +1,66 @@
1
+ // src/utils/validation.ts
2
+ import picocolors from "picocolors";
3
+ var ValidationError = class extends Error {
4
+ constructor(message, issues) {
5
+ super(
6
+ `${message}:
7
+ ${issues.map((issue) => ` ${issue.path}: ${issue.message}`).join("\n")}`
8
+ );
9
+ this.title = message;
10
+ this.issues = issues;
11
+ }
12
+ toStringFormatted() {
13
+ return [
14
+ picocolors.bold(`[MDX] ${this.title}:`),
15
+ ...this.issues.map(
16
+ (issue) => picocolors.redBright(
17
+ `- ${picocolors.bold(issue.path?.join(".") ?? "*")}: ${issue.message}`
18
+ )
19
+ )
20
+ ].join("\n");
21
+ }
22
+ };
23
+ async function validate(schema, data, context, errorMessage) {
24
+ if (typeof schema === "function" && !("~standard" in schema)) {
25
+ schema = schema(context);
26
+ }
27
+ if ("~standard" in schema) {
28
+ const result = await schema["~standard"].validate(
29
+ data
30
+ );
31
+ if (result.issues) {
32
+ throw new ValidationError(errorMessage, result.issues);
33
+ }
34
+ return result.value;
35
+ }
36
+ return data;
37
+ }
38
+
39
+ // src/utils/git-timestamp.ts
40
+ import path from "path";
41
+ import { x } from "tinyexec";
42
+ var cache = /* @__PURE__ */ new Map();
43
+ async function getGitTimestamp(file) {
44
+ const cached = cache.get(file);
45
+ if (cached) return cached;
46
+ try {
47
+ const out = await x(
48
+ "git",
49
+ ["log", "-1", '--pretty="%ai"', path.relative(process.cwd(), file)],
50
+ {
51
+ throwOnError: true
52
+ }
53
+ );
54
+ const time = new Date(out.stdout);
55
+ cache.set(file, time);
56
+ return time;
57
+ } catch {
58
+ return;
59
+ }
60
+ }
61
+
62
+ export {
63
+ ValidationError,
64
+ validate,
65
+ getGitTimestamp
66
+ };
@@ -1,12 +1,14 @@
1
1
  import {
2
- getConfigHash,
2
+ buildMDX
3
+ } from "./chunk-QQWCBFFE.js";
4
+ import {
3
5
  getGitTimestamp,
4
- loadConfig,
5
6
  validate
6
- } from "./chunk-2HKRTQYP.js";
7
+ } from "./chunk-IQAEAI4P.js";
7
8
  import {
8
- buildMDX
9
- } from "./chunk-QQWCBFFE.js";
9
+ getConfigHash,
10
+ loadConfig
11
+ } from "./chunk-766EAFX6.js";
10
12
  import {
11
13
  fumaMatter
12
14
  } from "./chunk-VWJKRQZR.js";
@@ -1,4 +1,4 @@
1
- // src/runtime/index.ts
1
+ // src/runtime/next/index.ts
2
2
  import fs from "fs";
3
3
  var cache = /* @__PURE__ */ new Map();
4
4
  var _runtime = {
@@ -1,42 +1,16 @@
1
- // src/utils/import-formatter.ts
1
+ import {
2
+ getGlobPatterns,
3
+ ident,
4
+ toImportPath
5
+ } from "./chunk-6Y5JDZHD.js";
6
+ import {
7
+ findConfigFile,
8
+ loadConfig
9
+ } from "./chunk-766EAFX6.js";
10
+
11
+ // src/vite/postinstall.ts
12
+ import fs from "fs/promises";
2
13
  import path from "path";
3
- function getImportCode(info) {
4
- const specifier = JSON.stringify(info.specifier);
5
- if (info.type === "default") return `import ${info.name} from ${specifier}`;
6
- if (info.type === "namespace")
7
- return `import * as ${info.name} from ${specifier}`;
8
- if (info.type === "named") {
9
- const names = info.names.map(
10
- (name) => Array.isArray(name) ? `${name[0]} as ${name[1]}` : name
11
- );
12
- return `import { ${names.join(", ")} } from ${specifier}`;
13
- }
14
- return `import ${specifier}`;
15
- }
16
- function toImportPath(file, config) {
17
- const ext = path.extname(file);
18
- let filename;
19
- if (ext === ".ts" && config.jsExtension) {
20
- filename = file.substring(0, file.length - ext.length) + ".js";
21
- } else if (ext === ".ts") {
22
- filename = file.substring(0, file.length - ext.length);
23
- } else {
24
- filename = file;
25
- }
26
- let importPath;
27
- if ("relativeTo" in config) {
28
- importPath = path.relative(config.relativeTo, filename);
29
- if (!path.isAbsolute(importPath) && !importPath.startsWith(".")) {
30
- importPath = `./${importPath}`;
31
- }
32
- } else {
33
- importPath = path.resolve(filename);
34
- }
35
- return importPath.replaceAll(path.sep, "/");
36
- }
37
- function ident(code, tab = 1) {
38
- return code.split("\n").map((v) => " ".repeat(tab) + v).join("\n");
39
- }
40
14
 
41
15
  // src/vite/generate-glob.ts
42
16
  function generateGlob(name, patterns, globOptions) {
@@ -69,24 +43,6 @@ function getGlobBase(collection) {
69
43
  return enforceRelative(dir);
70
44
  }
71
45
 
72
- // src/utils/collections.ts
73
- function getSupportedFormats(collection) {
74
- return {
75
- doc: ["mdx", "md"],
76
- meta: ["json", "yaml"]
77
- }[collection.type];
78
- }
79
- function getGlobPatterns(collection) {
80
- if (collection.files) return collection.files;
81
- return [`**/*.{${getSupportedFormats(collection).join(",")}}`];
82
- }
83
- function isFileSupported(filePath, collection) {
84
- for (const format of getSupportedFormats(collection)) {
85
- if (filePath.endsWith(`.${format}`)) return true;
86
- }
87
- return false;
88
- }
89
-
90
46
  // src/vite/generate.ts
91
47
  function docs(name, collection) {
92
48
  const obj = [
@@ -149,10 +105,21 @@ function entry(configPath, config, outDir, jsExtension) {
149
105
  return lines.join("\n");
150
106
  }
151
107
 
108
+ // src/vite/postinstall.ts
109
+ async function postInstall(configPath = findConfigFile(), outDir, addJsExtension = false) {
110
+ const config = await loadConfig(configPath, "node_modules", void 0, true);
111
+ const outFile = "source.generated.ts";
112
+ if (outDir) {
113
+ await fs.mkdir(outDir, { recursive: true });
114
+ }
115
+ await fs.writeFile(
116
+ outDir ? path.join(outDir, outFile) : outFile,
117
+ entry(configPath, config, outDir ?? process.cwd(), addJsExtension)
118
+ );
119
+ console.log("[MDX] types generated");
120
+ }
121
+
152
122
  export {
153
- getImportCode,
154
- toImportPath,
155
- getGlobPatterns,
156
- isFileSupported,
157
- entry
123
+ entry,
124
+ postInstall
158
125
  };
@@ -1,13 +1,14 @@
1
1
  import {
2
2
  toWebpack
3
- } from "./chunk-NVX3U5YE.js";
3
+ } from "./chunk-CNKI574E.js";
4
4
  import {
5
5
  createMdxLoader,
6
6
  dynamicConfig
7
- } from "./chunk-5XJM5RPV.js";
8
- import "./chunk-2HKRTQYP.js";
7
+ } from "./chunk-QJCCVMBJ.js";
9
8
  import "./chunk-QQWCBFFE.js";
10
9
  import "./chunk-SVTXMVLQ.js";
10
+ import "./chunk-IQAEAI4P.js";
11
+ import "./chunk-766EAFX6.js";
11
12
  import "./chunk-QVZ7JH4H.js";
12
13
  import "./chunk-VWJKRQZR.js";
13
14
 
@@ -369,9 +369,9 @@ async function getConfigHash(configPath) {
369
369
  throw new Error("Cannot find config file");
370
370
  }
371
371
 
372
- // src/map/index.ts
373
- var path5 = __toESM(require("path"), 1);
374
- var fs3 = __toESM(require("fs/promises"), 1);
372
+ // src/next/index.ts
373
+ var import_node_path3 = __toESM(require("path"), 1);
374
+ var import_promises = __toESM(require("fs/promises"), 1);
375
375
 
376
376
  // src/map/generate.ts
377
377
  var path4 = __toESM(require("path"), 1);
@@ -514,9 +514,6 @@ function toImportPath(file, config) {
514
514
  }
515
515
  return importPath.replaceAll(import_node_path2.default.sep, "/");
516
516
  }
517
- function ident(code, tab = 1) {
518
- return code.split("\n").map((v) => " ".repeat(tab) + v).join("\n");
519
- }
520
517
 
521
518
  // src/utils/collections.ts
522
519
  function getSupportedFormats(collection) {
@@ -637,18 +634,18 @@ async function generateJS(configPath, config, importPath, configHash = false) {
637
634
  }
638
635
  const declares = entries.map(async ([k, collection]) => {
639
636
  if (collection.type === "docs") {
640
- const docs2 = await getCollectionFiles(collection.docs);
637
+ const docs = await getCollectionFiles(collection.docs);
641
638
  const metas = await getCollectionFiles(collection.meta);
642
639
  const metaEntries = (await getMetaEntries(collection.meta, metas)).join(
643
640
  ", "
644
641
  );
645
642
  if (collection.docs.async) {
646
- const docsEntries2 = (await getAsyncEntries(collection.docs, docs2)).join(
643
+ const docsEntries2 = (await getAsyncEntries(collection.docs, docs)).join(
647
644
  ", "
648
645
  );
649
646
  return `export const ${k} = _runtimeAsync.docs<typeof _source.${k}>([${docsEntries2}], [${metaEntries}], "${k}", _sourceConfig)`;
650
647
  }
651
- const docsEntries = (await getDocEntries(k, docs2)).join(", ");
648
+ const docsEntries = (await getDocEntries(k, docs)).join(", ");
652
649
  return `export const ${k} = _runtime.docs<typeof _source.${k}>([${docsEntries}], [${metaEntries}])`;
653
650
  }
654
651
  const files = await getCollectionFiles(collection);
@@ -699,6 +696,8 @@ function parseMetaEntry(file, content) {
699
696
  }
700
697
 
701
698
  // src/map/index.ts
699
+ var path5 = __toESM(require("path"), 1);
700
+ var fs3 = __toESM(require("fs/promises"), 1);
702
701
  async function start(dev, configPath, outDir) {
703
702
  let configHash = await getConfigHash(configPath);
704
703
  let config = await loadConfig(configPath, outDir, configHash, true);
@@ -823,132 +822,18 @@ function createMDX({
823
822
  };
824
823
  }
825
824
 
826
- // src/postinstall.ts
827
- var path6 = __toESM(require("path"), 1);
828
- var fs4 = __toESM(require("fs/promises"), 1);
829
- var import_node_fs2 = require("fs");
830
-
831
- // src/vite/generate-glob.ts
832
- function generateGlob(name, patterns, globOptions) {
833
- const options = {
834
- ...globOptions,
835
- query: {
836
- ...globOptions?.query,
837
- collection: name
838
- }
839
- };
840
- return `import.meta.glob(${JSON.stringify(mapGlobPatterns(patterns))}, ${JSON.stringify(options, null, 2)})`;
841
- }
842
- function mapGlobPatterns(patterns) {
843
- return patterns.map(enforceRelative);
844
- }
845
- function enforceRelative(file) {
846
- if (file.startsWith("./")) return file;
847
- if (file.startsWith("/")) return `.${file}`;
848
- return `./${file}`;
849
- }
850
- function getGlobBase(collection) {
851
- let dir = collection.dir;
852
- if (Array.isArray(dir)) {
853
- if (dir.length !== 1)
854
- throw new Error(
855
- `[Fumadocs MDX] Vite Plugin doesn't support multiple \`dir\` for a collection at the moment.`
856
- );
857
- dir = dir[0];
858
- }
859
- return enforceRelative(dir);
860
- }
861
-
862
- // src/vite/generate.ts
863
- function docs(name, collection) {
864
- const obj = [
865
- ident(`doc: ${doc(name, collection.docs)}`),
866
- ident(`meta: ${meta(name, collection.meta)}`)
867
- ].join(",\n");
868
- return `{
869
- ${obj}
870
- }`;
871
- }
872
- function doc(name, collection) {
873
- const patterns = getGlobPatterns(collection);
874
- const base = getGlobBase(collection);
875
- const docGlob = generateGlob(name, patterns, {
876
- base
877
- });
878
- if (collection.async) {
879
- const headBlob = generateGlob(name, patterns, {
880
- query: {
881
- only: "frontmatter"
882
- },
883
- import: "frontmatter",
884
- base
885
- });
886
- return `create.docLazy("${name}", "${base}", ${headBlob}, ${docGlob})`;
887
- }
888
- return `create.doc("${name}", "${base}", ${docGlob})`;
889
- }
890
- function meta(name, collection) {
891
- const patterns = getGlobPatterns(collection);
892
- const base = getGlobBase(collection);
893
- return `create.meta("${name}", "${base}", ${generateGlob(name, patterns, {
894
- import: "default",
895
- base
896
- })})`;
897
- }
898
- function entry(configPath, config, outDir, jsExtension) {
899
- const lines = [
900
- '/// <reference types="vite/client" />',
901
- `import { fromConfig } from 'fumadocs-mdx/runtime/vite';`,
902
- `import type * as Config from '${toImportPath(configPath, {
903
- relativeTo: outDir,
904
- jsExtension
905
- })}';`,
906
- "",
907
- `export const create = fromConfig<typeof Config>();`
908
- ];
909
- for (const [name, collection] of config.collections.entries()) {
910
- let body;
911
- if (collection.type === "docs") {
912
- body = docs(name, collection);
913
- } else if (collection.type === "meta") {
914
- body = meta(name, collection);
915
- } else {
916
- body = doc(name, collection);
917
- }
918
- lines.push("");
919
- lines.push(`export const ${name} = ${body};`);
920
- }
921
- return lines.join("\n");
922
- }
923
-
924
- // src/postinstall.ts
925
- async function postInstall(configPath = findConfigFile(), outDir) {
926
- 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.ts");
927
- if (isNext) {
928
- await onNext(configPath, outDir ?? ".source");
929
- } else {
930
- await onVite(configPath, outDir ?? process.cwd());
931
- }
932
- console.log("[MDX] types generated");
933
- }
934
- async function onNext(configPath, outDir) {
825
+ // src/next/index.ts
826
+ async function postInstall(configPath = findConfigFile(), outDir = ".source") {
935
827
  const config = await loadConfig(configPath, outDir, void 0, true);
936
- const outPath = path6.join(outDir, "index.ts");
937
- await fs4.rm(outDir, { recursive: true });
938
- await fs4.mkdir(outDir, { recursive: true });
828
+ const outPath = import_node_path3.default.join(outDir, "index.ts");
829
+ await import_promises.default.rm(outDir, { recursive: true });
830
+ await import_promises.default.mkdir(outDir, { recursive: true });
939
831
  const hash = await getConfigHash(configPath);
940
- await fs4.writeFile(
832
+ await import_promises.default.writeFile(
941
833
  outPath,
942
834
  await generateJS(configPath, config, { relativeTo: outDir }, hash)
943
835
  );
944
- }
945
- async function onVite(configPath, outDir, addJsExtension) {
946
- const config = await loadConfig(configPath, "node_modules", void 0, true);
947
- const outFile = "source.generated.ts";
948
- await fs4.writeFile(
949
- path6.join(outDir, outFile),
950
- entry(configPath, config, outDir, addJsExtension)
951
- );
836
+ console.log("[MDX] types generated");
952
837
  }
953
838
  // Annotate the CommonJS export names for ESM import in node:
954
839
  0 && (module.exports = {