fumadocs-mdx 11.7.1 → 11.7.4

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.
@@ -205,7 +205,7 @@ var import_node_url = require("url");
205
205
  // src/config/build.ts
206
206
  function buildConfig(config) {
207
207
  const collections = /* @__PURE__ */ new Map();
208
- let globalConfig;
208
+ let globalConfig = {};
209
209
  for (const [k, v] of Object.entries(config)) {
210
210
  if (!v) {
211
211
  continue;
@@ -220,34 +220,30 @@ function buildConfig(config) {
220
220
  continue;
221
221
  }
222
222
  }
223
- if (k === "default") {
223
+ if (k === "default" && v) {
224
224
  globalConfig = v;
225
225
  continue;
226
226
  }
227
- return [
228
- `Unknown export "${k}", you can only export collections from source configuration file.`,
229
- null
230
- ];
227
+ throw new Error(
228
+ `Unknown export "${k}", you can only export collections from source configuration file.`
229
+ );
231
230
  }
232
231
  let cachedMdxOptions;
233
- return [
234
- null,
235
- {
236
- global: globalConfig,
237
- collections,
238
- async getDefaultMDXOptions() {
239
- if (cachedMdxOptions) return cachedMdxOptions;
240
- const input = this.global?.mdxOptions;
241
- async function uncached() {
242
- const options = typeof input === "function" ? await input() : input;
243
- const { getDefaultMDXOptions: getDefaultMDXOptions2 } = await Promise.resolve().then(() => (init_mdx_options(), mdx_options_exports));
244
- if (options?.preset === "minimal") return options;
245
- return getDefaultMDXOptions2(options ?? {});
246
- }
247
- return cachedMdxOptions = uncached();
232
+ return {
233
+ global: globalConfig,
234
+ collections,
235
+ async getDefaultMDXOptions() {
236
+ if (cachedMdxOptions) return cachedMdxOptions;
237
+ const input = this.global.mdxOptions;
238
+ async function uncached() {
239
+ const options = typeof input === "function" ? await input() : input;
240
+ const { getDefaultMDXOptions: getDefaultMDXOptions2 } = await Promise.resolve().then(() => (init_mdx_options(), mdx_options_exports));
241
+ if (options?.preset === "minimal") return options;
242
+ return getDefaultMDXOptions2(options ?? {});
248
243
  }
244
+ return cachedMdxOptions = uncached();
249
245
  }
250
- ];
246
+ };
251
247
  }
252
248
 
253
249
  // src/utils/config.ts
@@ -282,12 +278,10 @@ async function loadConfig(configPath, outDir, hash, build = false) {
282
278
  if (build) await compileConfig(configPath, outDir);
283
279
  const url = (0, import_node_url.pathToFileURL)(path.resolve(outDir, "source.config.mjs"));
284
280
  const config = import(`${url.href}?hash=${hash}`).then((loaded) => {
285
- const [err, config2] = buildConfig(
281
+ return buildConfig(
286
282
  // every call to `loadConfig` will cause the previous cache to be ignored
287
283
  loaded
288
284
  );
289
- if (err !== null) throw new Error(err);
290
- return config2;
291
285
  });
292
286
  cache = { config, hash };
293
287
  return await config;
@@ -543,8 +537,7 @@ async function generateJS(configPath, config, importPath, configHash = false) {
543
537
  specifier: "fumadocs-mdx/runtime/async",
544
538
  names: ["_runtimeAsync", "buildConfig"]
545
539
  }),
546
- "const [err, _sourceConfig] = buildConfig(_source)",
547
- "if (!_sourceConfig) throw new Error(err)"
540
+ "const _sourceConfig = buildConfig(_source)"
548
541
  );
549
542
  asyncInit = true;
550
543
  }
@@ -1,20 +1,22 @@
1
1
  import {
2
2
  findConfigFile,
3
3
  getConfigHash,
4
- getGitTimestamp,
5
4
  loadConfig
6
- } from "../chunk-2KBRPMAM.js";
5
+ } from "../chunk-4CGSOZUZ.js";
7
6
  import {
8
7
  getGlobPatterns,
9
8
  getImportCode,
10
9
  isFileSupported,
11
10
  toImportPath
12
11
  } from "../chunk-OWZSTKKX.js";
13
- import "../chunk-GWR7KMRU.js";
12
+ import {
13
+ getGitTimestamp
14
+ } from "../chunk-VUEZTR2H.js";
14
15
  import {
15
16
  ValidationError,
16
17
  validate
17
18
  } from "../chunk-ZOWJF3OH.js";
19
+ import "../chunk-JFNBRKRV.js";
18
20
  import {
19
21
  fumaMatter
20
22
  } from "../chunk-KVWX6THC.js";
@@ -118,8 +120,7 @@ async function generateJS(configPath, config, importPath, configHash = false) {
118
120
  specifier: "fumadocs-mdx/runtime/async",
119
121
  names: ["_runtimeAsync", "buildConfig"]
120
122
  }),
