diff --git a/src/lib/editor/markdownParser.ts b/src/lib/editor/markdownParser.ts index 7a7924f..9c1e0fd 100644 --- a/src/lib/editor/markdownParser.ts +++ b/src/lib/editor/markdownParser.ts @@ -276,14 +276,28 @@ function legacyLink(state: StateInline, silent: boolean): boolean { return true; } -function emptyParagraph(state: StateBlock, startLine: number, _endLine: number, silent: boolean): boolean { +const EMPTY_PARAGRAPH_MARKER = ''; + +function emptyParagraph(state: StateBlock, startLine: number, endLine: number, silent: boolean): boolean { const from = state.bMarks[startLine] + state.tShift[startLine]; - if (state.src.slice(from, state.eMarks[startLine]).trim() !== '') return false; + const line = state.src.slice(from, state.eMarks[startLine]); + // Versions before 1.3.7 glued the marker to a following image (`![](a.png)`), which + // reads as a raw HTML line and showed the image as text. Read it as the empty paragraph and the + // image it was written from. + const glued = line.startsWith(EMPTY_PARAGRAPH_MARKER) && line.slice(EMPTY_PARAGRAPH_MARKER.length).trimStart().startsWith('!['); + if (!glued && line.trim() !== EMPTY_PARAGRAPH_MARKER) return false; if (silent) return true; const token = state.push('helix_empty', 'p', 0); token.block = true; token.map = [startLine, startLine + 1]; - state.line = startLine + 1; + if (!glued) { + state.line = startLine + 1; + return true; + } + const rest = from + EMPTY_PARAGRAPH_MARKER.length; + state.bMarks[startLine] = rest; + state.tShift[startLine] = state.src.slice(rest, state.eMarks[startLine]).search(/\S/); + state.md.block.tokenize(state, startLine, endLine); return true; } diff --git a/tests/markdown-parser.test.mjs b/tests/markdown-parser.test.mjs index 77c2c40..b457a3c 100644 --- a/tests/markdown-parser.test.mjs +++ b/tests/markdown-parser.test.mjs @@ -276,3 +276,17 @@ test('HTML comments are kept as text, the blank-line marker is not', () => { assert.equal(parser.render('one\n\n\n\ntwo'), '

one

\n

\n

two

\n'); assert.equal(parser.render('x y'), '

x y

\n'); }); + +test('an empty paragraph marker glued to an image by older versions still shows the image', () => { + const html = parser.render('Before\n\n![](a.png)\n![](b.png)\n\nAfter'); + assert.match(html, /

<\/p>/); + assert.equal((html.match(/After<\/p>/); + const indented = parser.render(' ![](a.png)'); + assert.equal((indented.match(//); + // Anything else after the marker is still raw HTML, and code keeps the line as written. + assert.match(parser.render('text'), /^text/); + assert.equal(unescape(escapedCode(parser.render('```\n![](a.png)\n```'))), '![](a.png)'); +});