fumadocs-mdx 13.0.4 → 13.0.6

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 CHANGED
@@ -1274,84 +1274,88 @@ var init_build_mdx = __esm({
1274
1274
 
1275
1275
  // src/loaders/mdx/index.ts
1276
1276
  function createMdxLoader(configLoader) {
1277
- return async ({
1278
- source: value,
1279
- development: isDevelopment,
1280
- query,
1281
- compiler,
1282
- filePath
1283
- }) => {
1284
- const matter = fumaMatter(value);
1285
- const parsed = querySchema.parse(query);
1286
- const config = await configLoader.getConfig();
1287
- let after;
1288
- if (!isDevelopment && config.global.experimentalBuildCache) {
1289
- const cacheDir = config.global.experimentalBuildCache;
1290
- const cacheKey = `${parsed.hash}_${parsed.collection ?? "global"}_${generateCacheHash(filePath)}`;
1291
- const cached = await import_promises3.default.readFile(import_node_path5.default.join(cacheDir, cacheKey)).then((content) => cacheEntry.parse(JSON.parse(content.toString()))).catch(() => null);
1292
- if (cached && cached.hash === generateCacheHash(value)) return cached;
1293
- after = async () => {
1294
- await import_promises3.default.mkdir(cacheDir, { recursive: true });
1295
- await import_promises3.default.writeFile(
1296
- import_node_path5.default.join(cacheDir, cacheKey),
1297
- JSON.stringify({
1298
- ...out,
1299
- hash: generateCacheHash(value)
1300
- })
1277
+ return {
1278
+ test: mdxLoaderGlob,
1279
+ async load({
1280
+ getSource,
1281
+ development: isDevelopment,
1282
+ query,
1283
+ compiler,
1284
+ filePath
1285
+ }) {
1286
+ const value = await getSource();
1287
+ const matter = fumaMatter(value);
1288
+ const parsed = querySchema.parse(query);
1289
+ const config = await configLoader.getConfig();
1290
+ let after;
1291
+ if (!isDevelopment && config.global.experimentalBuildCache) {
1292
+ const cacheDir = config.global.experimentalBuildCache;
1293
+ const cacheKey = `${parsed.hash}_${parsed.collection ?? "global"}_${generateCacheHash(filePath)}`;
1294
+ const cached = await import_promises3.default.readFile(import_node_path5.default.join(cacheDir, cacheKey)).then((content) => cacheEntry.parse(JSON.parse(content.toString()))).catch(() => null);
1295
+ if (cached && cached.hash === generateCacheHash(value)) return cached;
1296
+ after = async () => {
1297
+ await import_promises3.default.mkdir(cacheDir, { recursive: true });
1298
+ await import_promises3.default.writeFile(
1299
+ import_node_path5.default.join(cacheDir, cacheKey),
1300
+ JSON.stringify({
1301
+ ...out,
1302
+ hash: generateCacheHash(value)
1303
+ })
1304
+ );
1305
+ };
1306
+ }
1307
+ const collection = parsed.collection ? config.getCollection(parsed.collection) : void 0;
1308
+ let docCollection;
1309
+ switch (collection?.type) {
1310
+ case "doc":
1311
+ docCollection = collection;
1312
+ break;
1313
+ case "docs":
1314
+ docCollection = collection.docs;
1315
+ break;
1316
+ }
1317
+ if (docCollection?.schema) {
1318
+ matter.data = await validate(
1319
+ docCollection.schema,
1320
+ matter.data,
1321
+ {
1322
+ source: value,
1323
+ path: filePath
1324
+ },
1325
+ `invalid frontmatter in ${filePath}`
1301
1326
  );
1302
- };
1303
- }
1304
- const collection = parsed.collection ? config.getCollection(parsed.collection) : void 0;
1305
- let docCollection;
1306
- switch (collection?.type) {
1307
- case "doc":
1308
- docCollection = collection;
1309
- break;
1310
- case "docs":
1311
- docCollection = collection.docs;
1312
- break;
1313
- }
1314
- if (docCollection?.schema) {
1315
- matter.data = await validate(
1316
- docCollection.schema,
1317
- matter.data,
1327
+ }
1328
+ if (parsed.only === "frontmatter") {
1329
+ return {
1330
+ code: `export const frontmatter = ${JSON.stringify(matter.data)}`,
1331
+ map: null
1332
+ };
1333
+ }
1334
+ const data = {};
1335
+ if (config.global.lastModifiedTime === "git") {
1336
+ data.lastModified = (await getGitTimestamp(filePath))?.getTime();
1337
+ }
1338
+ const lineOffset = isDevelopment ? countLines(matter.matter) : 0;
1339
+ const compiled = await buildMDX(
1340
+ `${getConfigHash(config)}:${parsed.collection ?? "global"}`,
1341
+ "\n".repeat(lineOffset) + matter.content,
1318
1342
  {
1319
- source: value,
1320
- path: filePath
1321
- },
1322
- `invalid frontmatter in ${filePath}`
1343
+ development: isDevelopment,
1344
+ ...docCollection?.mdxOptions ?? await config.getDefaultMDXOptions(),
1345
+ postprocess: docCollection?.postprocess,
1346
+ data,
1347
+ filePath,
1348
+ frontmatter: matter.data,
1349
+ _compiler: compiler
1350
+ }
1323
1351
  );
1324
- }
1325
- if (parsed.only === "frontmatter") {
1326
- return {
1327
- code: `export const frontmatter = ${JSON.stringify(matter.data)}`,
1328
- map: null
1352
+ const out = {
1353
+ code: String(compiled.value),
1354
+ map: compiled.map
1329
1355
  };
1356
+ await after?.();
1357
+ return out;
1330
1358
  }
1331
- const data = {};
1332
- if (config.global.lastModifiedTime === "git") {
1333
- data.lastModified = (await getGitTimestamp(filePath))?.getTime();
1334
- }
1335
- const lineOffset = isDevelopment ? countLines(matter.matter) : 0;
1336
- const compiled = await buildMDX(
1337
- `${getConfigHash(config)}:${parsed.collection ?? "global"}`,
1338
- "\n".repeat(lineOffset) + matter.content,
1339
- {
1340
- development: isDevelopment,
1341
- ...docCollection?.mdxOptions ?? await config.getDefaultMDXOptions(),
1342
- postprocess: docCollection?.postprocess,
1343
- data,
1344
- filePath,
1345
- frontmatter: matter.data,
1346
- _compiler: compiler
1347
- }
1348
- );
1349
- const out = {
1350
- code: String(compiled.value),
1351
- map: compiled.map
1352
- };
1353
- await after?.();
1354
- return out;
1355
1359
  };
1356
1360
  }
1357
1361
  function getConfigHash(config) {
@@ -1383,6 +1387,7 @@ var init_mdx = __esm({
1383
1387
  import_promises3 = __toESM(require("fs/promises"), 1);
1384
1388
  import_node_path5 = __toESM(require("path"), 1);
1385
1389
  import_node_crypto2 = require("crypto");
1390
+ init_loaders();
1386
1391
  querySchema = import_zod.z.object({
1387
1392
  only: import_zod.z.literal(["frontmatter", "all"]).default("all"),
1388
1393
  collection: import_zod.z.string().optional()
@@ -1398,23 +1403,31 @@ var init_mdx = __esm({
1398
1403
 
1399
1404
  // src/loaders/adapter.ts
1400
1405
  function toVite(loader) {
1401
- return async function(file, query, value) {
1402
- const result = await loader({
1403
- filePath: file,
1404
- query: (0, import_node_querystring.parse)(query),
1405
- source: value,
1406
- development: this.environment.mode === "dev",
1407
- compiler: {
1408
- addDependency: (file2) => {
1409
- this.addWatchFile(file2);
1406
+ return {
1407
+ filter(id) {
1408
+ return !loader.test || loader.test.test(id);
1409
+ },
1410
+ async transform(value, id) {
1411
+ const [file, query = ""] = id.split("?", 2);
1412
+ const result = await loader.load({
1413
+ filePath: file,
1414
+ query: (0, import_node_querystring.parse)(query),
1415
+ getSource() {
1416
+ return value;
1417
+ },
1418
+ development: this.environment.mode === "dev",
1419
+ compiler: {
1420
+ addDependency: (file2) => {
1421
+ this.addWatchFile(file2);
1422
+ }
1410
1423
  }
1411
- }
1412
- });
1413
- if (result === null) return null;
1414
- return {
1415
- code: result.code,
1416
- map: result.map
1417
- };
1424
+ });
1425
+ if (result === null) return null;
1426
+ return {
1427
+ code: result.code,
1428
+ map: result.map
1429
+ };
1430
+ }
1418
1431
  };
1419
1432
  }
1420
1433
  var import_node_url2, import_promises4, import_node_querystring, import_node_path6;
@@ -1652,44 +1665,64 @@ var init_config = __esm({
1652
1665
  // src/loaders/meta.ts
1653
1666
  function createMetaLoader(configLoader, resolve5 = {}) {
1654
1667
  const { json: resolveJson = "js", yaml: resolveYaml = "js" } = resolve5;
1655
- return async ({ filePath, query, source }) => {
1656
- const isJson = filePath.endsWith(".json");
1657
- const parsed = querySchema2.parse(query);
1658
- const collection = parsed.collection ? (await configLoader.getConfig()).getCollection(parsed.collection) : void 0;
1659
- if (!collection) return null;
1660
- let data;
1661
- try {
1662
- data = isJson ? JSON.parse(source) : (0, import_js_yaml3.load)(source);
1663
- } catch (e) {
1664
- throw new Error(`invalid data in ${filePath}`, { cause: e });
1665
- }
1666
- let schema;
1667
- switch (collection?.type) {
1668
- case "meta":
1669
- schema = collection.schema;
1670
- break;
1671
- case "docs":
1672
- schema = collection.meta.schema;
1673
- break;
1674
- }
1675
- if (schema) {
1676
- data = await validate(
1677
- schema,
1678
- data,
1679
- { path: filePath, source },
1680
- `invalid data in ${filePath}`
1681
- );
1682
- }
1683
- let code;
1668
+ function stringifyOutput(isJson, data) {
1684
1669
  if (isJson) {
1685
- code = resolveJson === "json" ? JSON.stringify(data) : `export default ${JSON.stringify(data)}`;
1670
+ return resolveJson === "json" ? JSON.stringify(data) : `export default ${JSON.stringify(data)}`;
1686
1671
  } else {
1687
- code = resolveYaml === "yaml" ? (0, import_js_yaml3.dump)(data) : `export default ${JSON.stringify(data)}`;
1672
+ return resolveYaml === "yaml" ? (0, import_js_yaml3.dump)(data) : `export default ${JSON.stringify(data)}`;
1673
+ }
1674
+ }
1675
+ return {
1676
+ test: metaLoaderGlob,
1677
+ async load({ filePath, query, getSource }) {
1678
+ const parsed = querySchema2.parse(query);
1679
+ const collection = parsed.collection ? (await configLoader.getConfig()).getCollection(parsed.collection) : void 0;
1680
+ if (!collection) return null;
1681
+ const isJson = filePath.endsWith(".json");
1682
+ const source = await getSource();
1683
+ let data;
1684
+ try {
1685
+ data = isJson ? JSON.parse(source) : (0, import_js_yaml3.load)(source);
1686
+ } catch (e) {
1687
+ throw new Error(`invalid data in ${filePath}`, { cause: e });
1688
+ }
1689
+ let schema;
1690
+ switch (collection?.type) {
1691
+ case "meta":
1692
+ schema = collection.schema;
1693
+ break;
1694
+ case "docs":
1695
+ schema = collection.meta.schema;
1696
+ break;
1697
+ }
1698
+ if (schema) {
1699
+ data = await validate(
1700
+ schema,
1701
+ data,
1702
+ { path: filePath, source },
1703
+ `invalid data in ${filePath}`
1704
+ );
1705
+ }
1706
+ return {
1707
+ code: stringifyOutput(isJson, data)
1708
+ };
1709
+ },
1710
+ bun: {
1711
+ async fallback({ getSource, filePath }) {
1712
+ const source = await getSource();
1713
+ const isJson = filePath.endsWith(".json");
1714
+ let data;
1715
+ try {
1716
+ data = isJson ? JSON.parse(source) : (0, import_js_yaml3.load)(source);
1717
+ } catch (e) {
1718
+ throw new Error(`invalid data in ${filePath}`, { cause: e });
1719
+ }
1720
+ return {
1721
+ loader: "object",
1722
+ exports: data
1723
+ };
1724
+ }
1688
1725
  }
1689
- return {
1690
- code,
1691
- map: null
1692
- };
1693
1726
  };
1694
1727
  }
1695
1728
  var import_js_yaml3, import_zod2, querySchema2;
@@ -1699,6 +1732,7 @@ var init_meta = __esm({
1699
1732
  import_js_yaml3 = require("js-yaml");
1700
1733
  init_validation();
1701
1734
  import_zod2 = require("zod");
1735
+ init_loaders();
1702
1736
  querySchema2 = import_zod2.z.object({
1703
1737
  collection: import_zod2.z.string().optional()
1704
1738
  }).loose();
@@ -1721,7 +1755,7 @@ async function mdx(config, pluginOptions = {}) {
1721
1755
  const metaLoader = toVite(
1722
1756
  createMetaLoader(configLoader, {
1723
1757
  // vite has built-in plugin for JSON files
1724
- json: "json"
1758
+ json: "js"
1725
1759
  })
1726
1760
  );
1727
1761
  return {
@@ -1750,13 +1784,11 @@ async function mdx(config, pluginOptions = {}) {
1750
1784
  },
1751
1785
  async transform(value, id) {
1752
1786
  try {
1753
- if (metaLoaderGlob.test(id)) {
1754
- const [file, query = ""] = id.split("?", 2);
1755
- return await metaLoader.call(this, file, query, value);
1787
+ if (metaLoader.filter(id)) {
1788
+ return await metaLoader.transform.call(this, value, id);
1756
1789
  }
1757
- if (mdxLoaderGlob.test(id)) {
1758
- const [file, query = ""] = id.split("?", 2);
1759
- return await mdxLoader.call(this, file, query, value);
1790
+ if (mdxLoader.filter(id)) {
1791
+ return await mdxLoader.transform.call(this, value, id);
1760
1792
  }
1761
1793
  } catch (e) {
1762
1794
  if (e instanceof ValidationError) {
@@ -1814,7 +1846,6 @@ var init_vite2 = __esm({
1814
1846
  init_core();
1815
1847
  init_config();
1816
1848
  init_meta();
1817
- init_loaders();
1818
1849
  FumadocsDeps = ["fumadocs-core", "fumadocs-ui", "fumadocs-openapi"];
1819
1850
  }
1820
1851
  });
@@ -1823,7 +1854,7 @@ var init_vite2 = __esm({
1823
1854
  var import_node_fs = require("fs");
1824
1855
  async function start() {
1825
1856
  const [configPath] = process.argv.slice(2);
1826
- const isNext = (0, import_node_fs.existsSync)("next.config.js") || (0, import_node_fs.existsSync)("next.config.mjs") || (0, import_node_fs.existsSync)("next.config.ts");
1857
+ const isNext = (0, import_node_fs.existsSync)("next.config.js") || (0, import_node_fs.existsSync)("next.config.mjs") || (0, import_node_fs.existsSync)("next.config.mts") || (0, import_node_fs.existsSync)("next.config.ts");
1827
1858
  if (isNext) {
1828
1859
  const { postInstall: postInstall3 } = await Promise.resolve().then(() => (init_next2(), next_exports));
1829
1860
  await postInstall3(configPath);
package/dist/bin.js CHANGED
@@ -4,7 +4,7 @@
4
4
  import { existsSync } from "fs";
5
5
  async function start() {
6
6
  const [configPath] = process.argv.slice(2);
7
- const isNext = existsSync("next.config.js") || existsSync("next.config.mjs") || existsSync("next.config.ts");
7
+ const isNext = existsSync("next.config.js") || existsSync("next.config.mjs") || existsSync("next.config.mts") || existsSync("next.config.ts");
8
8
  if (isNext) {
9
9
  const { postInstall } = await import("./next/index.js");
10
10
  await postInstall(configPath);