fix: show images glued to the empty-line marker by older versions

Before 1.3.7 an empty line before an image could be saved as
<!-- -->![](image.png) on one line, which reads as raw HTML and showed
the image as text. That line is now read as the empty line plus the
image, and the next save writes them apart.
This commit is contained in:
Yuri Karamian committed 2026-10-14 08:53:58 +02:00
1 parent 76919d6f1c
commit 4e2c87434d
2 files changed
+31 -3

No files matched your search

+17 -3
View File
@@ -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;
}
+14
View File
@@ -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'), '<p>one</p>\n<p></p>\n<p>two</p>\n');
assert.equal(parser.render('x <!-- --> y'), '<p>x <!-- --> y</p>\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><\/p>/);
assert.equal((html.match(/<img /g) ?? []).length, 2);
assert.doesNotMatch(html, /&lt;!--|!\[\]|<!--/);
assert.match(html, /<p>After<\/p>/);
const indented = parser.render(' <!-- --> ![](a.png)');
assert.equal((indented.match(/<img /g) ?? []).length, 1);
assert.doesNotMatch(indented, /<pre>/);
// 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)');
});