Skip to content

Commit 77d90bb

Browse files
committed
Don't run prose transform over an unterminated code fence
transformOutsideCode() ran its transform over the remaining tail of the document unconditionally, including text inside a fence that was never closed. Detect an unterminated opening fence and leave it (and everything after it) untouched.
1 parent 0d87a06 commit 77d90bb

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

ui/src/pages/milkdown_shared.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
function transformOutsideCode(md, transform) {
22
const fence = /(^|\n)([ \t]*)(`{3,}|~{3,})[^\n]*\n[\s\S]*?\n[ \t]*\3[ \t]*(?=\n|$)/g;
3+
// Matches only the opening delimiter of a fence, used to detect an
4+
// unterminated fence in the remaining tail once the loop above has
5+
// consumed every properly closed one.
6+
const opener = /(^|\n)([ \t]*)(`{3,}|~{3,})[^\n]*(?:\n|$)/;
37
let output = '';
48
let last = 0;
59
let match;
@@ -8,7 +12,17 @@ function transformOutsideCode(md, transform) {
812
output += match[0];
913
last = match.index + match[0].length;
1014
}
11-
output += transform(md.slice(last));
15+
const tail = md.slice(last);
16+
const unterminated = opener.exec(tail);
17+
if (unterminated) {
18+
// Leave the unterminated fence and everything after it untouched
19+
// instead of running the prose transform over what is meant to be
20+
// code content.
21+
output += transform(tail.slice(0, unterminated.index));
22+
output += tail.slice(unterminated.index);
23+
} else {
24+
output += transform(tail);
25+
}
1226
return output;
1327
}
1428

0 commit comments

Comments
 (0)