fumadocs-mdx 11.5.7 → 11.6.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.
- package/dist/{chunk-KGLACICA.js → chunk-2ZOW45YZ.js} +25 -5
- package/dist/{chunk-SLCPEEMF.js → chunk-DRVUBK5B.js} +1 -4
- package/dist/{chunk-R6U7CJLB.js → chunk-HFLDWPJA.js} +1 -1
- package/dist/chunk-MK7EXW7O.js +75 -0
- package/dist/{chunk-IZURUUPO.js → chunk-VFALQK6O.js} +3 -9
- package/dist/config/index.cjs +47 -39
- package/dist/config/index.d.cts +1 -1
- package/dist/config/index.d.ts +1 -1
- package/dist/config/index.js +2 -2
- package/dist/{define-P0QTVn7W.d.cts → define-BaW0PQDJ.d.cts} +2 -2
- package/dist/{define-P0QTVn7W.d.ts → define-BaW0PQDJ.d.ts} +2 -2
- package/dist/index.cjs +3 -9
- package/dist/index.d.cts +3 -4
- package/dist/index.d.ts +3 -4
- package/dist/index.js +1 -1
- package/dist/loader-mdx.cjs +72 -49
- package/dist/loader-mdx.js +5 -5
- package/dist/next/index.cjs +110 -108
- package/dist/next/index.js +89 -102
- package/dist/runtime/async.cjs +54 -56
- package/dist/runtime/async.d.cts +2 -2
- package/dist/runtime/async.d.ts +2 -2
- package/dist/runtime/async.js +9 -9
- package/dist/{types-CJRGJLAg.d.ts → types-BNrQHCj5.d.cts} +10 -10
- package/dist/{types-bWXuqsw9.d.cts → types-DEduCvIT.d.ts} +10 -10
- package/package.json +11 -9
- package/dist/chunk-ITGWT23S.js +0 -68
package/dist/next/index.cjs
CHANGED
|
@@ -110,10 +110,7 @@ function buildConfig(config) {
|
|
|
110
110
|
null,
|
|
111
111
|
{
|
|
112
112
|
global: globalConfig,
|
|
113
|
-
collections
|
|
114
|
-
_runtime: {
|
|
115
|
-
files: /* @__PURE__ */ new Map()
|
|
116
|
-
}
|
|
113
|
+
collections
|
|
117
114
|
}
|
|
118
115
|
];
|
|
119
116
|
}
|
|
@@ -170,11 +167,11 @@ async function getConfigHash(configPath) {
|
|
|
170
167
|
|
|
171
168
|
// src/map/index.ts
|
|
172
169
|
var path3 = __toESM(require("path"), 1);
|
|
173
|
-
var
|
|
170
|
+
var fs3 = __toESM(require("fs/promises"), 1);
|
|
174
171
|
|
|
175
172
|
// src/map/generate.ts
|
|
176
173
|
var path2 = __toESM(require("path"), 1);
|
|
177
|
-
var
|
|
174
|
+
var fs2 = __toESM(require("fs/promises"), 1);
|
|
178
175
|
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
179
176
|
|
|
180
177
|
// src/utils/get-type-from-path.ts
|
|
@@ -189,6 +186,7 @@ function getTypeFromPath(path6) {
|
|
|
189
186
|
|
|
190
187
|
// src/utils/schema.ts
|
|
191
188
|
var import_zod = require("zod");
|
|
189
|
+
var import_picocolors = __toESM(require("picocolors"), 1);
|
|
192
190
|
var metaSchema = import_zod.z.object({
|
|
193
191
|
title: import_zod.z.string().optional(),
|
|
194
192
|
pages: import_zod.z.array(import_zod.z.string()).optional(),
|
|
@@ -205,6 +203,28 @@ var frontmatterSchema = import_zod.z.object({
|
|
|
205
203
|
// Fumadocs OpenAPI generated
|
|
206
204
|
_openapi: import_zod.z.object({}).passthrough().optional()
|
|
207
205
|
});
|
|
206
|
+
var ValidationError = class extends Error {
|
|
207
|
+
constructor(message, issues) {
|
|
208
|
+
super(message);
|
|
209
|
+
this.issues = issues;
|
|
210
|
+
}
|
|
211
|
+
print() {
|
|
212
|
+
console.error(
|
|
213
|
+
[
|
|
214
|
+
`[MDX] ${this.message}:`,
|
|
215
|
+
...this.issues.map(
|
|
216
|
+
(issue) => import_picocolors.default.redBright(
|
|
217
|
+
`- ${import_picocolors.default.bold(issue.path?.join(".") ?? "*")}: ${issue.message}`
|
|
218
|
+
)
|
|
219
|
+
)
|
|
220
|
+
].join("\n")
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
toString() {
|
|
224
|
+
return `${this.message}:
|
|
225
|
+
${this.issues.map((issue) => ` ${issue.path}: ${issue.message}`).join("\n")}`;
|
|
226
|
+
}
|
|
227
|
+
};
|
|
208
228
|
async function validate(schema, data, context, errorMessage) {
|
|
209
229
|
if (typeof schema === "function" && !("~standard" in schema)) {
|
|
210
230
|
schema = schema(context);
|
|
@@ -214,19 +234,18 @@ async function validate(schema, data, context, errorMessage) {
|
|
|
214
234
|
data
|
|
215
235
|
);
|
|
216
236
|
if (result.issues) {
|
|
217
|
-
throw new
|
|
237
|
+
throw new ValidationError(errorMessage, result.issues);
|
|
218
238
|
}
|
|
219
239
|
return result.value;
|
|
220
240
|
}
|
|
221
241
|
return data;
|
|
222
242
|
}
|
|
223
|
-
function formatError(message, issues) {
|
|
224
|
-
return `${message}:
|
|
225
|
-
${issues.map((issue) => ` ${issue.path}: ${issue.message}`).join("\n")}`;
|
|
226
|
-
}
|
|
227
243
|
|
|
228
244
|
// src/map/file-cache.ts
|
|
229
|
-
var
|
|
245
|
+
var import_lru_cache = require("lru-cache");
|
|
246
|
+
var map = new import_lru_cache.LRUCache({
|
|
247
|
+
max: 200
|
|
248
|
+
});
|
|
230
249
|
var fileCache = {
|
|
231
250
|
read(namespace, path6) {
|
|
232
251
|
return map.get(`${namespace}.${path6}`);
|
|
@@ -242,46 +261,12 @@ var fileCache = {
|
|
|
242
261
|
}
|
|
243
262
|
};
|
|
244
263
|
|
|
245
|
-
// src/utils/read-frontmatter.ts
|
|
246
|
-
var fs2 = __toESM(require("fs"), 1);
|
|
247
|
-
var import_gray_matter = __toESM(require("gray-matter"), 1);
|
|
248
|
-
async function readFrontmatter(file) {
|
|
249
|
-
const readStream = fs2.createReadStream(file, {
|
|
250
|
-
highWaterMark: 250
|
|
251
|
-
});
|
|
252
|
-
return new Promise((res, rej) => {
|
|
253
|
-
let idx = 0;
|
|
254
|
-
let str = "";
|
|
255
|
-
readStream.on("data", (_chunk) => {
|
|
256
|
-
const chunk = _chunk.toString();
|
|
257
|
-
if (idx === 0 && !chunk.startsWith("---")) {
|
|
258
|
-
res({});
|
|
259
|
-
readStream.close();
|
|
260
|
-
return;
|
|
261
|
-
}
|
|
262
|
-
str += chunk;
|
|
263
|
-
idx++;
|
|
264
|
-
if (str.includes("\n---")) {
|
|
265
|
-
res(
|
|
266
|
-
(0, import_gray_matter.default)({
|
|
267
|
-
content: str
|
|
268
|
-
}).data
|
|
269
|
-
);
|
|
270
|
-
readStream.close();
|
|
271
|
-
}
|
|
272
|
-
});
|
|
273
|
-
readStream.on("end", () => res({}));
|
|
274
|
-
readStream.on("error", (e) => rej(e));
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
|
|
278
264
|
// src/map/generate.ts
|
|
279
|
-
|
|
280
|
-
|
|
265
|
+
var import_gray_matter = __toESM(require("gray-matter"), 1);
|
|
266
|
+
async function readFileWithCache(file) {
|
|
267
|
+
const cached = fileCache.read("read-file", file);
|
|
281
268
|
if (cached) return cached;
|
|
282
|
-
|
|
283
|
-
fileCache.write("read-frontmatter", file, res);
|
|
284
|
-
return res;
|
|
269
|
+
return (await fs2.readFile(file)).toString();
|
|
285
270
|
}
|
|
286
271
|
async function generateJS(configPath, config, outputPath, configHash) {
|
|
287
272
|
const outDir2 = path2.dirname(outputPath);
|
|
@@ -298,31 +283,9 @@ async function generateJS(configPath, config, outputPath, configHash) {
|
|
|
298
283
|
name: "_source"
|
|
299
284
|
})
|
|
300
285
|
];
|
|
301
|
-
config._runtime.files.clear();
|
|
302
286
|
const entries = Array.from(config.collections.entries());
|
|
303
|
-
async function
|
|
287
|
+
async function getDocEntries(collectionName, files) {
|
|
304
288
|
const items = files.map(async (file, i) => {
|
|
305
|
-
config._runtime.files.set(file.absolutePath, collectionName);
|
|
306
|
-
if (collection.type === "meta") {
|
|
307
|
-
const cached = fileCache.read("generate-js", file.absolutePath);
|
|
308
|
-
if (cached) return cached;
|
|
309
|
-
const source = (await fs3.readFile(file.absolutePath)).toString();
|
|
310
|
-
let data = JSON.parse(source);
|
|
311
|
-
if (collection?.schema) {
|
|
312
|
-
data = await validate(
|
|
313
|
-
collection.schema,
|
|
314
|
-
data,
|
|
315
|
-
{
|
|
316
|
-
source,
|
|
317
|
-
path: file.absolutePath
|
|
318
|
-
},
|
|
319
|
-
`invalid data in ${file.absolutePath}:`
|
|
320
|
-
);
|
|
321
|
-
}
|
|
322
|
-
const entry = `{ info: ${JSON.stringify(file)}, data: ${JSON.stringify(data)} }`;
|
|
323
|
-
fileCache.write("generate-js", file.absolutePath, entry);
|
|
324
|
-
return entry;
|
|
325
|
-
}
|
|
326
289
|
const importId = `${collectionName}_${i}`;
|
|
327
290
|
lines.unshift(
|
|
328
291
|
getImportCode({
|
|
@@ -335,7 +298,29 @@ async function generateJS(configPath, config, outputPath, configHash) {
|
|
|
335
298
|
});
|
|
336
299
|
return Promise.all(items);
|
|
337
300
|
}
|
|
338
|
-
async function
|
|
301
|
+
async function getMetaEntries(collection, files) {
|
|
302
|
+
const items = files.map(async (file) => {
|
|
303
|
+
const source = await readFileWithCache(file.absolutePath).catch(() => "");
|
|
304
|
+
let data = source.length === 0 ? {} : JSON.parse(source);
|
|
305
|
+
if (collection?.schema) {
|
|
306
|
+
data = await validate(
|
|
307
|
+
collection.schema,
|
|
308
|
+
data,
|
|
309
|
+
{
|
|
310
|
+
source,
|
|
311
|
+
path: file.absolutePath
|
|
312
|
+
},
|
|
313
|
+
`invalid data in ${file.absolutePath}`
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
return JSON.stringify({
|
|
317
|
+
info: file,
|
|
318
|
+
data
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
return Promise.all(items);
|
|
322
|
+
}
|
|
323
|
+
async function getAsyncEntries(collection, files) {
|
|
339
324
|
if (!asyncInit) {
|
|
340
325
|
lines.unshift(
|
|
341
326
|
getImportCode({
|
|
@@ -349,9 +334,21 @@ async function generateJS(configPath, config, outputPath, configHash) {
|
|
|
349
334
|
asyncInit = true;
|
|
350
335
|
}
|
|
351
336
|
const entries2 = files.map(async (file) => {
|
|
337
|
+
const parsed = (0, import_gray_matter.default)(
|
|
338
|
+
await readFileWithCache(file.absolutePath).catch(() => "")
|
|
339
|
+
);
|
|
340
|
+
if (collection.schema) {
|
|
341
|
+
parsed.data = await validate(
|
|
342
|
+
collection.schema,
|
|
343
|
+
parsed.data,
|
|
344
|
+
{ path: file.absolutePath, source: parsed.content },
|
|
345
|
+
`invalid frontmatter in ${file.absolutePath}`
|
|
346
|
+
);
|
|
347
|
+
}
|
|
352
348
|
return JSON.stringify({
|
|
353
349
|
info: file,
|
|
354
|
-
data:
|
|
350
|
+
data: parsed.data,
|
|
351
|
+
content: parsed.content
|
|
355
352
|
});
|
|
356
353
|
});
|
|
357
354
|
return Promise.all(entries2);
|
|
@@ -360,23 +357,23 @@ async function generateJS(configPath, config, outputPath, configHash) {
|
|
|
360
357
|
if (collection.type === "docs") {
|
|
361
358
|
const docs = await getCollectionFiles(collection.docs);
|
|
362
359
|
const metas = await getCollectionFiles(collection.meta);
|
|
363
|
-
const metaEntries = (await
|
|
360
|
+
const metaEntries = (await getMetaEntries(collection.meta, metas)).join(
|
|
364
361
|
", "
|
|
365
362
|
);
|
|
366
363
|
if (collection.docs.async) {
|
|
367
|
-
const docsEntries2 = (await getAsyncEntries(docs)).join(
|
|
364
|
+
const docsEntries2 = (await getAsyncEntries(collection.docs, docs)).join(
|
|
365
|
+
", "
|
|
366
|
+
);
|
|
368
367
|
return `export const ${k} = _runtimeAsync.docs<typeof _source.${k}>([${docsEntries2}], [${metaEntries}], "${k}", _sourceConfig)`;
|
|
369
368
|
}
|
|
370
|
-
const docsEntries = (await
|
|
371
|
-
", "
|
|
372
|
-
);
|
|
369
|
+
const docsEntries = (await getDocEntries(k, docs)).join(", ");
|
|
373
370
|
return `export const ${k} = _runtime.docs<typeof _source.${k}>([${docsEntries}], [${metaEntries}])`;
|
|
374
371
|
}
|
|
375
372
|
const files = await getCollectionFiles(collection);
|
|
376
373
|
if (collection.type === "doc" && collection.async) {
|
|
377
|
-
return `export const ${k} = _runtimeAsync.doc<typeof _source.${k}>([${(await getAsyncEntries(files)).join(", ")}], "${k}", _sourceConfig)`;
|
|
374
|
+
return `export const ${k} = _runtimeAsync.doc<typeof _source.${k}>([${(await getAsyncEntries(collection, files)).join(", ")}], "${k}", _sourceConfig)`;
|
|
378
375
|
}
|
|
379
|
-
return `export const ${k} = _runtime.${collection.type}<typeof _source.${k}>([${(await
|
|
376
|
+
return `export const ${k} = _runtime.${collection.type}<typeof _source.${k}>([${(await getDocEntries(k, files)).join(", ")}]);`;
|
|
380
377
|
});
|
|
381
378
|
const resolvedDeclares = await Promise.all(declares);
|
|
382
379
|
return [
|
|
@@ -432,18 +429,26 @@ function toImportPath(file, dir) {
|
|
|
432
429
|
|
|
433
430
|
// src/map/index.ts
|
|
434
431
|
async function start(dev, configPath, outDir2) {
|
|
435
|
-
void
|
|
436
|
-
void
|
|
437
|
-
await
|
|
432
|
+
void fs3.rm(path3.resolve(outDir2, `index.js`), { force: true });
|
|
433
|
+
void fs3.rm(path3.resolve(outDir2, `index.d.ts`), { force: true });
|
|
434
|
+
await fs3.mkdir(outDir2, { recursive: true });
|
|
438
435
|
let configHash = await getConfigHash(configPath);
|
|
439
436
|
let config = await loadConfig(configPath, configHash, true);
|
|
440
437
|
const outPath = path3.resolve(outDir2, `index.ts`);
|
|
441
438
|
async function updateMapFile() {
|
|
442
439
|
console.time(`[MDX] update map file`);
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
440
|
+
try {
|
|
441
|
+
await fs3.writeFile(
|
|
442
|
+
outPath,
|
|
443
|
+
await generateJS(configPath, config, outPath, configHash)
|
|
444
|
+
);
|
|
445
|
+
} catch (err) {
|
|
446
|
+
if (err instanceof ValidationError) {
|
|
447
|
+
err.print();
|
|
448
|
+
} else {
|
|
449
|
+
console.error(err);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
447
452
|
console.timeEnd(`[MDX] update map file`);
|
|
448
453
|
}
|
|
449
454
|
await updateMapFile();
|
|
@@ -494,22 +499,19 @@ function createMDX({
|
|
|
494
499
|
};
|
|
495
500
|
return {
|
|
496
501
|
...nextConfig,
|
|
497
|
-
|
|
498
|
-
...nextConfig
|
|
499
|
-
|
|
500
|
-
...nextConfig
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
],
|
|
511
|
-
as: "*.js"
|
|
512
|
-
}
|
|
502
|
+
turbopack: {
|
|
503
|
+
...nextConfig?.turbopack,
|
|
504
|
+
rules: {
|
|
505
|
+
...nextConfig?.turbopack?.rules,
|
|
506
|
+
// @ts-expect-error -- safe
|
|
507
|
+
"*.{md,mdx}": {
|
|
508
|
+
loaders: [
|
|
509
|
+
{
|
|
510
|
+
loader: "fumadocs-mdx/loader-mdx",
|
|
511
|
+
options: mdxLoaderOptions
|
|
512
|
+
}
|
|
513
|
+
],
|
|
514
|
+
as: "*.js"
|
|
513
515
|
}
|
|
514
516
|
}
|
|
515
517
|
},
|
|
@@ -537,13 +539,13 @@ function createMDX({
|
|
|
537
539
|
|
|
538
540
|
// src/postinstall.ts
|
|
539
541
|
var path5 = __toESM(require("path"), 1);
|
|
540
|
-
var
|
|
542
|
+
var fs4 = __toESM(require("fs"), 1);
|
|
541
543
|
async function postInstall(configPath = findConfigFile()) {
|
|
542
544
|
const jsOut = path5.resolve(".source/index.ts");
|
|
543
545
|
const hash = await getConfigHash(configPath);
|
|
544
546
|
const config = await loadConfig(configPath, hash, true);
|
|
545
|
-
|
|
546
|
-
|
|
547
|
+
fs4.mkdirSync(path5.dirname(jsOut), { recursive: true });
|
|
548
|
+
fs4.writeFileSync(
|
|
547
549
|
jsOut,
|
|
548
550
|
await generateJS(
|
|
549
551
|
configPath,
|
package/dist/next/index.js
CHANGED
|
@@ -2,22 +2,23 @@ import {
|
|
|
2
2
|
findConfigFile,
|
|
3
3
|
getConfigHash,
|
|
4
4
|
loadConfig
|
|
5
|
-
} from "../chunk-
|
|
5
|
+
} from "../chunk-HFLDWPJA.js";
|
|
6
|
+
import "../chunk-DRVUBK5B.js";
|
|
6
7
|
import {
|
|
8
|
+
ValidationError,
|
|
7
9
|
validate
|
|
8
|
-
} from "../chunk-
|
|
9
|
-
import "../chunk-SLCPEEMF.js";
|
|
10
|
+
} from "../chunk-2ZOW45YZ.js";
|
|
10
11
|
|
|
11
12
|
// src/next/create.ts
|
|
12
13
|
import path3 from "node:path";
|
|
13
14
|
|
|
14
15
|
// src/map/index.ts
|
|
15
16
|
import * as path2 from "node:path";
|
|
16
|
-
import * as
|
|
17
|
+
import * as fs2 from "node:fs/promises";
|
|
17
18
|
|
|
18
19
|
// src/map/generate.ts
|
|
19
20
|
import * as path from "node:path";
|
|
20
|
-
import * as
|
|
21
|
+
import * as fs from "node:fs/promises";
|
|
21
22
|
import fg from "fast-glob";
|
|
22
23
|
|
|
23
24
|
// src/utils/get-type-from-path.ts
|
|
@@ -31,7 +32,10 @@ function getTypeFromPath(path5) {
|
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
// src/map/file-cache.ts
|
|
34
|
-
|
|
35
|
+
import { LRUCache } from "lru-cache";
|
|
36
|
+
var map = new LRUCache({
|
|
37
|
+
max: 200
|
|
38
|
+
});
|
|
35
39
|
var fileCache = {
|
|
36
40
|
read(namespace, path5) {
|
|
37
41
|
return map.get(`${namespace}.${path5}`);
|
|
@@ -47,46 +51,12 @@ var fileCache = {
|
|
|
47
51
|
}
|
|
48
52
|
};
|
|
49
53
|
|
|
50
|
-
// src/utils/read-frontmatter.ts
|
|
51
|
-
import * as fs from "node:fs";
|
|
52
|
-
import grayMatter from "gray-matter";
|
|
53
|
-
async function readFrontmatter(file) {
|
|
54
|
-
const readStream = fs.createReadStream(file, {
|
|
55
|
-
highWaterMark: 250
|
|
56
|
-
});
|
|
57
|
-
return new Promise((res, rej) => {
|
|
58
|
-
let idx = 0;
|
|
59
|
-
let str = "";
|
|
60
|
-
readStream.on("data", (_chunk) => {
|
|
61
|
-
const chunk = _chunk.toString();
|
|
62
|
-
if (idx === 0 && !chunk.startsWith("---")) {
|
|
63
|
-
res({});
|
|
64
|
-
readStream.close();
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
str += chunk;
|
|
68
|
-
idx++;
|
|
69
|
-
if (str.includes("\n---")) {
|
|
70
|
-
res(
|
|
71
|
-
grayMatter({
|
|
72
|
-
content: str
|
|
73
|
-
}).data
|
|
74
|
-
);
|
|
75
|
-
readStream.close();
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
readStream.on("end", () => res({}));
|
|
79
|
-
readStream.on("error", (e) => rej(e));
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
|
|
83
54
|
// src/map/generate.ts
|
|
84
|
-
|
|
85
|
-
|
|
55
|
+
import matter from "gray-matter";
|
|
56
|
+
async function readFileWithCache(file) {
|
|
57
|
+
const cached = fileCache.read("read-file", file);
|
|
86
58
|
if (cached) return cached;
|
|
87
|
-
|
|
88
|
-
fileCache.write("read-frontmatter", file, res);
|
|
89
|
-
return res;
|
|
59
|
+
return (await fs.readFile(file)).toString();
|
|
90
60
|
}
|
|
91
61
|
async function generateJS(configPath, config, outputPath, configHash) {
|
|
92
62
|
const outDir2 = path.dirname(outputPath);
|
|
@@ -103,31 +73,9 @@ async function generateJS(configPath, config, outputPath, configHash) {
|
|
|
103
73
|
name: "_source"
|
|
104
74
|
})
|
|
105
75
|
];
|
|
106
|
-
config._runtime.files.clear();
|
|
107
76
|
const entries = Array.from(config.collections.entries());
|
|
108
|
-
async function
|
|
77
|
+
async function getDocEntries(collectionName, files) {
|
|
109
78
|
const items = files.map(async (file, i) => {
|
|
110
|
-
config._runtime.files.set(file.absolutePath, collectionName);
|
|
111
|
-
if (collection.type === "meta") {
|
|
112
|
-
const cached = fileCache.read("generate-js", file.absolutePath);
|
|
113
|
-
if (cached) return cached;
|
|
114
|
-
const source = (await fs2.readFile(file.absolutePath)).toString();
|
|
115
|
-
let data = JSON.parse(source);
|
|
116
|
-
if (collection?.schema) {
|
|
117
|
-
data = await validate(
|
|
118
|
-
collection.schema,
|
|
119
|
-
data,
|
|
120
|
-
{
|
|
121
|
-
source,
|
|
122
|
-
path: file.absolutePath
|
|
123
|
-
},
|
|
124
|
-
`invalid data in ${file.absolutePath}:`
|
|
125
|
-
);
|
|
126
|
-
}
|
|
127
|
-
const entry = `{ info: ${JSON.stringify(file)}, data: ${JSON.stringify(data)} }`;
|
|
128
|
-
fileCache.write("generate-js", file.absolutePath, entry);
|
|
129
|
-
return entry;
|
|
130
|
-
}
|
|
131
79
|
const importId = `${collectionName}_${i}`;
|
|
132
80
|
lines.unshift(
|
|
133
81
|
getImportCode({
|
|
@@ -140,7 +88,29 @@ async function generateJS(configPath, config, outputPath, configHash) {
|
|
|
140
88
|
});
|
|
141
89
|
return Promise.all(items);
|
|
142
90
|
}
|
|
143
|
-
async function
|
|
91
|
+
async function getMetaEntries(collection, files) {
|
|
92
|
+
const items = files.map(async (file) => {
|
|
93
|
+
const source = await readFileWithCache(file.absolutePath).catch(() => "");
|
|
94
|
+
let data = source.length === 0 ? {} : JSON.parse(source);
|
|
95
|
+
if (collection?.schema) {
|
|
96
|
+
data = await validate(
|
|
97
|
+
collection.schema,
|
|
98
|
+
data,
|
|
99
|
+
{
|
|
100
|
+
source,
|
|
101
|
+
path: file.absolutePath
|
|
102
|
+
},
|
|
103
|
+
`invalid data in ${file.absolutePath}`
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
return JSON.stringify({
|
|
107
|
+
info: file,
|
|
108
|
+
data
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
return Promise.all(items);
|
|
112
|
+
}
|
|
113
|
+
async function getAsyncEntries(collection, files) {
|
|
144
114
|
if (!asyncInit) {
|
|
145
115
|
lines.unshift(
|
|
146
116
|
getImportCode({
|
|
@@ -154,9 +124,21 @@ async function generateJS(configPath, config, outputPath, configHash) {
|
|
|
154
124
|
asyncInit = true;
|
|
155
125
|
}
|
|
156
126
|
const entries2 = files.map(async (file) => {
|
|
127
|
+
const parsed = matter(
|
|
128
|
+
await readFileWithCache(file.absolutePath).catch(() => "")
|
|
129
|
+
);
|
|
130
|
+
if (collection.schema) {
|
|
131
|
+
parsed.data = await validate(
|
|
132
|
+
collection.schema,
|
|
133
|
+
parsed.data,
|
|
134
|
+
{ path: file.absolutePath, source: parsed.content },
|
|
135
|
+
`invalid frontmatter in ${file.absolutePath}`
|
|
136
|
+
);
|
|
137
|
+
}
|
|
157
138
|
return JSON.stringify({
|
|
158
139
|
info: file,
|
|
159
|
-
data:
|
|
140
|
+
data: parsed.data,
|
|
141
|
+
content: parsed.content
|
|
160
142
|
});
|
|
161
143
|
});
|
|
162
144
|
return Promise.all(entries2);
|
|
@@ -165,23 +147,23 @@ async function generateJS(configPath, config, outputPath, configHash) {
|
|
|
165
147
|
if (collection.type === "docs") {
|
|
166
148
|
const docs = await getCollectionFiles(collection.docs);
|
|
167
149
|
const metas = await getCollectionFiles(collection.meta);
|
|
168
|
-
const metaEntries = (await
|
|
150
|
+
const metaEntries = (await getMetaEntries(collection.meta, metas)).join(
|
|
169
151
|
", "
|
|
170
152
|
);
|
|
171
153
|
if (collection.docs.async) {
|
|
172
|
-
const docsEntries2 = (await getAsyncEntries(docs)).join(
|
|
154
|
+
const docsEntries2 = (await getAsyncEntries(collection.docs, docs)).join(
|
|
155
|
+
", "
|
|
156
|
+
);
|
|
173
157
|
return `export const ${k} = _runtimeAsync.docs<typeof _source.${k}>([${docsEntries2}], [${metaEntries}], "${k}", _sourceConfig)`;
|
|
174
158
|
}
|
|
175
|
-
const docsEntries = (await
|
|
176
|
-
", "
|
|
177
|
-
);
|
|
159
|
+
const docsEntries = (await getDocEntries(k, docs)).join(", ");
|
|
178
160
|
return `export const ${k} = _runtime.docs<typeof _source.${k}>([${docsEntries}], [${metaEntries}])`;
|
|
179
161
|
}
|
|
180
162
|
const files = await getCollectionFiles(collection);
|
|
181
163
|
if (collection.type === "doc" && collection.async) {
|
|
182
|
-
return `export const ${k} = _runtimeAsync.doc<typeof _source.${k}>([${(await getAsyncEntries(files)).join(", ")}], "${k}", _sourceConfig)`;
|
|
164
|
+
return `export const ${k} = _runtimeAsync.doc<typeof _source.${k}>([${(await getAsyncEntries(collection, files)).join(", ")}], "${k}", _sourceConfig)`;
|
|
183
165
|
}
|
|
184
|
-
return `export const ${k} = _runtime.${collection.type}<typeof _source.${k}>([${(await
|
|
166
|
+
return `export const ${k} = _runtime.${collection.type}<typeof _source.${k}>([${(await getDocEntries(k, files)).join(", ")}]);`;
|
|
185
167
|
});
|
|
186
168
|
const resolvedDeclares = await Promise.all(declares);
|
|
187
169
|
return [
|
|
@@ -237,18 +219,26 @@ function toImportPath(file, dir) {
|
|
|
237
219
|
|
|
238
220
|
// src/map/index.ts
|
|
239
221
|
async function start(dev, configPath, outDir2) {
|
|
240
|
-
void
|
|
241
|
-
void
|
|
242
|
-
await
|
|
222
|
+
void fs2.rm(path2.resolve(outDir2, `index.js`), { force: true });
|
|
223
|
+
void fs2.rm(path2.resolve(outDir2, `index.d.ts`), { force: true });
|
|
224
|
+
await fs2.mkdir(outDir2, { recursive: true });
|
|
243
225
|
let configHash = await getConfigHash(configPath);
|
|
244
226
|
let config = await loadConfig(configPath, configHash, true);
|
|
245
227
|
const outPath = path2.resolve(outDir2, `index.ts`);
|
|
246
228
|
async function updateMapFile() {
|
|
247
229
|
console.time(`[MDX] update map file`);
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
230
|
+
try {
|
|
231
|
+
await fs2.writeFile(
|
|
232
|
+
outPath,
|
|
233
|
+
await generateJS(configPath, config, outPath, configHash)
|
|
234
|
+
);
|
|
235
|
+
} catch (err) {
|
|
236
|
+
if (err instanceof ValidationError) {
|
|
237
|
+
err.print();
|
|
238
|
+
} else {
|
|
239
|
+
console.error(err);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
252
242
|
console.timeEnd(`[MDX] update map file`);
|
|
253
243
|
}
|
|
254
244
|
await updateMapFile();
|
|
@@ -299,22 +289,19 @@ function createMDX({
|
|
|
299
289
|
};
|
|
300
290
|
return {
|
|
301
291
|
...nextConfig,
|
|
302
|
-
|
|
303
|
-
...nextConfig
|
|
304
|
-
|
|
305
|
-
...nextConfig
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
],
|
|
316
|
-
as: "*.js"
|
|
317
|
-
}
|
|
292
|
+
turbopack: {
|
|
293
|
+
...nextConfig?.turbopack,
|
|
294
|
+
rules: {
|
|
295
|
+
...nextConfig?.turbopack?.rules,
|
|
296
|
+
// @ts-expect-error -- safe
|
|
297
|
+
"*.{md,mdx}": {
|
|
298
|
+
loaders: [
|
|
299
|
+
{
|
|
300
|
+
loader: "fumadocs-mdx/loader-mdx",
|
|
301
|
+
options: mdxLoaderOptions
|
|
302
|
+
}
|
|
303
|
+
],
|
|
304
|
+
as: "*.js"
|
|
318
305
|
}
|
|
319
306
|
}
|
|
320
307
|
},
|
|
@@ -342,13 +329,13 @@ function createMDX({
|
|
|
342
329
|
|
|
343
330
|
// src/postinstall.ts
|
|
344
331
|
import * as path4 from "node:path";
|
|
345
|
-
import * as
|
|
332
|
+
import * as fs3 from "node:fs";
|
|
346
333
|
async function postInstall(configPath = findConfigFile()) {
|
|
347
334
|
const jsOut = path4.resolve(".source/index.ts");
|
|
348
335
|
const hash = await getConfigHash(configPath);
|
|
349
336
|
const config = await loadConfig(configPath, hash, true);
|
|
350
|
-
|
|
351
|
-
|
|
337
|
+
fs3.mkdirSync(path4.dirname(jsOut), { recursive: true });
|
|
338
|
+
fs3.writeFileSync(
|
|
352
339
|
jsOut,
|
|
353
340
|
await generateJS(
|
|
354
341
|
configPath,
|