MDX: Fix incorrect line number with frontmatter on dev mode

This commit is contained in:
Fuma Nama
2025-01-26 21:02:31 +08:00
parent 980f6dd520
commit d6781cc5e3
2 changed files with 24 additions and 5 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-mdx': patch
---
Fix incorrect line number with frontmatter on dev mode
+19 -5
View File
@@ -67,7 +67,6 @@ export default async function loader(
const mdxOptions =
collection?.mdxOptions ?? (await config.getDefaultMDXOptions());
let frontmatter = matter.data;
if (collection?.schema) {
let schema = collection.schema;
@@ -87,7 +86,7 @@ export default async function loader(
});
}
const result = await schema.safeParseAsync(frontmatter);
const result = await schema.safeParseAsync(matter.data);
if (result.error) {
callback(
new Error(
@@ -97,7 +96,7 @@ export default async function loader(
return;
}
frontmatter = result.data as Record<string, unknown>;
matter.data = result.data as Record<string, unknown>;
}
let timestamp: number | undefined;
@@ -105,15 +104,20 @@ export default async function loader(
timestamp = (await getGitTimestamp(filePath))?.getTime();
try {
// ensure the line number is correct in dev mode
const lineOffset = '\n'.repeat(
this.mode === 'development' ? lines(source) - lines(matter.content) : 0,
);
const file = await buildMDX(
collectionId ?? 'global',
configHash,
matter.content,
lineOffset + matter.content,
{
development: this.mode === 'development',
...mdxOptions,
filePath,
frontmatter,
frontmatter: matter.data,
data: {
lastModified: timestamp,
},
@@ -130,3 +134,13 @@ export default async function loader(
callback(error);
}
}
function lines(s: string) {
let num = 0;
for (const c of s) {
if (c === '\n') num++;
}
return num;
}