Repository navigation
fix(core): correct markdown parsing of hard breaks, CRLF line ends, link URLs and code spans #3385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
639a1ca
c1cbce5
aebcf9d
cd0801c
ee143e6
75374ea
d9e837f
1126946
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // @vitest-environment node | ||
| import { describe, expect } from "vite-plus/test"; | ||
| import { markdownToHtml } from "./markdownToHtml.js"; | ||
|
|
||
| describe("markdownToHtml", () => { | ||
| // CommonMark: a line ending is "\n", "\r\n" or "\r". A file from Windows | ||
| // has "\r\n". | ||
| it.each([ | ||
| ["\\r\\n", "\r\n"], | ||
| ["\\r", "\r"], | ||
| ])("reads %s as a line end", (_, lineEnd) => { | ||
| const lines = [ | ||
| "# Heading", | ||
| "", | ||
| "* Bullet", | ||
| " * Nested bullet", | ||
| "", | ||
| "1. Numbered", | ||
| "", | ||
| "- [x] Checked", | ||
| "", | ||
| "> Quote", | ||
| "", | ||
| "Line one\\", | ||
| "line two", | ||
| "", | ||
| "```js", | ||
| "const a = 1;", | ||
| "```", | ||
| "", | ||
| ]; | ||
|
|
||
| expect(markdownToHtml(lines.join(lineEnd))).toBe( | ||
| markdownToHtml(lines.join("\n")), | ||
| ); | ||
| }); | ||
|
|
||
| // A paragraph line after the first can have any indent, and the indent is | ||
| // not a part of the text. But a code span has its text as it is: only the | ||
| // line end becomes a space (CommonMark). | ||
| describe("the indent of a paragraph line after the first", () => { | ||
| it.each([ | ||
| ["a line end alone", "aaa\n bbb\n\tccc"], | ||
| ["a backslash", "aaa\\\n bbb\\\n\tccc"], | ||
| ["two spaces", "aaa \n bbb \n\tccc"], | ||
| ])("is removed after a line break from %s", (_, markdown) => { | ||
| expect(markdownToHtml(markdown)).toBe("<p>aaa<br>bbb<br>ccc</p>"); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ["a tab", "`foo\n\tbar`", "<p><code>foo \tbar</code></p>"], | ||
| ["spaces", "`foo\n bar`", "<p><code>foo bar</code></p>"], | ||
| ])("stays in a code span, for %s", (_, markdown, html) => { | ||
| expect(markdownToHtml(markdown)).toBe(html); | ||
| }); | ||
| }); | ||
|
|
||
| // The info string of a backtick code fence cannot contain a backtick | ||
| // (CommonMark). Thus a line that starts with a code span of three backticks | ||
| // is text. The markdown exporter writes such a code span for code that | ||
| // contains two backticks. | ||
| describe("a line that starts with a code span of three backticks", () => { | ||
| it("is a paragraph and not the start of a code block", () => { | ||
| expect(markdownToHtml("```a``b```\n\nnext")).toBe( | ||
| "<p><code>a``b</code></p><p>next</p>", | ||
| ); | ||
| }); | ||
|
|
||
| it("continues the paragraph before it", () => { | ||
| expect(markdownToHtml("text\n```a``b```")).toMatch( | ||
| /^<p>text<br>\s*<code>a``b<\/code><\/p>$/, | ||
| ); | ||
| }); | ||
|
|
||
| it("continues the quote before it", () => { | ||
| expect(markdownToHtml("> quote\n```a``b```")).toMatch( | ||
| /^<blockquote><p>quote<br>\s*<code>a``b<\/code><\/p><\/blockquote>$/, | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,22 @@ type InlineTokenizer = ( | |
| allowLinks: boolean, | ||
| ) => { html: string; end: number } | null; | ||
|
|
||
| /** The characters that a backslash escapes. */ | ||
| const ESCAPABLE_CHARACTERS = "\\`*_{}[]()#+-.!~|<>"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Include all escapable ASCII punctuation. For
|
||
|
|
||
| /** | ||
| * Gives the index after the indent (spaces and tabs) of the line that starts | ||
| * at `i`. The line after a line break in a paragraph can have any indent, | ||
| * which is not a part of the text (CommonMark). The line break rules use this | ||
| * and a code span does not, so the indent of a line in a code span stays. | ||
| */ | ||
| function skipIndent(text: string, i: number): number { | ||
| while (text[i] === " " || text[i] === "\t") { | ||
| i++; | ||
| } | ||
| return i; | ||
| } | ||
|
|
||
| function tryBackslashEscape( | ||
| text: string, | ||
| i: number, | ||
|
|
@@ -54,12 +70,14 @@ function tryBackslashEscape( | |
| return null; | ||
| } | ||
| const next = text[i + 1]; | ||
| // Hard line break: backslash at end of line | ||
| // Hard line break: backslash at end of line. No newline follows the `<br>`: | ||
| // the HTML parser reads that newline as a space at the start of the next | ||
| // line. | ||
| if (next === "\n") { | ||
| return { html: "<br>\n", end: i + 2 }; | ||
| return { html: "<br>", end: skipIndent(text, i + 2) }; | ||
| } | ||
| // Escapable characters | ||
| if ("\\`*_{}[]()#+-.!~|<>".includes(next)) { | ||
| if (ESCAPABLE_CHARACTERS.includes(next)) { | ||
| return { html: escapeHtml(next), end: i + 2 }; | ||
| } | ||
| return null; | ||
|
|
@@ -228,7 +246,7 @@ function trySoftBreak( | |
| i: number, | ||
| ): { html: string; end: number } | null { | ||
| if (text[i] === "\n") { | ||
| return { html: "<br>\n", end: i + 1 }; | ||
| return { html: "<br>", end: skipIndent(text, i + 1) }; | ||
| } | ||
| return null; | ||
| } | ||
|
|
@@ -331,8 +349,8 @@ function parseInline(text: string, allowLinks = true): string { | |
| text[i - 2] === " " | ||
| ) { | ||
| result = result.replace(/ +$/, ""); | ||
| result += "<br>\n"; | ||
| i++; | ||
| result += "<br>"; | ||
| i = skipIndent(text, i + 1); | ||
| continue; | ||
| } | ||
|
|
||
|
|
@@ -582,7 +600,18 @@ function parseDestinationAndTitle(raw: string): { | |
| } | ||
| } | ||
|
|
||
| return { url, title }; | ||
| return { url: removeBackslashEscapes(url), title }; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Escape backslashes in image sources during markdown export.
Suggested fix
|
||
| } | ||
|
|
||
| /** | ||
| * Removes the backslash of each backslash escape. As in CommonMark, a | ||
| * backslash escape works in a link destination as it does in text. The | ||
| * markdown exporter writes `\(`, `\)` and `\\` in a link destination. | ||
| */ | ||
| function removeBackslashEscapes(text: string): string { | ||
| return text.replace(/\\(.)/g, (match, char: string) => | ||
| ESCAPABLE_CHARACTERS.includes(char) ? char : match, | ||
| ); | ||
| } | ||
|
|
||
| function parseDelimited( | ||
|
|
@@ -803,7 +832,9 @@ function isHtmlBlockStart(line: string): boolean { | |
| // ─── Block-Level Tokenizer ────────────────────────────────────────────────── | ||
|
|
||
| function tokenize(markdown: string): Token[] { | ||
| const lines = markdown.split("\n"); | ||
| // A line ends with "\n", "\r\n" or "\r" (CommonMark). A file from Windows | ||
| // has "\r\n". | ||
| const lines = markdown.split(/\r\n?|\n/); | ||
| const tokens: Token[] = []; | ||
| let i = 0; | ||
| let prevLineWasBlank = true; // treat start of document as after blank | ||
|
|
@@ -818,8 +849,11 @@ function tokenize(markdown: string): Token[] { | |
| continue; | ||
| } | ||
|
|
||
| // Fenced code block (0-3 leading spaces allowed per CommonMark) | ||
| const fenceMatch = line.match(/^ {0,3}(`{3,}|~{3,})(.*)$/); | ||
| // Fenced code block (0-3 leading spaces allowed per CommonMark). The info | ||
| // string after a backtick fence cannot contain a backtick (CommonMark). | ||
| // Thus a line that starts with a code span, for example ```a``b```, is | ||
| // not a fence. | ||
| const fenceMatch = line.match(/^ {0,3}(`{3,}(?=[^`]*$)|~{3,})(.*)$/); | ||
| if (fenceMatch) { | ||
| const fence = fenceMatch[1]; | ||
| const fenceChar = fence[0]; | ||
|
|
@@ -936,7 +970,7 @@ function tokenize(markdown: string): Token[] { | |
| if (/^(#{1,6})\s/.test(cur)) { | ||
| break; | ||
| } // heading | ||
| if (/^(`{3,}|~{3,})/.test(cur)) { | ||
| if (/^(`{3,}(?=[^`]*$)|~{3,})/.test(cur)) { | ||
| break; | ||
| } // code fence | ||
| if (/^(\s{0,3})([-*_])\s*(\2\s*){2,}$/.test(cur)) { | ||
|
|
@@ -1098,7 +1132,7 @@ function tokenize(markdown: string): Token[] { | |
| if (/^(#{1,6})\s/.test(nextLine)) { | ||
| break; | ||
| } | ||
| if (/^(`{3,}|~{3,})/.test(nextLine)) { | ||
| if (/^(`{3,}(?=[^`]*$)|~{3,})/.test(nextLine)) { | ||
| break; | ||
| } | ||
| if (/^\s{0,3}>/.test(nextLine)) { | ||
|
|
@@ -1127,14 +1161,16 @@ function tokenize(markdown: string): Token[] { | |
| paraLines.push(nextLine); | ||
| i++; | ||
| } | ||
| // CommonMark allows up to 3 leading spaces of indent on paragraph lines. | ||
| // CommonMark allows up to 3 leading spaces of indent on the first | ||
| // paragraph line. The lines after it can have any indent: the line break | ||
| // rules of `parseInline` remove it (see `skipIndent`). | ||
| // Also strip trailing whitespace from the final line so a trailing | ||
| // hard-break sequence (` \n` at end of paragraph) doesn't leak as | ||
| // literal trailing spaces in the rendered output. | ||
| tokens.push({ | ||
| type: "paragraph", | ||
| content: paraLines | ||
| .map((l) => l.replace(/^ {1,3}/, "")) | ||
| .map((l, index) => (index === 0 ? l.replace(/^ {1,3}/, "") : l)) | ||
| .join("\n") | ||
| .replace(/[ \t]+$/, ""), | ||
| }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ | |
| { | ||
| "styles": {}, | ||
| "text": "Line 1 | ||
| Line 2", | ||
| Line 2", | ||
| "type": "text", | ||
| }, | ||
| ], | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| [ | ||
| { | ||
| "children": [], | ||
| "content": [ | ||
| { | ||
| "styles": { | ||
| "code": true, | ||
| }, | ||
| "text": " Code text ", | ||
| "type": "text", | ||
| }, | ||
| ], | ||
| "id": "1", | ||
| "props": { | ||
| "backgroundColor": "default", | ||
| "textAlignment": "left", | ||
| "textColor": "default", | ||
| }, | ||
| "type": "paragraph", | ||
| }, | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| [ | ||
| { | ||
| "children": [], | ||
| "content": [ | ||
| { | ||
| "content": [ | ||
| { | ||
| "styles": {}, | ||
| "text": "Link", | ||
| "type": "text", | ||
| }, | ||
| ], | ||
| "href": "https://example.com/wiki/Page_(topic)", | ||
| "type": "link", | ||
| }, | ||
| ], | ||
| "id": "1", | ||
| "props": { | ||
| "backgroundColor": "default", | ||
| "textAlignment": "left", | ||
| "textColor": "default", | ||
| }, | ||
| "type": "paragraph", | ||
| }, | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| [ | ||
| { | ||
| "children": [], | ||
| "content": undefined, | ||
| "id": "1", | ||
| "props": { | ||
| "backgroundColor": "default", | ||
| "caption": "", | ||
| "name": "Example", | ||
| "showPreview": true, | ||
| "textAlignment": "left", | ||
| "url": "\\server\share\image.png", | ||
| }, | ||
| "type": "image", | ||
| }, | ||
| { | ||
| "children": [], | ||
| "content": undefined, | ||
| "id": "2", | ||
| "props": { | ||
| "backgroundColor": "default", | ||
| "caption": "", | ||
| "name": "Example", | ||
| "showPreview": true, | ||
| "textAlignment": "left", | ||
| "url": "C:\videos\_old\video.mp4", | ||
| }, | ||
| "type": "video", | ||
| }, | ||
| ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preserve destinations with a backslash before whitespace.
For
foo\ bar.png, the previous output kept the whole destination: the parser skipped the escaped space and retained the backslash. The new output doubles the backslash, so the parser splits at the space and returns onlyfoo\. This also affects the other changed media serializers. Encode such destinations in a form that preserves the complete URL.