fumadocs-mdx 11.9.1 → 11.10.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 (37) hide show
  1. package/dist/{browser-CyU2Yl7A.d.cts → browser-B2G8uAF2.d.cts} +1 -1
  2. package/dist/{browser-DEsQvNRx.d.ts → browser-DrH7tKRi.d.ts} +1 -1
  3. package/dist/bun/index.cjs +667 -0
  4. package/dist/bun/index.d.cts +8 -0
  5. package/dist/bun/index.d.ts +8 -0
  6. package/dist/bun/index.js +50 -0
  7. package/dist/{chunk-766EAFX6.js → chunk-2HKRTQYP.js} +64 -0
  8. package/dist/chunk-5XJM5RPV.js +149 -0
  9. package/dist/chunk-NVX3U5YE.js +82 -0
  10. package/dist/config/index.d.cts +1 -1
  11. package/dist/config/index.d.ts +1 -1
  12. package/dist/config/zod-3.d.cts +1 -1
  13. package/dist/config/zod-3.d.ts +1 -1
  14. package/dist/{define-DnJzAZrj.d.cts → define-BH4bnHQl.d.cts} +6 -0
  15. package/dist/{define-DnJzAZrj.d.ts → define-BH4bnHQl.d.ts} +6 -0
  16. package/dist/index.d.cts +2 -2
  17. package/dist/index.d.ts +2 -2
  18. package/dist/loader-mdx.cjs +345 -265
  19. package/dist/loader-mdx.js +11 -80
  20. package/dist/next/index.js +4 -6
  21. package/dist/node/loader.cjs +748 -0
  22. package/dist/node/loader.d.cts +5 -0
  23. package/dist/node/loader.d.ts +5 -0
  24. package/dist/node/loader.js +23 -0
  25. package/dist/runtime/async.d.cts +2 -2
  26. package/dist/runtime/async.d.ts +2 -2
  27. package/dist/runtime/vite/browser.d.cts +2 -2
  28. package/dist/runtime/vite/browser.d.ts +2 -2
  29. package/dist/runtime/vite/server.d.cts +3 -3
  30. package/dist/runtime/vite/server.d.ts +3 -3
  31. package/dist/{types-WSHJKA8L.d.ts → types-DN9KrG7R.d.ts} +1 -1
  32. package/dist/{types-BmVgoqsr.d.cts → types-DT83Ijs6.d.cts} +1 -1
  33. package/dist/vite/index.cjs +356 -282
  34. package/dist/vite/index.js +12 -78
  35. package/package.json +17 -8
  36. package/dist/chunk-GX3THK2Q.js +0 -66
  37. package/dist/chunk-UCY7OBZG.js +0 -12
@@ -0,0 +1,50 @@
1
+ import {
2
+ createMdxLoader,
3
+ resolvedConfig
4
+ } from "../chunk-5XJM5RPV.js";
5
+ import {
6
+ findConfigFile
7
+ } from "../chunk-2HKRTQYP.js";
8
+ import "../chunk-QQWCBFFE.js";
9
+ import "../chunk-SVTXMVLQ.js";
10
+ import {
11
+ buildConfig
12
+ } from "../chunk-QVZ7JH4H.js";
13
+ import "../chunk-VWJKRQZR.js";
14
+
15
+ // src/bun/index.ts
16
+ import { parse } from "querystring";
17
+ function createMdxPlugin(options = {}) {
18
+ const { configPath = findConfigFile() } = options;
19
+ async function getMdxLoader() {
20
+ const out = buildConfig(await import(configPath));
21
+ return createMdxLoader(resolvedConfig(out));
22
+ }
23
+ return {
24
+ name: "bun-plugin-fumadocs-mdx",
25
+ setup(build) {
26
+ const mdxLoader = getMdxLoader();
27
+ build.onLoad({ filter: /\.mdx(\?.+?)?$/ }, async (args) => {
28
+ const [filePath, query] = args.path.split("?", 2);
29
+ const content = await Bun.file(filePath).text();
30
+ const result = await (await mdxLoader)({
31
+ source: content,
32
+ query: parse(query),
33
+ filePath,
34
+ development: false,
35
+ compiler: {
36
+ addDependency() {
37
+ }
38
+ }
39
+ });
40
+ return {
41
+ contents: result.code,
42
+ loader: "js"
43
+ };
44
+ });
45
+ }
46
+ };
47
+ }
48
+ export {
49
+ createMdxPlugin
50
+ };
@@ -2,6 +2,44 @@ 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
+
5
43
  // src/utils/config.ts
