Skip to content

Commit ef0f26e

Browse files
bukowaGerrit0
authored andcommitted
fix: frontmatter parsing in windows (CLRF)
1 parent f0f0106 commit ef0f26e

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

Diff for: src/lib/converter/comments/parser.ts

+17-6
Original file line numberDiff line numberDiff line change
@@ -203,28 +203,39 @@ export function parseCommentString(
203203
// Check for frontmatter
204204
let frontmatterData: Record<string, unknown> = {};
205205
const firstBlock = content[0];
206-
if (firstBlock.text.startsWith("---\n")) {
207-
const end = firstBlock.text.indexOf("\n---\n");
206+
207+
let lineBreak: string;
208+
switch (firstBlock.text.startsWith("---\r\n")) {
209+
case true:
210+
lineBreak = "\r\n";
211+
break;
212+
case false:
213+
lineBreak = "\n";
214+
break;
215+
}
216+
217+
if (firstBlock.text.startsWith(`---${lineBreak}`)) {
218+
const end = firstBlock.text.indexOf(`${lineBreak}---${lineBreak}`);
208219
if (end !== -1) {
209-
const yamlText = firstBlock.text.slice("---\n".length, end);
220+
const yamlText = firstBlock.text.slice(`---${lineBreak}`.length, end);
210221
firstBlock.text = firstBlock.text
211-
.slice(end + "\n---\n".length)
222+
.slice(end + `${lineBreak}---${lineBreak}`.length)
212223
.trimStart();
213224

214225
const frontmatter = parseYamlDoc(yamlText, { prettyErrors: false });
215226
for (const warning of frontmatter.warnings) {
216227
// Can't translate issues coming from external library...
217228
logger.warn(
218229
warning.message as TranslatedString,
219-
warning.pos[0] + "---\n".length,
230+
warning.pos[0] + `---${lineBreak}`.length,
220231
file,
221232
);
222233
}
223234
for (const error of frontmatter.errors) {
224235
// Can't translate issues coming from external library...
225236
logger.error(
226237
error.message as TranslatedString,
227-
error.pos[0] + "---\n".length,
238+
error.pos[0] + `---${lineBreak}`.length,
228239
file,
229240
);
230241
}

0 commit comments

Comments
 (0)