Handle linechunked input

Adds logic to correctly handle line chunked input, in which reading the input will only read until next new line character.
Seems to be used by codemirror, as that's when I found the issue with it.
This commit is contained in:
Daniel Bulant 2023-12-27 16:22:09 +01:00 committed by GitHub
parent 6223674c53
commit 9356c4fe2e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -470,11 +470,20 @@ export const YAMLFrontMatter: MarkdownConfig = {
return false; return false;
} }
cx.checkedYaml = true; cx.checkedYaml = true;
const fmRegex = /(^|^\s*\n)(---\n.+?\n---)/s; if(cx.input.lineChunks) {
const match = fmRegex.exec(cx.input.chunk(0)); let cline = cx.input.chunk(0);
if (match) { if(cline !== "---") return false;
const start = match[1].length; let start = cx.lineStart;
const end = start + match[2].length; let end = 0;
while(cx.nextLine()) {
let cline = cx.input.chunk(cx.lineStart);
if(cline === "---" || cline === "---\n") {
end = cx.lineStart + 3;
break;
}
}
if(end === 0) return false;
line.pos = 3;
cx.addElement( cx.addElement(
cx.elt("YAMLFrontMatter", start, end, [ cx.elt("YAMLFrontMatter", start, end, [
cx.elt("YAMLMarker", start, start + 3), cx.elt("YAMLMarker", start, start + 3),
@ -482,9 +491,24 @@ export const YAMLFrontMatter: MarkdownConfig = {
cx.elt("YAMLMarker", end - 3, end), cx.elt("YAMLMarker", end - 3, end),
]) ])
); );
while (cx.lineStart + line.text.length < end && cx.nextLine()) {}
line.pos = 3;
return true; return true;
} else {
const fmRegex = /(^|^\s*\n)(---\n.+?\n---)/s;
const match = fmRegex.exec(cx.input.chunk(0));
if (match) {
const start = match[1].length;
const end = start + match[2].length;
cx.addElement(
cx.elt("YAMLFrontMatter", start, end, [
cx.elt("YAMLMarker", start, start + 3),
cx.elt("YAMLContent", start + 4, end - 4),
cx.elt("YAMLMarker", end - 3, end),
])
);
while (cx.lineStart + line.text.length < end && cx.nextLine()) {}
line.pos = 3;
return true;
}
} }
return false; return false;
}, },