fix: normalize line endings in frontmatter parsing

Normalize CRLF line endings to LF before parsing frontmatter
to ensure regex matching works consistently across different platforms.
This commit is contained in:
hypercross 2026-07-07 21:47:25 +08:00
parent 6b3a915350
commit 1ef2560faa
1 changed files with 5 additions and 2 deletions

View File

@ -13,7 +13,9 @@ export interface DocEntry {
/** Splits frontmatter and markdown body from a raw .md string. */ /** Splits frontmatter and markdown body from a raw .md string. */
function parseFrontmatter(raw: string): Record<string, unknown> | null { function parseFrontmatter(raw: string): Record<string, unknown> | null {
const match = raw.match(/^---\n([\s\S]*?)\n---\n?([\s\S]*)$/); // Handle CRLF line endings by normalizing to LF first
const normalized = raw.replace(/\r\n/g, "\n");
const match = normalized.match(/^---\n([\s\S]*?)\n---\n?([\s\S]*)$/);
if (!match) return null; if (!match) return null;
try { try {
return yaml.load(match[1]) as Record<string, unknown>; return yaml.load(match[1]) as Record<string, unknown>;
@ -23,7 +25,8 @@ function parseFrontmatter(raw: string): Record<string, unknown> | null {
} }
function getBody(raw: string): string { function getBody(raw: string): string {
const match = raw.match(/^---\n[\s\S]*?\n---\n?([\s\S]*)$/); const normalized = raw.replace(/\r\n/g, "\n");
const match = normalized.match(/^---\n[\s\S]*?\n---\n?([\s\S]*)$/);
return match ? match[1] : raw; return match ? match[1] : raw;
} }