From 9356c4fe2e3add2e9543d8dc5e9fe779ce2839c4 Mon Sep 17 00:00:00 2001 From: Daniel Bulant Date: Wed, 27 Dec 2023 16:22:09 +0100 Subject: [PATCH] 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. --- src/extensions.ts | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/src/extensions.ts b/src/extensions.ts index b1fb2d3..427deac 100644 --- a/src/extensions.ts +++ b/src/extensions.ts @@ -470,11 +470,20 @@ export const YAMLFrontMatter: MarkdownConfig = { return false; } cx.checkedYaml = true; - 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; + if(cx.input.lineChunks) { + let cline = cx.input.chunk(0); + if(cline !== "---") return false; + let start = cx.lineStart; + 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.elt("YAMLFrontMatter", start, end, [ cx.elt("YAMLMarker", start, start + 3), @@ -482,9 +491,24 @@ export const YAMLFrontMatter: MarkdownConfig = { cx.elt("YAMLMarker", end - 3, end), ]) ); - while (cx.lineStart + line.text.length < end && cx.nextLine()) {} - line.pos = 3; 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; },