diff --git a/.changeset/fix-blockquote-newlines.md b/.changeset/fix-blockquote-newlines.md new file mode 100644 index 000000000..3e963a5cc --- /dev/null +++ b/.changeset/fix-blockquote-newlines.md @@ -0,0 +1,5 @@ +--- +default: patch +--- + +Fix single new lines after block quotes being block-quoted. diff --git a/src/app/plugins/markdown/expandBlockNewlines.ts b/src/app/plugins/markdown/expandBlockNewlines.ts index 6c860744e..efcbf7257 100644 --- a/src/app/plugins/markdown/expandBlockNewlines.ts +++ b/src/app/plugins/markdown/expandBlockNewlines.ts @@ -104,16 +104,46 @@ function looksLikeBlockStart(effective: string): boolean { return false; } -function nextLineIsBlockStarter(md: string, newlineIdx: number): boolean { +function lineAtNewline(md: string, newlineIdx: number): string { + let start = newlineIdx - 1; + while (start >= 0 && md[start] !== '\n') start--; + return md.slice(start + 1, newlineIdx); +} + +function lineAfterNewline(md: string, newlineIdx: number): string { const start = newlineIdx + 1; - if (start >= md.length) return false; + if (start >= md.length) return ''; const nextNl = md.indexOf('\n', start); - const line = nextNl === -1 ? md.slice(start) : md.slice(start, nextNl); - const effective = effectiveContentAfterEscapes(line); + return nextNl === -1 ? md.slice(start) : md.slice(start, nextNl); +} + +function prevLineIsBlockquote(md: string, newlineIdx: number): boolean { + const effective = effectiveContentAfterEscapes(lineAtNewline(md, newlineIdx)); + if (effective === null) return false; + return /^>\s/.test(effective); +} + +function nextLineContinuesBlockquote(md: string, newlineIdx: number): boolean { + const effective = effectiveContentAfterEscapes(lineAfterNewline(md, newlineIdx)); + if (effective === null) return false; + return effective.startsWith('>'); +} + +function nextLineIsBlockStarter(md: string, newlineIdx: number): boolean { + const effective = effectiveContentAfterEscapes(lineAfterNewline(md, newlineIdx)); if (effective === null) return false; return looksLikeBlockStart(effective); } +function shouldExpandSingleNewline(md: string, newlineIdx: number): boolean { + if (nextLineIsBlockStarter(md, newlineIdx)) return true; + // CommonMark lazy continuation keeps non-`>` lines inside blockquotes, close on single `\n`. + if (prevLineIsBlockquote(md, newlineIdx) && !nextLineContinuesBlockquote(md, newlineIdx)) { + return true; + } + return false; +} + /** * After a single newline (not part of `\n\n`), inserts one more `\n` when the following line * opens a block the marked stack understands. Fenced code is copied verbatim without changes. @@ -139,7 +169,7 @@ export function expandBlockBoundariesAfterSingleNewlines(markdown: string): stri md[i] === '\n' && (i === 0 || md[i - 1] !== '\n') && (i + 1 >= n || md[i + 1] !== '\n') && - nextLineIsBlockStarter(md, i) + shouldExpandSingleNewline(md, i) ) { out += '\n\n'; i += 1; diff --git a/src/app/plugins/markdown/markdownToHtml.test.ts b/src/app/plugins/markdown/markdownToHtml.test.ts index d6e2b2461..aa41188b0 100644 --- a/src/app/plugins/markdown/markdownToHtml.test.ts +++ b/src/app/plugins/markdown/markdownToHtml.test.ts @@ -137,6 +137,21 @@ describe('markdownToHtml', () => { expect(markdownToHtml('intro\n> quote')).toContain('
'); }); + it('ends blockquote when the next line does not start with >', () => { + const html = markdownToHtml('> test\ntest 2'); + expect(html).toContain('
'); + expect(html).toContain('test'); + expect(html).not.toMatch(/
[\s\S]*test 2[\s\S]*<\/blockquote>/); + expect(html).toContain('test 2'); + }); + + it('keeps consecutive blockquote lines with > markers', () => { + const html = markdownToHtml('> line one\n> line two'); + expect(html).toContain('
'); + expect(html).toContain('line one'); + expect(html).toContain('line two'); + }); + it('does not promote -# inside fenced code when the fence follows a single newline', () => { const html = markdownToHtml('test\n```\n-# not sub\n```'); expect(html).not.toContain('