121
- "const [err, _sourceConfig] = buildConfig(_source)",
122
- "if (!_sourceConfig) throw new Error(err)"
123
+ "const _sourceConfig = buildConfig(_source)"
123
124
  );
124
125
  asyncInit = true;
125
126
  }
@@ -195,9 +195,29 @@ function flattenNode(node) {
195
195
  if ("value" in node) return node.value;
196
196
  return "";
197
197
  }
198
+ function parseSpecifier(specifier) {
199
+ const idx = specifier.lastIndexOf("#");
200
+ if (idx === -1) return { file: specifier };
201
+ return {
202
+ file: specifier.slice(0, idx),
203
+ section: specifier.slice(idx + 1)
204
+ };
205
+ }
206
+ function extractSection(root, section) {
207
+ for (const node of root.children) {
208
+ if (node.type === "mdxJsxFlowElement" && node.name === "section" && node.attributes.some(
209
+ (attr) => attr.type === "mdxJsxAttribute" && attr.name === "id" && attr.value === section
210
+ )) {
211
+ return {
212
+ type: "root",
213
+ children: node.children
214
+ };
215
+ }
216
+ }
217
+ }
198
218
  function remarkInclude() {
199
219
  const TagName = "include";
200
- async function update(tree, file, processor, compiler) {
220
+ async function update(tree, directory, processor, compiler) {
201
221
  const queue = [];
202
222
  (0, import_unist_util_visit.visit)(
203
223
  tree,
@@ -217,27 +237,41 @@ function remarkInclude() {
217
237
  }
218
238
  }
219
239
  if (!specifier) return;
240
+ const { file, section } = parseSpecifier(specifier);
220
241
  const targetPath = path.resolve(
221
- "cwd" in params ? process.cwd() : path.dirname(file),
222
- specifier
242
+ "cwd" in params ? process.cwd() : directory,
243
+ file
223
244
  );
224
- const asCode = params.lang || !specifier.endsWith(".md") && !specifier.endsWith(".mdx");
245
+ const asCode = params.lang || !file.endsWith(".md") && !file.endsWith(".mdx");
225
246
  queue.push(
226
247
  fs.readFile(targetPath).then((buffer) => buffer.toString()).then(async (content) => {
227
248
  compiler?.addDependency(targetPath);
228
249
  if (asCode) {
229
- const lang = params.lang ?? path.extname(specifier).slice(1);
250
+ const lang = params.lang ?? path.extname(file).slice(1);
230
251
  Object.assign(node, {
231
252
  type: "code",
232
253
  lang,
233
254
  meta: params.meta,
234
- value: content.toString(),
255
+ value: content,
235
256
  data: {}
236
257
  });
237
258
  return;
238
259
  }
239
- const parsed = processor.parse(fumaMatter(content).content);
240
- await update(parsed, targetPath, processor, compiler);
260
+ let parsed = processor.parse(fumaMatter(content).content);
261
+ if (section) {
262
+ const extracted = extractSection(parsed, section);
263
+ if (!extracted)
264
+ throw new Error(
265
+ `Cannot find section ${section} in ${file}, make sure you have encapsulated the section in a <section id="${section}"> tag`
266
+ );
267
+ parsed = extracted;
268
+ }
269
+ await update(
270
+ parsed,
271
+ path.dirname(targetPath),
272
+ processor,
273
+ compiler
274
+ );
241
275
  Object.assign(
242
276
  parent && parent.type === "paragraph" ? parent : node,
243
277
  parsed
@@ -245,7 +279,8 @@ function remarkInclude() {
245
279
  }).catch((e) => {
246
280
  throw new Error(
247
281
  `failed to read file ${targetPath}
248
- ${e instanceof Error ? e.message : String(e)}`
282
+ ${e instanceof Error ? e.message : String(e)}`,
283
+ { cause: e }
249
284
  );
250
285
  })
251
286
  );
@@ -255,7 +290,7 @@ ${e instanceof Error ? e.message : String(e)}`
255
290
  await Promise.all(queue);
256
291
  }
257
292
  return async (tree, file) => {
258
- await update(tree, file.path, this, file.data._compiler);
293
+ await update(tree, path.dirname(file.path), this, file.data._compiler);
259
294
  };
260
295
  }
261
296
 
@@ -338,7 +373,7 @@ function resolveFiles({ docs, meta }) {
338
373
  // src/config/build.ts
339
374
  function buildConfig(config) {
340
375
  const collections = /* @__PURE__ */ new Map();
341
- let globalConfig;
376
+ let globalConfig = {};
342
377
  for (const [k, v] of Object.entries(config)) {
343
378
  if (!v) {
344
379
  continue;
@@ -353,34 +388,30 @@ function buildConfig(config) {
353
388
  continue;
354
389
  }
355
390
  }
356
- if (k === "default") {
391
+ if (k === "default" && v) {
357
392
  globalConfig = v;
358
393
  continue;
359
394
  }
360
- return [
361
- `Unknown export "${k}", you can only export collections from source configuration file.`,
362
- null
363
- ];
395
+ throw new Error(
396
+ `Unknown export "${k}", you can only export collections from source configuration file.`
397
+ );
364
398
  }
365
399
  let cachedMdxOptions;
366
- return [
367
- null,
368
- {
369
- global: globalConfig,
370
- collections,
371
- async getDefaultMDXOptions() {
372
- if (cachedMdxOptions) return cachedMdxOptions;
373
- const input = this.global?.mdxOptions;
374
- async function uncached() {
375
- const options = typeof input === "function" ? await input() : input;
376
- const { getDefaultMDXOptions: getDefaultMDXOptions2 } = await Promise.resolve().then(() => (init_mdx_options(), mdx_options_exports));
377
- if (options?.preset === "minimal") return options;
378
- return getDefaultMDXOptions2(options ?? {});
379
- }
380
- return cachedMdxOptions = uncached();
400
+ return {
401
+ global: globalConfig,
402
+ collections,
403
+ async getDefaultMDXOptions() {
404
+ if (cachedMdxOptions) return cachedMdxOptions;
405
+ const input = this.global.mdxOptions;
406
+ async function uncached() {
407
+ const options = typeof input === "function" ? await input() : input;
408
+ const { getDefaultMDXOptions: getDefaultMDXOptions2 } = await Promise.resolve().then(() => (init_mdx_options(), mdx_options_exports));
409
+ if (options?.preset === "minimal") return options;
410
+ return getDefaultMDXOptions2(options ?? {});
381
411
  }
412
+ return cachedMdxOptions = uncached();
382
413
  }
383
- ];
414
+ };
384
415
  }
385
416
 
386
417
  // src/runtime/async.ts
@@ -1,7 +1,7 @@
1
- import { L as LoadedConfig, a as RuntimeAsync } from '../types-CnslxmoO.cjs';
1
+ import { L as LoadedConfig, a as RuntimeAsync } from '../types-Lh_-Uuix.cjs';
2
2
  import '@standard-schema/spec';
3
3
  import 'fumadocs-core/source';
4
- import '../define-CCrinVBZ.cjs';
4
+ import '../define-E6TRBwBQ.cjs';
5
5
  import 'zod';
6
6
  import 'fumadocs-core/mdx-plugins';
7
7
  import '@mdx-js/mdx';
@@ -10,7 +10,7 @@ import 'react';
10
10
  import 'mdx/types';
11
11
  import 'fumadocs-core/server';
12
12
 
13
- declare function buildConfig(config: Record<string, unknown>): [err: string, value: null] | [err: null, value: LoadedConfig];
13
+ declare function buildConfig(config: Record<string, unknown>): LoadedConfig;
14
14
 
15
15
  declare const _runtimeAsync: RuntimeAsync;
16
16
 
@@ -1,7 +1,7 @@
1
- import { L as LoadedConfig, a as RuntimeAsync } from '../types-C0bKwtAx.js';
1
+ import { L as LoadedConfig, a as RuntimeAsync } from '../types-DiL328cG.js';
2
2
  import '@standard-schema/spec';
3
3
  import 'fumadocs-core/source';
4
- import '../define-CCrinVBZ.js';
4
+ import '../define-E6TRBwBQ.js';
5
5
  import 'zod';
6
6
  import 'fumadocs-core/mdx-plugins';
7
7
  import '@mdx-js/mdx';
@@ -10,7 +10,7 @@ import 'react';
10
10
  import 'mdx/types';
11
11
  import 'fumadocs-core/server';
12
12
 
13
- declare function buildConfig(config: Record<string, unknown>): [err: string, value: null] | [err: null, value: LoadedConfig];
13
+ declare function buildConfig(config: Record<string, unknown>): LoadedConfig;
14
14
 
15
15
  declare const _runtimeAsync: RuntimeAsync;
16
16
 
@@ -2,12 +2,12 @@ import {
2
2
  _runtime,
3
3
  createMDXSource
4
4
  } from "../chunk-NUDEC6C5.js";
5
- import {
6
- buildConfig
7
- } from "../chunk-GWR7KMRU.js";
8
5
  import {
9
6
  remarkInclude
10
- } from "../chunk-AVMO2SRO.js";
7
+ } from "../chunk-PQCNPAD3.js";
8
+ import {
9
+ buildConfig
10
+ } from "../chunk-JFNBRKRV.js";
11
11
  import "../chunk-KVWX6THC.js";
12
12
 
13
13
  // src/runtime/async.ts
@@ -36,12 +36,13 @@ function fromConfig() {
36
36
  return out;
37
37
  }
38
38
  function mapPageData(entry) {
39
- const { toc, structuredData } = entry;
39
+ const { toc, structuredData, lastModified, frontmatter } = entry;
40
40
  return {
41
- ...entry.frontmatter,
41
+ ...frontmatter,
42
42
  default: entry.default,
43
43
  toc,
44
44
  structuredData,
45
+ lastModified,
45
46
  _exports: entry
46
47
  };
47
48
  }
@@ -2,23 +2,25 @@ import { TableOfContents } from 'fumadocs-core/server';
2
2
  import { FC, ReactNode } from 'react';
3
3
  import { MDXProps } from 'mdx/types';
4
4
  import { StructuredData } from 'fumadocs-core/mdx-plugins';
5
- import { D as DocCollection, M as MetaCollection, a as DocsCollection } from '../define-CCrinVBZ.cjs';
5
+ import { a as DocCollection, M as MetaCollection, b as DocsCollection } from '../define-E6TRBwBQ.cjs';
6
6
  import { StandardSchemaV1 } from '@standard-schema/spec';
7
7
  import { PageData, MetaData, Source } from 'fumadocs-core/source';
8
8
  import 'zod';
9
9
  import '@mdx-js/mdx';
10
10
  import 'unified';
11
11
 
12
- type CompiledMDXFile<Frontmatter> = {
12
+ interface CompiledMDXProperties<Frontmatter> {
13
13
  frontmatter: Frontmatter;
14
- toc: TableOfContents;
15
- default: FC<MDXProps>;
16
14
  structuredData: StructuredData;
17
- } & Record<string, unknown>;
18
- type MDXFileToPageData<Frontmatter> = Frontmatter & {
19
15
  toc: TableOfContents;
20
16
  default: FC<MDXProps>;
21
- structuredData: StructuredData;
17
+ /**
18
+ * Only available when `lastModifiedTime` is enabled on MDX loader
19
+ */
20
+ lastModified?: Date;
21
+ }
22
+ type CompiledMDXFile<Frontmatter> = CompiledMDXProperties<Frontmatter> & Record<string, unknown>;
23
+ type MDXFileToPageData<Frontmatter> = Frontmatter & Omit<CompiledMDXProperties<Frontmatter>, 'frontmatter'> & {
22
24
  _exports: Record<string, unknown>;
23
25
  };
24
26
  type AttachGlobValue<GlobValue, Attach> = GlobValue extends () => Promise<unknown> ? () => Promise<Attach> : Attach;
@@ -2,23 +2,25 @@ import { TableOfContents } from 'fumadocs-core/server';
2
2
  import { FC, ReactNode } from 'react';
3
3
  import { MDXProps } from 'mdx/types';
4
4
  import { StructuredData } from 'fumadocs-core/mdx-plugins';
5
- import { D as DocCollection, M as MetaCollection, a as DocsCollection } from '../define-CCrinVBZ.js';
5
+ import { a as DocCollection, M as MetaCollection, b as DocsCollection } from '../define-E6TRBwBQ.js';
6
6
  import { StandardSchemaV1 } from '@standard-schema/spec';
7
7
  import { PageData, MetaData, Source } from 'fumadocs-core/source';
8
8
  import 'zod';
9
9
  import '@mdx-js/mdx';
10
10
  import 'unified';
11
11
 
12
- type CompiledMDXFile<Frontmatter> = {
12
+ interface CompiledMDXProperties<Frontmatter> {
13
13
  frontmatter: Frontmatter;
14
- toc: TableOfContents;
15
- default: FC<MDXProps>;
16
14
  structuredData: StructuredData;
17
- } & Record<string, unknown>;
18
- type MDXFileToPageData<Frontmatter> = Frontmatter & {
19
15
  toc: TableOfContents;
20
16
  default: FC<MDXProps>;
21
- structuredData: StructuredData;
17
+ /**
18
+ * Only available when `lastModifiedTime` is enabled on MDX loader
19
+ */
20
+ lastModified?: Date;
21
+ }
22
+ type CompiledMDXFile<Frontmatter> = CompiledMDXProperties<Frontmatter> & Record<string, unknown>;
23
+ type MDXFileToPageData<Frontmatter> = Frontmatter & Omit<CompiledMDXProperties<Frontmatter>, 'frontmatter'> & {
22
24
  _exports: Record<string, unknown>;
23
25
  };
24
26
  type AttachGlobValue<GlobValue, Attach> = GlobValue extends () => Promise<unknown> ? () => Promise<Attach> : Attach;
@@ -10,12 +10,13 @@ function fromConfig() {
10
10
  return out;
11
11
  }
12
12
  function mapPageData(entry) {
13
- const { toc, structuredData } = entry;
13
+ const { toc, structuredData, lastModified, frontmatter } = entry;
14
14
  return {
15
- ...entry.frontmatter,
15
+ ...frontmatter,
16
16
  default: entry.default,
17
17
  toc,
18
18
  structuredData,
19
+ lastModified,
19
20
  _exports: entry
20
21
  };
21
22
  }
@@ -1,6 +1,6 @@
1
1
  import { StandardSchemaV1 } from '@standard-schema/spec';
2
2
  import { Source, PageData, MetaData } from 'fumadocs-core/source';
3
- import { D as DocCollection, M as MetaCollection, a as DocsCollection, G as GlobalConfig } from './define-CCrinVBZ.js';
3
+ import { a as DocCollection, M as MetaCollection, b as DocsCollection, G as GlobalConfig } from './define-E6TRBwBQ.js';
4
4
  import { ProcessorOptions } from '@mdx-js/mdx';
5
5
  import { FC } from 'react';
6
6
  import { MDXProps } from 'mdx/types';
@@ -9,7 +9,7 @@ import { TableOfContents } from 'fumadocs-core/server';
9
9
 
10
10
  interface LoadedConfig {
11
11
  collections: Map<string, DocCollection | MetaCollection | DocsCollection>;
12
- global?: GlobalConfig;
12
+ global: GlobalConfig;
13
13
  getDefaultMDXOptions(): Promise<ProcessorOptions>;
14
14
  }
15
15
 
@@ -1,6 +1,6 @@
1
1
  import { StandardSchemaV1 } from '@standard-schema/spec';
2
2
  import { Source, PageData, MetaData } from 'fumadocs-core/source';
3
- import { D as DocCollection, M as MetaCollection, a as DocsCollection, G as GlobalConfig } from './define-CCrinVBZ.cjs';
3
+ import { a as DocCollection, M as MetaCollection, b as DocsCollection, G as GlobalConfig } from './define-E6TRBwBQ.cjs';
4
4
  import { ProcessorOptions } from '@mdx-js/mdx';
5
5
  import { FC } from 'react';
6
6
  import { MDXProps } from 'mdx/types';
@@ -9,7 +9,7 @@ import { TableOfContents } from 'fumadocs-core/server';
9
9
 
10
10
  interface LoadedConfig {
11
11
  collections: Map<string, DocCollection | MetaCollection | DocsCollection>;
12
- global?: GlobalConfig;
12
+ global: GlobalConfig;
13
13
  getDefaultMDXOptions(): Promise<ProcessorOptions>;
14
14
  }
15
15