6
44
  import * as fs from "fs/promises";
7
45
  import * as path from "path";
@@ -83,7 +121,33 @@ async function getConfigHash(configPath) {
83
121
  throw new Error("Cannot find config file");
84
122
  }
85
123
 
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
+
86
147
  export {
148
+ ValidationError,
149
+ validate,
150
+ getGitTimestamp,
87
151
  findConfigFile,
88
152
  loadConfig,
89
153
  getConfigHash
@@ -0,0 +1,149 @@
1
+ import {
2
+ getConfigHash,
3
+ getGitTimestamp,
4
+ loadConfig,
5
+ validate
6
+ } from "./chunk-2HKRTQYP.js";
7
+ import {
8
+ buildMDX
9
+ } from "./chunk-QQWCBFFE.js";
10
+ import {
11
+ fumaMatter
12
+ } from "./chunk-VWJKRQZR.js";
13
+
14
+ // src/utils/count-lines.ts
15
+ function countLines(s) {
16
+ let num = 0;
17
+ for (const c of s) {
18
+ if (c === "\n") num++;
19
+ }
20
+ return num;
21
+ }
22
+
23
+ // src/loaders/mdx.ts
24
+ import { z } from "zod";
25
+ import fs from "fs/promises";
26
+ import path from "path";
27
+ import { createHash } from "crypto";
28
+ var querySchema = z.object({
29
+ only: z.literal(["frontmatter", "all"]).default("all"),
30
+ collection: z.string().optional(),
31
+ hash: z.string().describe(
32
+ "the hash of config, used for revalidation on Turbopack/Webpack."
33
+ ).optional()
34
+ }).loose();
35
+ var cacheEntry = z.object({
36
+ code: z.string(),
37
+ map: z.any().optional(),
38
+ hash: z.string().optional()
39
+ });
40
+ function createMdxLoader(configLoader) {
41
+ return async ({
42
+ source: value,
43
+ development: isDevelopment,
44
+ query,
45
+ compiler,
46
+ filePath
47
+ }) => {
48
+ const matter = fumaMatter(value);
49
+ const parsed = querySchema.parse(query);
50
+ const loaded = await configLoader.getConfig(parsed.hash);
51
+ const cacheDir = isDevelopment ? void 0 : loaded.global.experimentalBuildCache;
52
+ const cacheKey = `${parsed.hash}_${parsed.collection ?? "global"}_${generateCacheHash(filePath)}`;
53
+ if (cacheDir) {
54
+ const cached = await fs.readFile(path.join(cacheDir, cacheKey)).then((content) => cacheEntry.parse(JSON.parse(content.toString()))).catch(() => null);
55
+ if (cached && cached.hash === generateCacheHash(value)) return cached;
56
+ }
57
+ const collection = parsed.collection ? loaded.collections.get(parsed.collection) : void 0;
58
+ let schema;
59
+ let mdxOptions;
60
+ switch (collection?.type) {
61
+ case "doc":
62
+ mdxOptions = collection.mdxOptions;
63
+ schema = collection.schema;
64
+ break;
65
+ case "docs":
66
+ mdxOptions = collection.docs.mdxOptions;
67
+ schema = collection.docs.schema;
68
+ break;
69
+ }
70
+ if (schema) {
71
+ matter.data = await validate(
72
+ schema,
73
+ matter.data,
74
+ {
75
+ source: value,
76
+ path: filePath
77
+ },
78
+ `invalid frontmatter in ${filePath}`
79
+ );
80
+ }
81
+ if (parsed.only === "frontmatter") {
82
+ return {
83
+ code: `export const frontmatter = ${JSON.stringify(matter.data)}`,
84
+ map: null
85
+ };
86
+ }
87
+ const data = {};
88
+ if (loaded.global.lastModifiedTime === "git") {
89
+ data.lastModified = (await getGitTimestamp(filePath))?.getTime();
90
+ }
91
+ const lineOffset = isDevelopment ? countLines(matter.matter) : 0;
92
+ const compiled = await buildMDX(
93
+ `${parsed.hash ?? ""}:${parsed.collection ?? "global"}`,
94
+ "\n".repeat(lineOffset) + matter.content,
95
+ {
96
+ development: isDevelopment,
97
+ ...mdxOptions ?? await loaded.getDefaultMDXOptions(),
98
+ data,
99
+ filePath,
100
+ frontmatter: matter.data,
101
+ _compiler: compiler
102
+ }
103
+ );
104
+ const out = {
105
+ code: String(compiled.value),
106
+ map: compiled.map
107
+ };
108
+ if (cacheDir) {
109
+ await fs.mkdir(cacheDir, { recursive: true });
110
+ await fs.writeFile(
111
+ path.join(cacheDir, cacheKey),
112
+ JSON.stringify({
113
+ ...out,
114
+ hash: generateCacheHash(value)
115
+ })
116
+ );
117
+ }
118
+ return out;
119
+ };
120
+ }
121
+ function generateCacheHash(input) {
122
+ return createHash("md5").update(input).digest("hex");
123
+ }
124
+
125
+ // src/loaders/config-loader.ts
126
+ function resolvedConfig(loaded) {
127
+ return {
128
+ getConfig() {
129
+ return loaded;
130
+ }
131
+ };
132
+ }
133
+ function dynamicConfig(configPath, outDir) {
134
+ return {
135
+ async getConfig(hash) {
136
+ return loadConfig(
137
+ configPath,
138
+ outDir,
139
+ hash ?? await getConfigHash(configPath)
140
+ );
141
+ }
142
+ };
143
+ }
144
+
145
+ export {
146
+ createMdxLoader,
147
+ resolvedConfig,
148
+ dynamicConfig
149
+ };
@@ -0,0 +1,82 @@
1
+ import {
2
+ ValidationError
3
+ } from "./chunk-2HKRTQYP.js";
4
+
5
+ // src/loaders/adapter.ts
6
+ import { fileURLToPath } from "url";
7
+ import fs from "fs/promises";
8
+ import { parse } from "querystring";
9
+ import path from "path";
10
+ function toNode(loader, filterByPath) {
11
+ return async (url, _context, nextLoad) => {
12
+ if (!url.startsWith("file:///")) return nextLoad(url);
13
+ const parsedUrl = new URL(url);
14
+ const filePath = fileURLToPath(parsedUrl);
15
+ if (filterByPath(filePath)) {
16
+ const source = (await fs.readFile(filePath)).toString();
17
+ const result = await loader({
18
+ filePath,
19
+ query: Object.fromEntries(parsedUrl.searchParams.entries()),
20
+ source,
21
+ development: false,
22
+ compiler: {
23
+ addDependency() {
24
+ }
25
+ }
26
+ });
27
+ return {
28
+ source: result.code,
29
+ format: "module",
30
+ shortCircuit: true
31
+ };
32
+ }
33
+ return nextLoad(url);
34
+ };
35
+ }
36
+ function toVite(loader) {
37
+ return async function(file, query, value) {
38
+ const result = await loader({
39
+ filePath: file,
40
+ query: parse(query),
41
+ source: value,
42
+ development: this.environment.mode === "dev",
43
+ compiler: {
44
+ addDependency: (file2) => {
45
+ this.addWatchFile(file2);
46
+ }
47
+ }
48
+ });
49
+ return {
50
+ code: result.code,
51
+ map: result.map
52
+ };
53
+ };
54
+ }
55
+ function toWebpack(loader) {
56
+ return async function(source, callback) {
57
+ try {
58
+ const result = await loader({
59
+ filePath: this.resourcePath,
60
+ query: parse(this.resourceQuery),
61
+ source,
62
+ development: this.mode === "development",
63
+ compiler: this
64
+ });
65
+ callback(void 0, result.code, result.map);
66
+ } catch (error) {
67
+ if (error instanceof ValidationError) {
68
+ return callback(new Error(error.toStringFormatted()));
69
+ }
70
+ if (!(error instanceof Error)) throw error;
71
+ const fpath = path.relative(this.context, this.resourcePath);
72
+ error.message = `${fpath}:${error.name}: ${error.message}`;
73
+ callback(error);
74
+ }
75
+ };
76
+ }
77
+
78
+ export {
79
+ toNode,
80
+ toVite,
81
+ toWebpack
82
+ };
@@ -1,4 +1,4 @@
1
- export { A as AnyCollection, B as BaseCollection, C as CollectionSchema, D as DefaultMDXOptions, a as DocCollection, b as DocsCollection, G as GlobalConfig, M as MetaCollection, d as defineCollections, e as defineConfig, c as defineDocs, f as frontmatterSchema, g as getDefaultMDXOptions, m as metaSchema } from '../define-DnJzAZrj.cjs';
1
+ export { A as AnyCollection, B as BaseCollection, C as CollectionSchema, D as DefaultMDXOptions, a as DocCollection, b as DocsCollection, G as GlobalConfig, M as MetaCollection, d as defineCollections, e as defineConfig, c as defineDocs, f as frontmatterSchema, g as getDefaultMDXOptions, m as metaSchema } from '../define-BH4bnHQl.cjs';
2
2
  import { Processor, Transformer } from 'unified';
3
3
  import { Root } from 'mdast';
4
4
  import '@standard-schema/spec';
@@ -1,4 +1,4 @@
1
- export { A as AnyCollection, B as BaseCollection, C as CollectionSchema, D as DefaultMDXOptions, a as DocCollection, b as DocsCollection, G as GlobalConfig, M as MetaCollection, d as defineCollections, e as defineConfig, c as defineDocs, f as frontmatterSchema, g as getDefaultMDXOptions, m as metaSchema } from '../define-DnJzAZrj.js';
1
+ export { A as AnyCollection, B as BaseCollection, C as CollectionSchema, D as DefaultMDXOptions, a as DocCollection, b as DocsCollection, G as GlobalConfig, M as MetaCollection, d as defineCollections, e as defineConfig, c as defineDocs, f as frontmatterSchema, g as getDefaultMDXOptions, m as metaSchema } from '../define-BH4bnHQl.js';
2
2
  import { Processor, Transformer } from 'unified';
3
3
  import { Root } from 'mdast';
4
4
  import '@standard-schema/spec';
@@ -1,5 +1,5 @@
1
1
  import { z } from 'zod/v3';
2
- export { A as AnyCollection, B as BaseCollection, C as CollectionSchema, D as DefaultMDXOptions, a as DocCollection, b as DocsCollection, G as GlobalConfig, M as MetaCollection, d as defineCollections, e as defineConfig, c as defineDocs, g as getDefaultMDXOptions } from '../define-DnJzAZrj.cjs';
2
+ export { A as AnyCollection, B as BaseCollection, C as CollectionSchema, D as DefaultMDXOptions, a as DocCollection, b as DocsCollection, G as GlobalConfig, M as MetaCollection, d as defineCollections, e as defineConfig, c as defineDocs, g as getDefaultMDXOptions } from '../define-BH4bnHQl.cjs';
3
3
  export { Params, remarkInclude } from './index.cjs';
4
4
  import '@standard-schema/spec';
5
5
  import 'fumadocs-core/mdx-plugins';
@@ -1,5 +1,5 @@
1
1
  import { z } from 'zod/v3';
2
- export { A as AnyCollection, B as BaseCollection, C as CollectionSchema, D as DefaultMDXOptions, a as DocCollection, b as DocsCollection, G as GlobalConfig, M as MetaCollection, d as defineCollections, e as defineConfig, c as defineDocs, g as getDefaultMDXOptions } from '../define-DnJzAZrj.js';
2
+ export { A as AnyCollection, B as BaseCollection, C as CollectionSchema, D as DefaultMDXOptions, a as DocCollection, b as DocsCollection, G as GlobalConfig, M as MetaCollection, d as defineCollections, e as defineConfig, c as defineDocs, g as getDefaultMDXOptions } from '../define-BH4bnHQl.js';
3
3
  export { Params, remarkInclude } from './index.js';
4
4
  import '@standard-schema/spec';
5
5
  import 'fumadocs-core/mdx-plugins';
@@ -92,6 +92,12 @@ interface GlobalConfig {
92
92
  * @defaultValue 'none'
93
93
  */
94
94
  lastModifiedTime?: 'git' | 'none';
95
+ /**
96
+ * specify a directory to access & store cache (disabled during development mode).
97
+ *
98
+ * The cache will never be updated, delete the cache folder to clean.
99
+ */
100
+ experimentalBuildCache?: string;
95
101
  }
96
102
  declare function defineCollections<Schema extends StandardSchemaV1 = StandardSchemaV1, Async extends boolean = false>(options: DocCollection<Schema, Async>): DocCollection<Schema, Async>;
97
103
  declare function defineCollections<Schema extends StandardSchemaV1 = StandardSchemaV1>(options: MetaCollection<Schema>): MetaCollection<Schema>;
@@ -92,6 +92,12 @@ interface GlobalConfig {
92
92
  * @defaultValue 'none'
93
93
  */
94
94
  lastModifiedTime?: 'git' | 'none';
95
+ /**
96
+ * specify a directory to access & store cache (disabled during development mode).
97
+ *
98
+ * The cache will never be updated, delete the cache folder to clean.
99
+ */
100
+ experimentalBuildCache?: string;
95
101
  }
96
102
  declare function defineCollections<Schema extends StandardSchemaV1 = StandardSchemaV1, Async extends boolean = false>(options: DocCollection<Schema, Async>): DocCollection<Schema, Async>;
97
103
  declare function defineCollections<Schema extends StandardSchemaV1 = StandardSchemaV1>(options: MetaCollection<Schema>): MetaCollection<Schema>;
package/dist/index.d.cts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { PageData, MetaData, Source, VirtualFile } from 'fumadocs-core/source';
2
- import { R as Runtime, B as BaseCollectionEntry } from './types-BmVgoqsr.cjs';
2
+ import { R as Runtime, B as BaseCollectionEntry } from './types-DT83Ijs6.cjs';
3
3
  import '@standard-schema/spec';
4
- import './define-DnJzAZrj.cjs';
4
+ import './define-BH4bnHQl.cjs';
5
5
  import 'fumadocs-core/mdx-plugins';
6
6
  import '@mdx-js/mdx';
7
7
  import 'unified';
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { PageData, MetaData, Source, VirtualFile } from 'fumadocs-core/source';
2
- import { R as Runtime, B as BaseCollectionEntry } from './types-WSHJKA8L.js';
2
+ import { R as Runtime, B as BaseCollectionEntry } from './types-DN9KrG7R.js';
3
3
  import '@standard-schema/spec';
4
- import './define-DnJzAZrj.js';
4
+ import './define-BH4bnHQl.js';
5
5
  import 'fumadocs-core/mdx-plugins';
6
6
  import '@mdx-js/mdx';
7
7
  import 'unified';