Skip to content

Commit

Permalink
fix: frontmatter parsing in windows (CLRF)
Browse files Browse the repository at this point in the history
  • Loading branch information
bukowa authored and Gerrit0 committed Jul 10, 2024
1 parent f0f0106 commit ef0f26e
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/lib/converter/comments/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,28 +203,39 @@ export function parseCommentString(
// Check for frontmatter
let frontmatterData: Record<string, unknown> = {};
const firstBlock = content[0];
if (firstBlock.text.startsWith("---\n")) {
const end = firstBlock.text.indexOf("\n---\n");

let lineBreak: string;
switch (firstBlock.text.startsWith("---\r\n")) {
case true:
lineBreak = "\r\n";
break;
case false:
lineBreak = "\n";
break;
}

if (firstBlock.text.startsWith(`---${lineBreak}`)) {
const end = firstBlock.text.indexOf(`${lineBreak}---${lineBreak}`);
if (end !== -1) {
const yamlText = firstBlock.text.slice("---\n".length, end);
const yamlText = firstBlock.text.slice(`---${lineBreak}`.length, end);
firstBlock.text = firstBlock.text
.slice(end + "\n---\n".length)
.slice(end + `${lineBreak}---${lineBreak}`.length)
.trimStart();

const frontmatter = parseYamlDoc(yamlText, { prettyErrors: false });
for (const warning of frontmatter.warnings) {
// Can't translate issues coming from external library...
logger.warn(
warning.message as TranslatedString,
warning.pos[0] + "---\n".length,
warning.pos[0] + `---${lineBreak}`.length,
file,
);
}
for (const error of frontmatter.errors) {
// Can't translate issues coming from external library...
logger.error(
error.message as TranslatedString,
error.pos[0] + "---\n".length,
error.pos[0] + `---${lineBreak}`.length,
file,
);
}
Expand Down

0 comments on commit ef0f26e

Please sign in to comment.