MDX: Add page.data.content to sync mode

This commit is contained in:
Fuma Nama
2025-05-03 16:55:58 +08:00
parent 80f662d720
commit 7b89faa74b
6 changed files with 34 additions and 9 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-mdx': patch
---
Add `page.data.content` to sync mode
+6
View File
@@ -58,6 +58,9 @@ export interface Source<Config extends SourceConfig> {
}
export interface Page<Data = PageData> {
/**
* Virtualized file path
*/
file: FileInfo;
slugs: string[];
url: string;
@@ -67,6 +70,9 @@ export interface Page<Data = PageData> {
}
export interface Meta<Data = MetaData> {
/**
* Virtualized file path
*/
file: FileInfo;
data: Data;
}
+2 -2
View File
@@ -9,12 +9,12 @@ export interface FileInfo {
flattenedPath: string;
/**
* Original path of file
* path of file (unparsed)
*/
path: string;
/**
* File name without locale and extension
* File name without extension
*/
name: string;
+3
View File
@@ -37,5 +37,8 @@ export interface MarkdownProps {
}
export interface BaseCollectionEntry {
/**
* Raw file path of collection entry, including absolute path (not normalized).
*/
_file: FileInfo;
}
+7 -1
View File
@@ -1,21 +1,27 @@
import type { VirtualFile } from 'fumadocs-core/source';
import {
type MetaData,
type PageData,
type Source,
} from 'fumadocs-core/source';
import { type BaseCollectionEntry } from '@/config';
import type { VirtualFile } from 'fumadocs-core/source';
import type { Runtime } from '@/runtime/types';
import fs from 'node:fs';
export const _runtime: Runtime = {
doc(files) {
return files.map((file) => {
const { default: body, frontmatter, ...exports } = file.data;
let cachedContent: string | undefined;
return {
body,
...exports,
...(frontmatter as object),
get content() {
cachedContent ??= fs.readFileSync(file.info.absolutePath).toString();
return cachedContent;
},
_exports: file.data,
_file: file.info,
};
+11 -6
View File
@@ -14,12 +14,17 @@ export interface AsyncRuntimeFile {
content: string;
}
type DocOut<Schema extends StandardSchemaV1> = Omit<
MarkdownProps,
keyof StandardSchemaV1.InferOutput<Schema>
> &
StandardSchemaV1.InferOutput<Schema> &
BaseCollectionEntry;
type DocOut<Schema extends StandardSchemaV1> = Override<
MarkdownProps & {
/**
* Read the original content of file from file system.
*/
get content(): string;
},
StandardSchemaV1.InferOutput<Schema> & BaseCollectionEntry
>;
type Override<A, B> = Omit<A, keyof B> & B;
type MetaOut<Schema extends StandardSchemaV1> =
StandardSchemaV1.InferOutput<Schema> & BaseCollectionEntry;