MDX: Include frontmatter into page.data.content by default

This commit is contained in:
Fuma Nama
2025-08-19 22:15:07 +08:00
parent f300315828
commit f3ffc14bce
8 changed files with 28 additions and 13 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-mdx': patch
---
Include frontmatter into `page.data.content` by default
@@ -17,10 +17,9 @@ export async function getLLMText(page: InferPageType<typeof source>) {
value: page.data.content,
});
// note: it doesn't escape frontmatter, it's up to you.
return `# ${page.data.title}
URL: ${page.url}
${page.data.description}
${processed.value}`;
}
+1 -1
View File
@@ -132,7 +132,7 @@ export async function generateJS(
info: file,
lastModified,
data: data as Record<string, unknown>,
content: parsed.content,
content: { body: parsed.content, matter: parsed.matter },
} satisfies AsyncRuntimeFile);
});
+4 -2
View File
@@ -26,10 +26,12 @@ export const _runtimeAsync: RuntimeAsync = {
return {
...data,
_file: file,
content,
get content() {
return `${content.matter}${content.body}`;
},
async load() {
const mdxOptions = await initMdxOptions;
const out = await buildMDX(collection, content, {
const out = await buildMDX(collection, content.body, {
...mdxOptions,
development: false,
frontmatter: data as Record<string, unknown>,
+1 -1
View File
@@ -39,7 +39,7 @@ export interface RuntimeFile {
export interface AsyncRuntimeFile {
info: FileInfo;
data: Record<string, unknown>;
content: string;
content: { matter: string; body: string };
lastModified?: Date;
}
+5 -2
View File
@@ -4,6 +4,9 @@
import { load } from 'js-yaml';
interface Output {
/**
* The matter section, including the delimiter.
*/
matter: string;
content: string;
data: unknown;
@@ -22,10 +25,10 @@ export function fumaMatter(input: string): Output {
}
// get the raw front-matter block
output.matter = match[1];
output.matter = match[0];
output.content = input.slice(match[0].length);
const loaded = load(output.matter);
const loaded = load(match[1]);
output.data = loaded ?? {};
return output;
+1 -1
View File
@@ -3,4 +3,4 @@ import { _runtimeAsync, buildConfig } from "fumadocs-mdx/runtime/async"
const _sourceConfig = buildConfig(_source)
import { _runtime } from "fumadocs-mdx"
import * as _source from "./config"
export const docs = _runtimeAsync.doc<typeof _source.docs>([{"info":{"path":"index.mdx","absolutePath":"$cwd/packages/mdx/test/fixtures/index.mdx"},"data":{},"content":"# Hello World\n"}, {"info":{"path":"folder/test.mdx","absolutePath":"$cwd/packages/mdx/test/fixtures/folder/test.mdx"},"data":{},"content":""}], "docs", _sourceConfig)
export const docs = _runtimeAsync.doc<typeof _source.docs>([{"info":{"path":"index.mdx","absolutePath":"$cwd/packages/mdx/test/fixtures/index.mdx"},"data":{},"content":{"body":"# Hello World\n","matter":""}}, {"info":{"path":"folder/test.mdx","absolutePath":"$cwd/packages/mdx/test/fixtures/folder/test.mdx"},"data":{},"content":{"body":"","matter":""}}], "docs", _sourceConfig)
+10 -4
View File
@@ -93,8 +93,11 @@ test('parse frontmatter', () => {
"description": "I love Fumadocs",
"title": "hello world",
},
"matter": "title: hello world
description: I love Fumadocs",
"matter": "---
title: hello world
description: I love Fumadocs
---
",
}
`);
@@ -109,8 +112,11 @@ test('parse frontmatter', () => {
"description": "I love Fumadocs",
"title": "hello world",
},
"matter": "title: hello world
description: I love Fumadocs",
"matter": "---
title: hello world
description: I love Fumadocs
---
",
}
`);