From 2c58bf3d9a6c1149c703c6c25b5610142b632134 Mon Sep 17 00:00:00 2001 From: Yuri Karamian Date: Tue, 30 Jun 2026 11:01:25 +0200 Subject: [PATCH] Fix empty lines round-trip and trim trailing markers (#185) --- src/lib/components/Editor.svelte | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/lib/components/Editor.svelte b/src/lib/components/Editor.svelte index 2c4881f..43ea8cb 100644 --- a/src/lib/components/Editor.svelte +++ b/src/lib/components/Editor.svelte @@ -3010,6 +3010,9 @@ } entries.push({ text: serializeNode(node), isImage: isImageNode(node) }); }); + while (entries.length > 0 && entries[entries.length - 1].text === '') { + entries.pop(); + } // Join: skip extra \n separator before image nodes so they don't get unwanted blank lines let result = ''; for (let i = 0; i < entries.length; i++) { @@ -3819,10 +3822,10 @@ src = src.replace(/^([\s>]*)-\s\[ \][^\S\n]+(.+)$/gm, '$1- $2'); src = src.replace(/^([\s>]*)-\s\[ \][^\S\n]*$/gm, '$1-  '); - // Pre-process: strip list-separator comments before markdown-it. - // markdown-it treats as an HTML block start, swallowing the - // next line (e.g. an image) as raw HTML instead of parsing it. - src = src.replace(//g, '\n'); + // Pre-process: replace empty-paragraph markers with a div sentinel before markdown-it. + // A bare starts an HTML block that swallows the next line; the blank lines + // around the div keep it isolated. Post-render converts it back to

. + src = src.replace(//g, '\n\n
\n\n'); // Pre-process: preserve blank lines before image-only lines // markdown-it collapses blank lines into paragraph breaks, losing the empty paragraph. @@ -3839,8 +3842,8 @@ html = html.replace(/]*)>\n?/g, ''); html = html.replace(/\n<\/code>/g, ''); - // Post-process: convert list-separator comments back to empty paragraphs for TipTap - html = html.replace(//g, '

'); + // Post-process: convert empty-paragraph div sentinels back to empty paragraphs for TipTap + html = html.replace(/
<\/div>\n?/g, '

\n'); // Post-process: convert task list items to TipTap format // Convert opening
  • + into data-attributed
  • , handles both tight and loose (with

    ) lists