Skip to content

Commit

Permalink
fix(md): parse Windows newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
pskfyi committed May 6, 2023
1 parent 77b36c1 commit 4336d8e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
13 changes: 13 additions & 0 deletions md/codeBlock/fenced.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ describe("parse", () => {
it("gets rest of info string", () =>
assertEquals(parse("```lang meta data\n```").meta, "meta data"));
});

it("handles Windows newlines", () =>
assertEquals(
parse("```\r\nHello!\r\n```"),
{
type: "fenced",
char: "`",
fence: "```",
code: "Hello!",
lang: undefined,
meta: undefined,
},
));
});

describe("findAll", () => {
Expand Down
10 changes: 5 additions & 5 deletions md/codeBlock/regex.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
export const FENCED_CODE_BLOCK_REGEX = new RegExp(
"^(?<fence>`{3,}|~{3,})" +
"(?<infoString>[^\n]*)\n" +
"((?<code>[\\s\\S]*?)\n)?" +
"(?<infoString>[^\r\n]*)\r?\n" +
"((?<code>[\\s\\S]*?)\r?\n)?" +
"\\k<fence>",
);

export const INDENTED_CODE_BLOCK_REGEX = new RegExp(
"^ {4,}[^\n]+(\n" + // first line, w/ optional trailing newline
"(\n|^ {4,}.*\n)*" + // ... subsequent lines
"^ {4,}[^\n]+)?", // ... and final line, closing w/ )?
"^ {4,}[^\r\n]+(\r?\n" + // first line, w/ optional trailing newline
"(\r?\n|^ {4,}.*\r?\n)*" + // ... subsequent lines
"^ {4,}[^\r\n]+)?", // ... and final line, closing w/ )?
"gm",
);

Expand Down
2 changes: 1 addition & 1 deletion md/script/evalCodeBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function evalCodeBlocks(
const { lang, code } = details;

const langLength = lang?.length ?? 0;
const inlineCode = code.trim().replace(/\n/g, "\\n");
const inlineCode = code.trim().replace(/\r?\n/g, "\\n");
const firstChars = inlineCode.slice(0, consoleWidth - langLength - 4);

const message = firstChars.length < inlineCode.length
Expand Down

0 comments on commit 4336d8e

Please sign in to comment.