fumadocs-mdx 13.0.8 → 14.0.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.cjs +985 -893
- package/dist/bin.js +3 -3
- package/dist/build-mdx-6UAK5FF5.js +8 -0
- package/dist/bun/index.cjs +613 -471
- package/dist/bun/index.d.cts +9 -2
- package/dist/bun/index.d.ts +9 -2
- package/dist/bun/index.js +15 -14
- package/dist/chunk-4JSFLXXT.js +8 -0
- package/dist/chunk-5OBUOALK.js +141 -0
- package/dist/{chunk-2E2JCOSO.js → chunk-6NISOLQ6.js} +16 -44
- package/dist/chunk-7UKSZSPY.js +331 -0
- package/dist/chunk-BEBCWQC7.js +224 -0
- package/dist/chunk-E5DJTSIM.js +86 -0
- package/dist/{chunk-K5ZLPEIQ.js → chunk-FBLMK4RS.js} +9 -6
- package/dist/chunk-IQGEFL2B.js +17 -0
- package/dist/{chunk-5FTSWCB4.js → chunk-K6HCOGOX.js} +9 -11
- package/dist/{chunk-QXHN25N3.js → chunk-MTTISKQJ.js} +6 -6
- package/dist/{chunk-3J3WL7WN.js → chunk-SLY7WXTX.js} +71 -58
- package/dist/chunk-TYJDYTKH.js +85 -0
- package/dist/chunk-WBIHDYMN.js +126 -0
- package/dist/{chunk-2HXTGJBI.js → chunk-ZY6UZ7NH.js} +22 -19
- package/dist/config/index.cjs +79 -71
- package/dist/config/index.d.cts +7 -1
- package/dist/config/index.d.ts +7 -1
- package/dist/config/index.js +5 -5
- package/dist/core-B9ZoS6sA.d.ts +341 -0
- package/dist/core-DTuP23zu.d.cts +341 -0
- package/dist/index-BD8Woo4m.d.cts +8 -0
- package/dist/index-CNOvhtOn.d.ts +8 -0
- package/dist/index.d.cts +38 -56
- package/dist/index.d.ts +38 -56
- package/dist/load-from-file-5HUQN36V.js +8 -0
- package/dist/next/index.cjs +763 -473
- package/dist/next/index.d.cts +16 -1
- package/dist/next/index.d.ts +16 -1
- package/dist/next/index.js +80 -281
- package/dist/node/loader.cjs +764 -627
- package/dist/node/loader.js +10 -11
- package/dist/plugins/index-file.cjs +495 -0
- package/dist/plugins/index-file.d.cts +14 -0
- package/dist/plugins/index-file.d.ts +14 -0
- package/dist/plugins/index-file.js +8 -0
- package/dist/plugins/json-schema.d.cts +9 -2
- package/dist/plugins/json-schema.d.ts +9 -2
- package/dist/plugins/last-modified.cjs +110 -0
- package/dist/plugins/last-modified.d.cts +37 -0
- package/dist/plugins/last-modified.d.ts +37 -0
- package/dist/plugins/last-modified.js +74 -0
- package/dist/runtime/browser.cjs +93 -0
- package/dist/runtime/browser.d.cts +53 -0
- package/dist/runtime/browser.d.ts +53 -0
- package/dist/runtime/browser.js +67 -0
- package/dist/runtime/dynamic.cjs +1023 -0
- package/dist/runtime/dynamic.d.cts +27 -0
- package/dist/runtime/dynamic.d.ts +27 -0
- package/dist/runtime/dynamic.js +79 -0
- package/dist/runtime/server.cjs +176 -0
- package/dist/runtime/server.d.cts +14 -0
- package/dist/runtime/server.d.ts +14 -0
- package/dist/runtime/server.js +8 -0
- package/dist/runtime/types.cjs +18 -0
- package/dist/runtime/types.d.cts +61 -0
- package/dist/runtime/types.d.ts +61 -0
- package/dist/runtime/types.js +0 -0
- package/dist/vite/index.cjs +984 -621
- package/dist/vite/index.d.cts +17 -22
- package/dist/vite/index.d.ts +17 -22
- package/dist/vite/index.js +32 -222
- package/dist/webpack/mdx.cjs +647 -514
- package/dist/webpack/mdx.d.cts +15 -1
- package/dist/webpack/mdx.d.ts +15 -1
- package/dist/webpack/mdx.js +12 -17
- package/dist/webpack/meta.cjs +360 -231
- package/dist/webpack/meta.d.cts +15 -1
- package/dist/webpack/meta.d.ts +15 -1
- package/dist/webpack/meta.js +13 -15
- package/package.json +15 -32
- package/dist/build-mdx-BjXOmv0b.d.cts +0 -53
- package/dist/build-mdx-CY5UldCO.d.ts +0 -53
- package/dist/chunk-2AQRQXSO.js +0 -119
- package/dist/chunk-CXA4JO4Z.js +0 -45
- package/dist/chunk-DMJ6I4C3.js +0 -76
- package/dist/chunk-FSZMKRVH.js +0 -80
- package/dist/chunk-II3H5ZVZ.js +0 -77
- package/dist/chunk-KILFIBVW.js +0 -75
- package/dist/chunk-NVRDCY6Z.js +0 -30
- package/dist/chunk-VUEZTR2H.js +0 -26
- package/dist/core-DB7TdlyC.d.cts +0 -234
- package/dist/core-DB7TdlyC.d.ts +0 -234
- package/dist/index-D7s7kCc2.d.cts +0 -7
- package/dist/index-D7s7kCc2.d.ts +0 -7
- package/dist/load-from-file-AVYOFOI7.js +0 -7
- package/dist/preset-ZMP6U62C.js +0 -6
- package/dist/runtime/next/async.cjs +0 -760
- package/dist/runtime/next/async.d.cts +0 -19
- package/dist/runtime/next/async.d.ts +0 -19
- package/dist/runtime/next/async.js +0 -86
- package/dist/runtime/next/index.cjs +0 -136
- package/dist/runtime/next/index.d.cts +0 -33
- package/dist/runtime/next/index.d.ts +0 -33
- package/dist/runtime/next/index.js +0 -11
- package/dist/runtime/vite/browser.cjs +0 -107
- package/dist/runtime/vite/browser.d.cts +0 -59
- package/dist/runtime/vite/browser.d.ts +0 -59
- package/dist/runtime/vite/browser.js +0 -11
- package/dist/runtime/vite/server.cjs +0 -243
- package/dist/runtime/vite/server.d.cts +0 -30
- package/dist/runtime/vite/server.d.ts +0 -30
- package/dist/runtime/vite/server.js +0 -111
- package/dist/types-Bnh9n7mj.d.cts +0 -45
- package/dist/types-ey1AZqrg.d.ts +0 -45
package/dist/bun/index.d.cts
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
import { BunPlugin } from 'bun';
|
|
2
|
-
import {
|
|
2
|
+
import { i as CoreOptions } from '../core-DTuP23zu.cjs';
|
|
3
3
|
import '@mdx-js/mdx';
|
|
4
4
|
import '@standard-schema/spec';
|
|
5
|
-
import 'fumadocs-core/mdx-plugins';
|
|
6
5
|
import 'unified';
|
|
6
|
+
import 'fumadocs-core/mdx-plugins';
|
|
7
7
|
import 'zod';
|
|
8
8
|
import 'chokidar';
|
|
9
|
+
import 'vfile';
|
|
10
|
+
import 'fumadocs-core/source';
|
|
11
|
+
import '../runtime/types.cjs';
|
|
12
|
+
import 'fumadocs-core/mdx-plugins/remark-structure';
|
|
13
|
+
import 'fumadocs-core/toc';
|
|
14
|
+
import 'mdast';
|
|
15
|
+
import 'mdx/types';
|
|
9
16
|
|
|
10
17
|
interface MdxPluginOptions extends Partial<CoreOptions> {
|
|
11
18
|
/**
|
package/dist/bun/index.d.ts
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
import { BunPlugin } from 'bun';
|
|
2
|
-
import {
|
|
2
|
+
import { i as CoreOptions } from '../core-B9ZoS6sA.js';
|
|
3
3
|
import '@mdx-js/mdx';
|
|
4
4
|
import '@standard-schema/spec';
|
|
5
|
-
import 'fumadocs-core/mdx-plugins';
|
|
6
5
|
import 'unified';
|
|
6
|
+
import 'fumadocs-core/mdx-plugins';
|
|
7
7
|
import 'zod';
|
|
8
8
|
import 'chokidar';
|
|
9
|
+
import 'vfile';
|
|
10
|
+
import 'fumadocs-core/source';
|
|
11
|
+
import '../runtime/types.js';
|
|
12
|
+
import 'fumadocs-core/mdx-plugins/remark-structure';
|
|
13
|
+
import 'fumadocs-core/toc';
|
|
14
|
+
import 'mdast';
|
|
15
|
+
import 'mdx/types';
|
|
9
16
|
|
|
10
17
|
interface MdxPluginOptions extends Partial<CoreOptions> {
|
|
11
18
|
/**
|
package/dist/bun/index.js
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
import {
|
|
2
2
|
buildConfig
|
|
3
|
-
} from "../chunk-
|
|
3
|
+
} from "../chunk-ZY6UZ7NH.js";
|
|
4
|
+
import "../chunk-E5DJTSIM.js";
|
|
4
5
|
import {
|
|
5
6
|
createMdxLoader
|
|
6
|
-
} from "../chunk-
|
|
7
|
+
} from "../chunk-6NISOLQ6.js";
|
|
7
8
|
import {
|
|
8
9
|
createMetaLoader
|
|
9
|
-
} from "../chunk-
|
|
10
|
+
} from "../chunk-TYJDYTKH.js";
|
|
10
11
|
import {
|
|
11
12
|
createIntegratedConfigLoader,
|
|
12
13
|
toBun
|
|
13
|
-
} from "../chunk-
|
|
14
|
-
import "../chunk-
|
|
15
|
-
import "../chunk-K5ZLPEIQ.js";
|
|
16
|
-
import "../chunk-VUEZTR2H.js";
|
|
14
|
+
} from "../chunk-K6HCOGOX.js";
|
|
15
|
+
import "../chunk-4JSFLXXT.js";
|
|
17
16
|
import {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
} from "../chunk-
|
|
17
|
+
_Defaults,
|
|
18
|
+
createCore
|
|
19
|
+
} from "../chunk-BEBCWQC7.js";
|
|
20
|
+
import "../chunk-WBIHDYMN.js";
|
|
21
21
|
import "../chunk-VWJKRQZR.js";
|
|
22
22
|
|
|
23
23
|
// src/bun/index.ts
|
|
@@ -25,19 +25,20 @@ import { pathToFileURL } from "url";
|
|
|
25
25
|
function createMdxPlugin(options = {}) {
|
|
26
26
|
const {
|
|
27
27
|
environment = "bun",
|
|
28
|
-
outDir =
|
|
29
|
-
configPath =
|
|
28
|
+
outDir = _Defaults.outDir,
|
|
29
|
+
configPath = _Defaults.configPath,
|
|
30
30
|
disableMetaFile = false
|
|
31
31
|
} = options;
|
|
32
32
|
return {
|
|
33
33
|
name: "bun-plugin-fumadocs-mdx",
|
|
34
34
|
async setup(build) {
|
|
35
35
|
const importPath = pathToFileURL(configPath).href;
|
|
36
|
-
const core =
|
|
36
|
+
const core = createCore({
|
|
37
37
|
environment,
|
|
38
38
|
outDir,
|
|
39
39
|
configPath
|
|
40
|
-
})
|
|
40
|
+
});
|
|
41
|
+
await core.init({
|
|
41
42
|
config: buildConfig(await import(importPath))
|
|
42
43
|
});
|
|
43
44
|
const configLoader = createIntegratedConfigLoader(core);
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// src/runtime/server.ts
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
function server(options = {}) {
|
|
4
|
+
const { doc: { passthroughs: docPassthroughs = [] } = {} } = options;
|
|
5
|
+
function fileInfo(file, base) {
|
|
6
|
+
if (file.startsWith("./")) {
|
|
7
|
+
file = file.slice(2);
|
|
8
|
+
}
|
|
9
|
+
return {
|
|
10
|
+
path: file,
|
|
11
|
+
fullPath: path.join(base, file)
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function mapDocData(entry) {
|
|
15
|
+
const data = {
|
|
16
|
+
body: entry.default,
|
|
17
|
+
toc: entry.toc,
|
|
18
|
+
structuredData: entry.structuredData,
|
|
19
|
+
_exports: entry
|
|
20
|
+
};
|
|
21
|
+
for (const key of docPassthroughs) {
|
|
22
|
+
data[key] = entry[key];
|
|
23
|
+
}
|
|
24
|
+
return data;
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
async doc(_name, base, glob) {
|
|
28
|
+
const out = await Promise.all(
|
|
29
|
+
Object.entries(glob).map(async ([k, v]) => {
|
|
30
|
+
const data = typeof v === "function" ? await v() : v;
|
|
31
|
+
return {
|
|
32
|
+
...mapDocData(data),
|
|
33
|
+
...data.frontmatter,
|
|
34
|
+
...createDocMethods(fileInfo(k, base), () => data)
|
|
35
|
+
};
|
|
36
|
+
})
|
|
37
|
+
);
|
|
38
|
+
return out;
|
|
39
|
+
},
|
|
40
|
+
async docLazy(_name, base, head, body) {
|
|
41
|
+
const out = await Promise.all(
|
|
42
|
+
Object.entries(head).map(async ([k, v]) => {
|
|
43
|
+
const data = typeof v === "function" ? await v() : v;
|
|
44
|
+
const content = body[k];
|
|
45
|
+
return {
|
|
46
|
+
...data,
|
|
47
|
+
...createDocMethods(fileInfo(k, base), content),
|
|
48
|
+
async load() {
|
|
49
|
+
return mapDocData(await content());
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
})
|
|
53
|
+
);
|
|
54
|
+
return out;
|
|
55
|
+
},
|
|
56
|
+
async meta(_name, base, glob) {
|
|
57
|
+
const out = await Promise.all(
|
|
58
|
+
Object.entries(glob).map(async ([k, v]) => {
|
|
59
|
+
const data = typeof v === "function" ? await v() : v;
|
|
60
|
+
return {
|
|
61
|
+
info: fileInfo(k, base),
|
|
62
|
+
...data
|
|
63
|
+
};
|
|
64
|
+
})
|
|
65
|
+
);
|
|
66
|
+
return out;
|
|
67
|
+
},
|
|
68
|
+
async docs(name, base, metaGlob, docGlob) {
|
|
69
|
+
const entry = {
|
|
70
|
+
docs: await this.doc(name, base, docGlob),
|
|
71
|
+
meta: await this.meta(name, base, metaGlob),
|
|
72
|
+
toFumadocsSource() {
|
|
73
|
+
return toFumadocsSource(this.docs, this.meta);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
return entry;
|
|
77
|
+
},
|
|
78
|
+
async docsLazy(name, base, metaGlob, docHeadGlob, docBodyGlob) {
|
|
79
|
+
const entry = {
|
|
80
|
+
docs: await this.docLazy(name, base, docHeadGlob, docBodyGlob),
|
|
81
|
+
meta: await this.meta(name, base, metaGlob),
|
|
82
|
+
toFumadocsSource() {
|
|
83
|
+
return toFumadocsSource(this.docs, this.meta);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
return entry;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function toFumadocsSource(pages, metas) {
|
|
91
|
+
const files = [];
|
|
92
|
+
for (const entry of pages) {
|
|
93
|
+
files.push({
|
|
94
|
+
type: "page",
|
|
95
|
+
path: entry.info.path,
|
|
96
|
+
absolutePath: entry.info.fullPath,
|
|
97
|
+
data: entry
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
for (const entry of metas) {
|
|
101
|
+
files.push({
|
|
102
|
+
type: "meta",
|
|
103
|
+
path: entry.info.path,
|
|
104
|
+
absolutePath: entry.info.fullPath,
|
|
105
|
+
data: entry
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
files
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function createDocMethods(info, load) {
|
|
113
|
+
return {
|
|
114
|
+
info,
|
|
115
|
+
async getText(type) {
|
|
116
|
+
if (type === "raw") {
|
|
117
|
+
const fs = await import("fs/promises");
|
|
118
|
+
return (await fs.readFile(info.fullPath)).toString();
|
|
119
|
+
}
|
|
120
|
+
const data = await load();
|
|
121
|
+
if (typeof data._markdown !== "string")
|
|
122
|
+
throw new Error(
|
|
123
|
+
"getText('processed') requires `includeProcessedMarkdown` to be enabled in your collection config."
|
|
124
|
+
);
|
|
125
|
+
return data._markdown;
|
|
126
|
+
},
|
|
127
|
+
async getMDAST() {
|
|
128
|
+
const data = await load();
|
|
129
|
+
if (!data._mdast)
|
|
130
|
+
throw new Error(
|
|
131
|
+
"getMDAST() requires `includeMDAST` to be enabled in your collection config."
|
|
132
|
+
);
|
|
133
|
+
return JSON.parse(data._mdast);
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export {
|
|
139
|
+
server,
|
|
140
|
+
toFumadocsSource
|
|
141
|
+
};
|
|
@@ -1,13 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
} from "./chunk-
|
|
4
|
-
import {
|
|
5
|
-
getGitTimestamp
|
|
6
|
-
} from "./chunk-VUEZTR2H.js";
|
|
7
|
-
import {
|
|
8
|
-
mdxLoaderGlob,
|
|
9
|
-
validate
|
|
10
|
-
} from "./chunk-2AQRQXSO.js";
|
|
2
|
+
mdxLoaderGlob
|
|
3
|
+
} from "./chunk-4JSFLXXT.js";
|
|
11
4
|
import {
|
|
12
5
|
fumaMatter
|
|
13
6
|
} from "./chunk-VWJKRQZR.js";
|
|
@@ -36,10 +29,10 @@ function createMdxLoader(configLoader) {
|
|
|
36
29
|
compiler,
|
|
37
30
|
filePath
|
|
38
31
|
}) {
|
|
32
|
+
const config = await configLoader.getConfig();
|
|
39
33
|
const value = await getSource();
|
|
40
34
|
const matter = fumaMatter(value);
|
|
41
35
|
const parsed = querySchema.parse(query);
|
|
42
|
-
const config = await configLoader.getConfig();
|
|
43
36
|
let after;
|
|
44
37
|
if (!isDevelopment && config.global.experimentalBuildCache) {
|
|
45
38
|
const cacheDir = config.global.experimentalBuildCache;
|
|
@@ -67,15 +60,10 @@ function createMdxLoader(configLoader) {
|
|
|
67
60
|
docCollection = collection.docs;
|
|
68
61
|
break;
|
|
69
62
|
}
|
|
70
|
-
if (docCollection
|
|
71
|
-
matter.data = await
|
|
72
|
-
docCollection
|
|
73
|
-
matter.data
|
|
74
|
-
{
|
|
75
|
-
source: value,
|
|
76
|
-
path: filePath
|
|
77
|
-
},
|
|
78
|
-
`invalid frontmatter in ${filePath}`
|
|
63
|
+
if (docCollection) {
|
|
64
|
+
matter.data = await configLoader.core.transformFrontmatter(
|
|
65
|
+
{ collection: docCollection, filePath, source: value },
|
|
66
|
+
matter.data
|
|
79
67
|
);
|
|
80
68
|
}
|
|
81
69
|
if (parsed.only === "frontmatter") {
|
|
@@ -84,24 +72,16 @@ function createMdxLoader(configLoader) {
|
|
|
84
72
|
map: null
|
|
85
73
|
};
|
|
86
74
|
}
|
|
87
|
-
const data = {};
|
|
88
|
-
if (config.global.lastModifiedTime === "git") {
|
|
89
|
-
data.lastModified = (await getGitTimestamp(filePath))?.getTime();
|
|
90
|
-
}
|
|
91
75
|
const lineOffset = isDevelopment ? countLines(matter.matter) : 0;
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
frontmatter: matter.data,
|
|
102
|
-
_compiler: compiler
|
|
103
|
-
}
|
|
104
|
-
);
|
|
76
|
+
const { buildMDX } = await import("./build-mdx-6UAK5FF5.js");
|
|
77
|
+
const compiled = await buildMDX(configLoader.core, docCollection, {
|
|
78
|
+
isDevelopment,
|
|
79
|
+
source: "\n".repeat(lineOffset) + matter.content,
|
|
80
|
+
filePath,
|
|
81
|
+
frontmatter: matter.data,
|
|
82
|
+
_compiler: compiler,
|
|
83
|
+
environment: "bundler"
|
|
84
|
+
});
|
|
105
85
|
const out = {
|
|
106
86
|
code: String(compiled.value),
|
|
107
87
|
map: compiled.map
|
|
@@ -111,14 +91,6 @@ function createMdxLoader(configLoader) {
|
|
|
111
91
|
}
|
|
112
92
|
};
|
|
113
93
|
}
|
|
114
|
-
var hashes = /* @__PURE__ */ new WeakMap();
|
|
115
|
-
function getConfigHash(config) {
|
|
116
|
-
let hash = hashes.get(config);
|
|
117
|
-
if (hash) return hash;
|
|
118
|
-
hash = Date.now().toString();
|
|
119
|
-
hashes.set(config, hash);
|
|
120
|
-
return hash;
|
|
121
|
-
}
|
|
122
94
|
function generateCacheHash(input) {
|
|
123
95
|
return createHash("md5").update(input).digest("hex");
|
|
124
96
|
}
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createCodegen,
|
|
3
|
+
ident
|
|
4
|
+
} from "./chunk-WBIHDYMN.js";
|
|
5
|
+
import {
|
|
6
|
+
fumaMatter
|
|
7
|
+
} from "./chunk-VWJKRQZR.js";
|
|
8
|
+
|
|
9
|
+
// src/plugins/index-file.ts
|
|
10
|
+
import path2 from "path";
|
|
11
|
+
import { glob } from "tinyglobby";
|
|
12
|
+
|
|
13
|
+
// src/utils/fs-cache.ts
|
|
14
|
+
import { LRUCache } from "lru-cache";
|
|
15
|
+
import fs from "fs/promises";
|
|
16
|
+
import path from "path";
|
|
17
|
+
var map = new LRUCache({
|
|
18
|
+
max: 100
|
|
19
|
+
});
|
|
20
|
+
function toFullPath(file) {
|
|
21
|
+
if (path.isAbsolute(file)) {
|
|
22
|
+
return path.relative(process.cwd(), file);
|
|
23
|
+
}
|
|
24
|
+
return file;
|
|
25
|
+
}
|
|
26
|
+
function readFileWithCache(file) {
|
|
27
|
+
const fullPath = toFullPath(file);
|
|
28
|
+
const cached = map.get(fullPath);
|
|
29
|
+
if (cached) return cached;
|
|
30
|
+
const read = fs.readFile(fullPath).then((s) => s.toString());
|
|
31
|
+
map.set(fullPath, read);
|
|
32
|
+
return read;
|
|
33
|
+
}
|
|
34
|
+
function removeFileCache(file) {
|
|
35
|
+
map.delete(toFullPath(file));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// src/plugins/index-file.ts
|
|
39
|
+
import { createHash } from "crypto";
|
|
40
|
+
function indexFile(options = {}) {
|
|
41
|
+
const {
|
|
42
|
+
target = "default",
|
|
43
|
+
addJsExtension,
|
|
44
|
+
browser = true,
|
|
45
|
+
dynamic = true
|
|
46
|
+
} = options;
|
|
47
|
+
let config;
|
|
48
|
+
let dynamicCollections;
|
|
49
|
+
function isDynamic(collection) {
|
|
50
|
+
return collection.type === "docs" && collection.docs.dynamic || collection.type === "doc" && collection.dynamic;
|
|
51
|
+
}
|
|
52
|
+
function generateConfigs(core) {
|
|
53
|
+
const serverOptions = {};
|
|
54
|
+
const typeConfigs = [];
|
|
55
|
+
const ctx = core.getPluginContext();
|
|
56
|
+
for (const plugin of core.getPlugins()) {
|
|
57
|
+
const indexFilePlugin = plugin["index-file"];
|
|
58
|
+
if (!indexFilePlugin) continue;
|
|
59
|
+
indexFilePlugin.serverOptions?.call(ctx, serverOptions);
|
|
60
|
+
const config2 = indexFilePlugin.generateTypeConfig?.call(ctx);
|
|
61
|
+
if (config2) typeConfigs.push(config2);
|
|
62
|
+
}
|
|
63
|
+
if (typeConfigs.length === 0) {
|
|
64
|
+
typeConfigs.push("{ DocData: {} }");
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
serverOptions,
|
|
68
|
+
tc: typeConfigs.join(" & ")
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
name: "index-file",
|
|
73
|
+
config(v) {
|
|
74
|
+
config = v;
|
|
75
|
+
dynamicCollections = config.collectionList.filter(isDynamic);
|
|
76
|
+
},
|
|
77
|
+
configureServer(server) {
|
|
78
|
+
if (!server.watcher) return;
|
|
79
|
+
server.watcher.on("all", async (event, file) => {
|
|
80
|
+
removeFileCache(file);
|
|
81
|
+
if (dynamicCollections.length === 0) {
|
|
82
|
+
if (target === "vite") return;
|
|
83
|
+
if (target === "default" && event === "change") return;
|
|
84
|
+
}
|
|
85
|
+
const updatedCollection = config.collectionList.find(
|
|
86
|
+
(collection) => collection.hasFile(file)
|
|
87
|
+
);
|
|
88
|
+
if (!updatedCollection) return;
|
|
89
|
+
if (!isDynamic(updatedCollection)) {
|
|
90
|
+
if (target === "vite") return;
|
|
91
|
+
if (target === "default" && event === "change") return;
|
|
92
|
+
}
|
|
93
|
+
await this.core.emitAndWrite({
|
|
94
|
+
filterPlugin: (plugin) => plugin.name === "index-file"
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
},
|
|
98
|
+
async emit() {
|
|
99
|
+
const globCache = /* @__PURE__ */ new Map();
|
|
100
|
+
const { serverOptions, tc } = generateConfigs(this.core);
|
|
101
|
+
const toEmitEntry = async (path3, content) => {
|
|
102
|
+
const codegen = createCodegen({
|
|
103
|
+
target,
|
|
104
|
+
outDir: this.outDir,
|
|
105
|
+
jsExtension: addJsExtension,
|
|
106
|
+
globCache
|
|
107
|
+
});
|
|
108
|
+
await content({
|
|
109
|
+
core: this.core,
|
|
110
|
+
codegen,
|
|
111
|
+
serverOptions,
|
|
112
|
+
tc
|
|
113
|
+
});
|
|
114
|
+
return {
|
|
115
|
+
path: path3,
|
|
116
|
+
content: codegen.toString()
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
const out = [
|
|
120
|
+
toEmitEntry("server.ts", generateServerIndexFile)
|
|
121
|
+
];
|
|
122
|
+
if (dynamic)
|
|
123
|
+
out.push(toEmitEntry("dynamic.ts", generateDynamicIndexFile));
|
|
124
|
+
if (browser)
|
|
125
|
+
out.push(toEmitEntry("browser.ts", generateBrowserIndexFile));
|
|
126
|
+
return await Promise.all(out);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
async function generateServerIndexFile({
|
|
131
|
+
core,
|
|
132
|
+
codegen,
|
|
133
|
+
serverOptions,
|
|
134
|
+
tc
|
|
135
|
+
}) {
|
|
136
|
+
codegen.lines.push(
|
|
137
|
+
`import { server } from 'fumadocs-mdx/runtime/server';`,
|
|
138
|
+
`import type * as Config from '${codegen.formatImportPath(core.getOptions().configPath)}';`,
|
|
139
|
+
"",
|
|
140
|
+
`const create = server<typeof Config, ${tc}>(${JSON.stringify(serverOptions)});`
|
|
141
|
+
);
|
|
142
|
+
async function generateCollectionObject(collection) {
|
|
143
|
+
switch (collection.type) {
|
|
144
|
+
case "docs": {
|
|
145
|
+
if (collection.docs.dynamic) return;
|
|
146
|
+
if (collection.docs.async) {
|
|
147
|
+
const [metaGlob2, headGlob, bodyGlob] = await Promise.all([
|
|
148
|
+
generateMetaCollectionGlob(codegen, collection.meta, true),
|
|
149
|
+
generateDocCollectionFrontmatterGlob(
|
|
150
|
+
codegen,
|
|
151
|
+
collection.docs,
|
|
152
|
+
true
|
|
153
|
+
),
|
|
154
|
+
generateDocCollectionGlob(codegen, collection.docs)
|
|
155
|
+
]);
|
|
156
|
+
return `await create.docsLazy("${collection.name}", "${collection.dir}", ${metaGlob2}, ${headGlob}, ${bodyGlob})`;
|
|
157
|
+
}
|
|
158
|
+
const [metaGlob, docGlob] = await Promise.all([
|
|
159
|
+
generateMetaCollectionGlob(codegen, collection.meta, true),
|
|
160
|
+
generateDocCollectionGlob(codegen, collection.docs, true)
|
|
161
|
+
]);
|
|
162
|
+
return `await create.docs("${collection.name}", "${collection.dir}", ${metaGlob}, ${docGlob})`;
|
|
163
|
+
}
|
|
164
|
+
case "doc":
|
|
165
|
+
if (collection.dynamic) return;
|
|
166
|
+
if (collection.async) {
|
|
167
|
+
const [headGlob, bodyGlob] = await Promise.all([
|
|
168
|
+
generateDocCollectionFrontmatterGlob(codegen, collection, true),
|
|
169
|
+
generateDocCollectionGlob(codegen, collection)
|
|
170
|
+
]);
|
|
171
|
+
return `await create.docLazy("${collection.name}", "${collection.dir}", ${headGlob}, ${bodyGlob})`;
|
|
172
|
+
}
|
|
173
|
+
return `await create.doc("${collection.name}", "${collection.dir}", ${await generateDocCollectionGlob(
|
|
174
|
+
codegen,
|
|
175
|
+
collection,
|
|
176
|
+
true
|
|
177
|
+
)})`;
|
|
178
|
+
case "meta":
|
|
179
|
+
return `await create.meta("${collection.name}", "${collection.dir}", ${await generateMetaCollectionGlob(
|
|
180
|
+
codegen,
|
|
181
|
+
collection,
|
|
182
|
+
true
|
|
183
|
+
)})`;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
await codegen.pushAsync(
|
|
187
|
+
core.getConfig().collectionList.map(async (collection) => {
|
|
188
|
+
const obj = await generateCollectionObject(collection);
|
|
189
|
+
if (!obj) return;
|
|
190
|
+
return `
|
|
191
|
+
export const ${collection.name} = ${obj};`;
|
|
192
|
+
})
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
async function generateDynamicIndexFile({
|
|
196
|
+
core,
|
|
197
|
+
codegen,
|
|
198
|
+
serverOptions,
|
|
199
|
+
tc
|
|
200
|
+
}) {
|
|
201
|
+
const { configPath } = core.getOptions();
|
|
202
|
+
codegen.lines.push(
|
|
203
|
+
`import { dynamic } from 'fumadocs-mdx/runtime/dynamic';`,
|
|
204
|
+
`import * as Config from '${codegen.formatImportPath(configPath)}';`,
|
|
205
|
+
"",
|
|
206
|
+
`const create = await dynamic<typeof Config, ${tc}>(Config, ${JSON.stringify(core.getOptions())}, ${JSON.stringify(serverOptions)});`
|
|
207
|
+
);
|
|
208
|
+
async function generateCollectionObjectEntry(collection, file) {
|
|
209
|
+
const fullPath = path2.join(collection.dir, file);
|
|
210
|
+
const content = await readFileWithCache(fullPath).catch(() => "");
|
|
211
|
+
const parsed = fumaMatter(content);
|
|
212
|
+
const data = await core.transformFrontmatter(
|
|
213
|
+
{
|
|
214
|
+
collection,
|
|
215
|
+
filePath: fullPath,
|
|
216
|
+
source: content
|
|
217
|
+
},
|
|
218
|
+
parsed.data
|
|
219
|
+
);
|
|
220
|
+
const hash = createHash("md5").update(content).digest("hex");
|
|
221
|
+
const infoStr = [
|
|
222
|
+
// make sure it's included in vercel/nft
|
|
223
|
+
`absolutePath: path.resolve(${JSON.stringify(fullPath)})`
|
|
224
|
+
];
|
|
225
|
+
for (const [k, v] of Object.entries({
|
|
226
|
+
info: {
|
|
227
|
+
fullPath,
|
|
228
|
+
path: file
|
|
229
|
+
},
|
|
230
|
+
data,
|
|
231
|
+
hash
|
|
232
|
+
})) {
|
|
233
|
+
infoStr.push(`${k}: ${JSON.stringify(v)}`);
|
|
234
|
+
}
|
|
235
|
+
return `{ ${infoStr.join(", ")} }`;
|
|
236
|
+
}
|
|
237
|
+
async function generateCollectionObject(parent) {
|
|
238
|
+
let collection;
|
|
239
|
+
if (parent.type === "doc") collection = parent;
|
|
240
|
+
else if (parent.type === "docs") collection = parent.docs;
|
|
241
|
+
if (!collection || !collection.dynamic) return;
|
|
242
|
+
const files = await glob(collection.patterns, {
|
|
243
|
+
cwd: collection.dir
|
|
244
|
+
});
|
|
245
|
+
const entries = await Promise.all(
|
|
246
|
+
files.map((file) => generateCollectionObjectEntry(collection, file))
|
|
247
|
+
);
|
|
248
|
+
switch (parent.type) {
|
|
249
|
+
case "docs": {
|
|
250
|
+
const metaGlob = await generateMetaCollectionGlob(
|
|
251
|
+
codegen,
|
|
252
|
+
parent.meta,
|
|
253
|
+
true
|
|
254
|
+
);
|
|
255
|
+
return `await create.docs("${parent.name}", "${parent.dir}", ${metaGlob}, ${entries.join(", ")})`;
|
|
256
|
+
}
|
|
257
|
+
case "doc":
|
|
258
|
+
return `await create.doc("${collection.name}", "${collection.dir}", ${entries.join(", ")})`;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
await codegen.pushAsync(
|
|
262
|
+
core.getConfig().collectionList.map(async (collection) => {
|
|
263
|
+
const obj = await generateCollectionObject(collection);
|
|
264
|
+
if (!obj) return;
|
|
265
|
+
return `
|
|
266
|
+
export const ${collection.name} = ${obj};`;
|
|
267
|
+
})
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
async function generateBrowserIndexFile({ core, codegen, tc }) {
|
|
271
|
+
codegen.lines.push(
|
|
272
|
+
`import { browser } from 'fumadocs-mdx/runtime/browser';`,
|
|
273
|
+
`import type * as Config from '${codegen.formatImportPath(core.getOptions().configPath)}';`,
|
|
274
|
+
"",
|
|
275
|
+
`const create = browser<typeof Config, ${tc}>();`
|
|
276
|
+
);
|
|
277
|
+
async function generateCollectionObject(collection) {
|
|
278
|
+
switch (collection.type) {
|
|
279
|
+
case "docs": {
|
|
280
|
+
if (collection.docs.dynamic) return;
|
|
281
|
+
return generateCollectionObject(collection.docs);
|
|
282
|
+
}
|
|
283
|
+
case "doc":
|
|
284
|
+
if (collection.dynamic) return;
|
|
285
|
+
return `create.doc("${collection.name}", ${await generateDocCollectionGlob(codegen, collection)})`;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
codegen.lines.push("const browserCollections = {");
|
|
289
|
+
await codegen.pushAsync(
|
|
290
|
+
core.getConfig().collectionList.map(async (collection) => {
|
|
291
|
+
const obj = await generateCollectionObject(collection);
|
|
292
|
+
if (!obj) return;
|
|
293
|
+
return ident(`${collection.name}: ${obj},`);
|
|
294
|
+
})
|
|
295
|
+
);
|
|
296
|
+
codegen.lines.push("};", "export default browserCollections;");
|
|
297
|
+
}
|
|
298
|
+
function generateDocCollectionFrontmatterGlob(codegen, collection, eager = false) {
|
|
299
|
+
return codegen.generateGlobImport(collection.patterns, {
|
|
300
|
+
query: {
|
|
301
|
+
collection: collection.name,
|
|
302
|
+
only: "frontmatter"
|
|
303
|
+
},
|
|
304
|
+
import: "frontmatter",
|
|
305
|
+
base: collection.dir,
|
|
306
|
+
eager
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
function generateDocCollectionGlob(codegen, collection, eager = false) {
|
|
310
|
+
return codegen.generateGlobImport(collection.patterns, {
|
|
311
|
+
query: {
|
|
312
|
+
collection: collection.name
|
|
313
|
+
},
|
|
314
|
+
base: collection.dir,
|
|
315
|
+
eager
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
function generateMetaCollectionGlob(codegen, collection, eager = false) {
|
|
319
|
+
return codegen.generateGlobImport(collection.patterns, {
|
|
320
|
+
query: {
|
|
321
|
+
collection: collection.name
|
|
322
|
+
},
|
|
323
|
+
import: "default",
|
|
324
|
+
base: collection.dir,
|
|
325
|
+
eager
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
export {
|
|
330
|
+
indexFile
|
|
331
|
+
};
|