Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c8f49d8

Browse files
committed
MDX: Include frontmatter into page.data.content by default
1 parent d21577a commit c8f49d8

File tree

8 files changed

+28
-13
lines changed

8 files changed

+28
-13
lines changed

.changeset/dark-shirts-relate.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'fumadocs-mdx': patch
3+
---
4+
5+
Include frontmatter into `page.data.content` by default

apps/docs/content/docs/ui/(integrations)/get-llm-text.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ export async function getLLMText(page: InferPageType<typeof source>) {
1717
value: page.data.content,
1818
});
1919

20+
// note: it doesn't escape frontmatter, it's up to you.
2021
return `# ${page.data.title}
2122
URL: ${page.url}
2223
23-
${page.data.description}
24-
2524
${processed.value}`;
2625
}

packages/mdx/src/map/generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export async function generateJS(
132132
info: file,
133133
lastModified,
134134
data: data as Record<string, unknown>,
135-
content: parsed.content,
135+
content: { body: parsed.content, matter: parsed.matter },
136136
} satisfies AsyncRuntimeFile);
137137
});
138138

packages/mdx/src/runtime/async.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ export const _runtimeAsync: RuntimeAsync = {
2626
return {
2727
...data,
2828
_file: file,
29-
content,
29+
get content() {
30+
return `${content.matter}${content.body}`;
31+
},
3032
async load() {
3133
const mdxOptions = await initMdxOptions;
32-
const out = await buildMDX(collection, content, {
34+
const out = await buildMDX(collection, content.body, {
3335
...mdxOptions,
3436
development: false,
3537
frontmatter: data as Record<string, unknown>,

packages/mdx/src/runtime/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export interface RuntimeFile {
3939
export interface AsyncRuntimeFile {
4040
info: FileInfo;
4141
data: Record<string, unknown>;
42-
content: string;
42+
content: { matter: string; body: string };
4343
lastModified?: Date;
4444
}
4545

packages/mdx/src/utils/fuma-matter.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import { load } from 'js-yaml';
55

66
interface Output {
7+
/**
8+
* The matter section, including the delimiter.
9+
*/
710
matter: string;
811
content: string;
912
data: unknown;
@@ -22,10 +25,10 @@ export function fumaMatter(input: string): Output {
2225
}
2326

2427
// get the raw front-matter block
25-
output.matter = match[1];
28+
output.matter = match[0];
2629
output.content = input.slice(match[0].length);
2730

28-
const loaded = load(output.matter);
31+
const loaded = load(match[1]);
2932
output.data = loaded ?? {};
3033

3134
return output;

packages/mdx/test/fixtures/index-async.output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ import { _runtimeAsync, buildConfig } from "fumadocs-mdx/runtime/async"
33
const _sourceConfig = buildConfig(_source)
44
import { _runtime } from "fumadocs-mdx"
55
import * as _source from "./config"
6-
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)
6+
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)

packages/mdx/test/index.test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,11 @@ test('parse frontmatter', () => {
9393
"description": "I love Fumadocs",
9494
"title": "hello world",
9595
},
96-
"matter": "title: hello world
97-
description: I love Fumadocs",
96+
"matter": "---
97+
title: hello world
98+
description: I love Fumadocs
99+
---
100+
",
98101
}
99102
`);
100103

@@ -109,8 +112,11 @@ test('parse frontmatter', () => {
109112
"description": "I love Fumadocs",
110113
"title": "hello world",
111114
},
112-
"matter": "title: hello world
113-
description: I love Fumadocs",
115+
"matter": "---
116+
title: hello world
117+
description: I love Fumadocs
118+
---
119+
",
114120
}
115121
`);
116122

0 commit comments

Comments
 (0)