mirror of
https://github.com/SableClient/Sable.git
synced 2026-12-04 07:42:24 -08:00
fix: block quote new lines
This commit is contained in:
1 parent
fdeaf481ef
commit
db2f11518d
3 files changed
+55
-5
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
default: patch
|
||||
---
|
||||
|
||||
Fix single new lines after block quotes being block-quoted.
|
||||
@@ -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;
|
||||
|
||||
@@ -137,6 +137,21 @@ describe('markdownToHtml', () => {
|
||||
expect(markdownToHtml('intro\n> quote')).toContain('<blockquote>');
|
||||
});
|
||||
|
||||
it('ends blockquote when the next line does not start with >', () => {
|
||||
const html = markdownToHtml('> test\ntest 2');
|
||||
expect(html).toContain('<blockquote>');
|
||||
expect(html).toContain('test');
|
||||
expect(html).not.toMatch(/<blockquote>[\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('<blockquote>');
|
||||
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('<sub');
|
||||
|
||||
Reference in new issue
Block a user