/** * Strip lines starting with `_` (comment convention for prompt .md files) * and collapse any resulting consecutive blank lines. * * Lines inside fenced code blocks (``` or ~~~ delimiters per CommonMark) * are never stripped, so code examples with `_`-prefixed identifiers are preserved. */ export function stripCommentLines(content: string): string { const normalized = content.replace(/\r\n/g, "\n"); let openFenceChar: string | null = null; const filtered = normalized.split("\n").filter((line) => { const fenceMatch = line.match(/^ {0,3}(`{3,}|~{3,})/); if (fenceMatch) { const char = fenceMatch[1][0]; if (!openFenceChar) { openFenceChar = char; } else if (char === openFenceChar) { openFenceChar = null; } } if (openFenceChar) { return true; } return !line.trimStart().startsWith("_"); }); return filtered .join("\n") .replace(/\n{3,}/g, "\n\n") .trim(); }