fix: keep callouts, numbered lists, images and blank lines intact on save

Render the first soft break of a callout's opening paragraph as a newline
so the title and body split correctly again. Only turn [ ] and [x] markers
into task items in bullet lists, not numbered ones. Join serialized blocks
in markdown.ts so an empty paragraph before an image is written as a blank
line, and lists no longer gain an extra blank line, without collapsing blank
lines inside code blocks.

In Version History, compare the current note and snapshots after ignoring
leading and trailing blank lines, so unchanged last lines and a blank line
after the frontmatter no longer show as changes.
This commit is contained in:
Yuri Karamian committed 2026-10-07 22:12:20 +02:00
1 parent b2c306d69f
commit 8d3338ef89
8 files changed
+168 -29

No files matched your search

+7 -23
View File
@@ -58,7 +58,7 @@
import { clearFormatting } from '$lib/editor/clearFormatting';
import { toolbarLabels, resolveToolbarLayout, groupToolbarButtons, moveToolbarButton, toolbarLayoutPreference, type ToolbarButtonId, type ToolbarArea, type ResolvedToolbarLayout } from '$lib/editor/toolbar';
import { buildCodeCopyDecorations, buildTaskMetaDecorations, focusedPlaceholderDecorations } from '$lib/editor/decorations';
import { serializeInlineMarkdown, serializeCodeBlock } from '$lib/editor/markdown';
import { serializeInlineMarkdown, serializeCodeBlock, joinMarkdownBlocks, type MarkdownBlock } from '$lib/editor/markdown';
import { restoreTitleHeading, stripTitleHeading, type HiddenTitleHeading } from '$lib/editor/titleVisibility';
import { documentHeadings, sourceHeadings, activeHeadingIndex, mapDocumentHeadings, mapSourceHeadings, type OutlineHeading } from '$lib/editor/outline';
import { createEditorMarkdownParser } from '$lib/editor/markdownParser';
@@ -3956,31 +3956,15 @@
}
function prosemirrorToMarkdown(doc: any): string {
const entries: { text: string; isImage: boolean }[] = [];
const blocks: MarkdownBlock[] = [];
doc.forEach((node: any) => {
// Preserve every empty paragraph so markdown round-trip keeps the user's vertical
// spacing exactly. The parser converts the marker (or a blank line before an image)
// back to <p></p> on load.
const isEmpty = node.type.name === 'paragraph' && node.childCount === 0;
// Preserve every empty paragraph as <!-- --> so markdown round-trip keeps the user's
// vertical spacing exactly. markdownToHtml converts the marker back to <p></p> on load.
if (isEmpty) {
entries.push({ text: '<!-- -->', isImage: false });
return;
}
entries.push({ text: serializeNode(node), isImage: isImageNode(node) });
blocks.push({ text: isEmpty ? '' : serializeNode(node), isImage: !isEmpty && isImageNode(node), isEmpty });
});
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++) {
if (i === 0) {
result = entries[i].text;
} else {
const separator = entries[i].isImage ? '' : '\n';
result += separator + entries[i].text;
}
}
return result.trim() + '\n';
return joinMarkdownBlocks(blocks);
}
function tableToMarkdown(table: any): string {
+2 -2
View File
@@ -1,7 +1,7 @@
<script lang="ts">
import { tick } from 'svelte';
import SnapshotDocument from './SnapshotDocument.svelte';
import { compareHistory, splitSnapshot, type HistoryComparison, type HistoryRow } from '$lib/utils/history-comparison';
import { comparableBody, compareHistory, splitSnapshot, type HistoryComparison, type HistoryRow } from '$lib/utils/history-comparison';
let { raw, current, selected, loading, error, canRestore, onRetry, onRestore, renderSnapshot }: {
raw: string | null;
@@ -40,7 +40,7 @@
comparisonError = null;
comparing = content !== null;
if (content !== null) {
compareHistory(working, body).then((result) => {
compareHistory(comparableBody(working), comparableBody(body)).then((result) => {
if (generation !== request) return;
comparison = result;
}).catch((cause) => {
+30
View File
@@ -19,6 +19,36 @@ function backtickDelimiter(text: string, minimum: number): string {
return '`'.repeat(length);
}
export interface MarkdownBlock {
text: string;
isImage: boolean;
/** An empty paragraph, which is kept as a marker line so its vertical space round-trips. */
isEmpty?: boolean;
}
const EMPTY_PARAGRAPH_MARKER = '<!-- -->';
/**
* Join serialized top-level blocks into a note body. Blocks that already end in a blank line
* (lists) get no extra separator, and an empty paragraph directly before an image is written as
* the blank line the parser turns back into that paragraph, not as a marker glued to the image.
*/
export function joinMarkdownBlocks(blocks: readonly MarkdownBlock[]): string {
const entries = [...blocks];
while (entries.length > 0 && entries[entries.length - 1].isEmpty) entries.pop();
let result = '';
entries.forEach((entry, index) => {
if (entry.isEmpty && entries[index + 1]?.isImage) {
if (index > 0 && !result.endsWith('\n\n')) result += result.endsWith('\n') ? '\n' : '\n\n';
return;
}
const text = entry.isEmpty ? EMPTY_PARAGRAPH_MARKER : entry.text;
// Images follow their predecessor directly; every other block needs a blank line before it.
result += index === 0 ? text : (entry.isImage || result.endsWith('\n\n') ? '' : '\n') + text;
});
return result.trim() + '\n';
}
export function serializeCodeBlock(code: string, language: string = ''): string {
const fence = backtickDelimiter(code, 3);
return `${fence}${language}\n${code}\n${fence}\n`;
+15 -2
View File
@@ -243,12 +243,14 @@ export function createEditorMarkdownParser(): MarkdownIt {
const list = lists[lists.length - 1];
if (!list.item || !list.firstParagraph || token.level !== list.item.level + 2) continue;
list.firstParagraph = false;
// Task items only exist in bullet lists; `1. [x] text` stays an ordinary numbered item.
if (list.open.type !== 'bullet_list_open') continue;
const checkbox = token.content.match(/^\[(x| )\](?:[^\S\n]+|$)/u);
if (!checkbox) continue;
token.content = token.content.slice(checkbox[0].length);
list.item.attrSet('data-type', 'taskItem');
list.item.attrSet('data-checked', checkbox[1] === 'x' ? 'true' : 'false');
if (list.firstItem && list.open.type === 'bullet_list_open') list.open.attrSet('data-type', 'taskList');
if (list.firstItem) list.open.attrSet('data-type', 'taskList');
}
}
});
@@ -281,6 +283,17 @@ export function createEditorMarkdownParser(): MarkdownIt {
if (lines[line].startsWith('![') && lines[line - 1].trim() === '') paragraph.meta = { helixImageGap: true };
}
});
// Callout rendering splits the title line from the body at the first newline of the opening
// paragraph, so that one soft break must stay a newline instead of becoming a space.
md.core.ruler.after('inline', 'helix_callout_title', state => {
for (let index = 0; index + 2 < state.tokens.length; index++) {
const [quote, paragraph, inline] = [state.tokens[index], state.tokens[index + 1], state.tokens[index + 2]];
if (quote.type !== 'blockquote_open' || paragraph.type !== 'paragraph_open' || inline.type !== 'inline') continue;
if (!/^\[![\w-]+\]/u.test(inline.content)) continue;
const softbreak = inline.children?.find(child => child.type === 'softbreak');
if (softbreak) softbreak.meta = { helixCalloutTitle: true };
}
});
md.renderer.rules.paragraph_open = (tokens, index, options, _env, renderer) =>
(tokens[index].meta?.helixImageGap ? '<p></p>\n' : '') + renderer.renderToken(tokens, index, options);
@@ -308,7 +321,7 @@ export function createEditorMarkdownParser(): MarkdownIt {
// Only the final structural LF is parser-added; leading and extra trailing blank lines are literal.
return renderFence(tokens, index, options, env, renderer).replace(/\n<\/code>/u, '</code>');
};
md.renderer.rules.softbreak = () => ' ';
md.renderer.rules.softbreak = (tokens, index) => (tokens[index].meta?.helixCalloutTitle ? '\n' : ' ');
md.renderer.rules.hardbreak = () => '<br>';
return md;
}
+34 -1
View File
@@ -1,6 +1,6 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { compareHistory, splitSnapshot } from './history-comparison.ts';
import { comparableBody, compareHistory, splitSnapshot } from './history-comparison.ts';
function rowsOf(comparison) {
return comparison.chunks.flatMap((chunk) => chunk.rows);
@@ -259,3 +259,36 @@ test('large notes preserve hidden content as well as an appended snapshot line',
['unchanged', 2997], ['lines', 4],
]);
});
test('comparable bodies ignore only leading and trailing blank lines', () => {
assert.equal(comparableBody(''), '');
assert.equal(comparableBody('\n\n'), '');
assert.equal(comparableBody('body'), 'body\n');
assert.equal(comparableBody('\n\n# T\nbody\n\n'), '# T\nbody\n');
assert.equal(comparableBody('\r\n# T\r\nbody\r\n'), '# T\r\nbody\n');
assert.equal(comparableBody('a\n\n\nb'), 'a\n\n\nb\n');
assert.equal(comparableBody(comparableBody('x\n')), 'x\n');
});
test('a stripped current body matches a snapshot that keeps its file whitespace', async () => {
// The app hands the review the note body without edge newlines; the stored snapshot keeps them.
const current = '# T\nbody';
for (const raw of [
'---\ntitle: T\n---\n# T\nbody\n',
'---\ntitle: T\n---\n\n# T\nbody\n',
'---\r\ntitle: T\r\n---\r\n\r\n# T\r\nbody\r\n',
]) {
const comparison = await compareHistory(comparableBody(current), comparableBody(splitSnapshot(raw).body));
assert.equal(comparison.added, 0, JSON.stringify(raw));
assert.equal(comparison.removed, 0, JSON.stringify(raw));
}
const changed = await compareHistory(
comparableBody(current),
comparableBody(splitSnapshot('---\ntitle: T\n---\n# T\nedited\n').body),
);
assert.deepEqual([changed.added, changed.removed], [1, 1]);
assert.deepEqual(
rowsOf(changed).filter((row) => row.kind !== 'same').map((row) => [row.kind, row.text]),
[['removed', 'body'], ['added', 'edited']],
);
});
+10
View File
@@ -34,6 +34,16 @@ export function splitSnapshot(raw: string): { body: string; metadata: string } {
return { body: raw.slice(bodyStart), metadata: raw.slice(0, bodyStart) };
}
/**
* Make two note bodies comparable. The note body the app loads has its leading and trailing
* blank lines removed, while a stored snapshot keeps the file's exact text, so differences in
* those lines are not changes the editor can show or restore.
*/
export function comparableBody(body: string): string {
const trimmed = body.replace(/^(?:\r?\n)+/, '').replace(/(?:\r?\n)+$/, '');
return trimmed === '' ? '' : trimmed + '\n';
}
/** Added/removed rows describe restoring the snapshot over the current editor body. */
export async function compareHistory(
currentBody: string,
+22
View File
@@ -165,3 +165,25 @@ test('code whitespace is marked for DOM import without changing soft or hard lin
assert.match(html, /<p data-helix-code="">/);
assert.match(html, /<\/code> next<br>last/);
});
test('a callout keeps a newline between its title line and body, while ordinary soft breaks stay spaces', () => {
assert.match(parser.render('> [!warning] Heads up\n> body here\n> more'), /\[!warning\] Heads up\nbody here more/);
assert.match(parser.render('> [!note]\n> body\n> more'), /\[!note\]\nbody more/);
assert.match(parser.render('> [!todo] Outer\n> > [!tip] Inner\n> > body'), /\[!todo\] Outer<\/p>[\s\S]*\[!tip\] Inner\nbody/);
assert.match(parser.render('> > [!tip] Inner\n> > body'), /\[!tip\] Inner\nbody/);
assert.match(parser.render('> plain\n> quote'), /plain quote/);
assert.match(parser.render('one\ntwo'), /one two/);
assert.match(parser.render('> [!tip] Title only'), /\[!tip\] Title only<\/p>/);
});
test('task markers only turn bullet-list items into tasks, never numbered items', () => {
const ordered = parser.render('1. [x] done\n2. [ ] todo\n3. plain');
assert.doesNotMatch(ordered, /data-type|data-checked/);
assert.match(ordered, /<li>\[x\] done<\/li>\s*<li>\[ \] todo<\/li>/);
const bullets = parser.render('- [x] done\n- [ ] todo');
assert.equal([...bullets.matchAll(/data-type="taskItem"/g)].length, 2);
assert.match(bullets, /<ul data-type="taskList">/);
const nested = parser.render('1. one\n - [x] nested task');
assert.equal([...nested.matchAll(/data-type="taskItem"/g)].length, 1);
assert.doesNotMatch(nested, /<ol[^>]*data-type/);
});
+48 -1
View File
@@ -14,7 +14,7 @@ const { code } = await transformWithEsbuild(source, 'markdown.ts', {
format: 'esm',
target: 'esnext'
});
const { serializeCodeBlock, serializeInlineMarkdown } = await import(
const { joinMarkdownBlocks, serializeCodeBlock, serializeInlineMarkdown } = await import(
`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`
);
@@ -361,3 +361,50 @@ test('splits contiguous code at link boundaries without losing code text or link
'<p data-helix-code=""><code style="white-space: pre-wrap">`first</code><a href="https://example.com"><code style="white-space: pre-wrap">second`</code></a><a href="https://other.example"><code style="white-space: pre-wrap">third</code></a><code style="white-space: pre-wrap">last</code></p>\n'
);
});
const block = (text) => ({ text, isImage: false });
const image = { text: '![x](a.png)\n', isImage: true };
const emptyParagraph = { text: '', isImage: false, isEmpty: true };
test('joined blocks are separated by exactly one blank line, including after lists', () => {
assert.equal(joinMarkdownBlocks([block('one\n'), block('two\n')]), 'one\n\ntwo\n');
assert.equal(joinMarkdownBlocks([block('- a\n- b\n\n'), block('text\n')]), '- a\n- b\n\ntext\n');
assert.equal(joinMarkdownBlocks([block('- a\n\n'), block('1. b\n\n'), block('text\n')]), '- a\n\n1. b\n\ntext\n');
assert.equal(joinMarkdownBlocks([block('text\n'), block('- a\n\n')]), 'text\n\n- a\n');
});
test('empty paragraphs stay as marker lines between blocks and are dropped at the end', () => {
assert.equal(
joinMarkdownBlocks([block('a\n'), emptyParagraph, emptyParagraph, block('b\n'), emptyParagraph, emptyParagraph]),
'a\n\n<!-- -->\n<!-- -->\nb\n'
);
assert.equal(joinMarkdownBlocks([emptyParagraph]), '\n');
});
test('an empty paragraph before an image is written as a blank line that parses back to it', () => {
const gap = '<p></p>\n<p><img';
for (const before of [block('# T\n'), block('para\n'), block('- a\n\n'), block('```\ncode\n```\n')]) {
const joined = joinMarkdownBlocks([before, emptyParagraph, image, block('text\n')]);
assert.equal(joined, `${before.text.replace(/\n*$/, '\n')}\n![x](a.png)\n\ntext\n`, before.text);
assert.doesNotMatch(joined, /<!--/);
assert.ok(editorMarkdown.render(joined).includes(gap), joined);
// Saving what was just loaded must not change it.
assert.equal(joinMarkdownBlocks([before, emptyParagraph, image, block('text\n')]), joined);
}
// A second empty paragraph keeps its marker and still leaves a blank line before the image.
const twice = joinMarkdownBlocks([block('para\n'), emptyParagraph, emptyParagraph, image]);
assert.equal(twice, 'para\n\n<!-- -->\n\n![x](a.png)\n');
assert.equal(editorMarkdown.render(twice).match(/<p><\/p>/g)?.length, 2);
});
test('an image placed directly after a block stays directly after it', () => {
const joined = joinMarkdownBlocks([block('# T\n'), image]);
assert.equal(joined, '# T\n![x](a.png)\n');
assert.doesNotMatch(editorMarkdown.render(joined), /<p><\/p>/);
});
test('joining never collapses blank lines inside code blocks', () => {
const code = serializeCodeBlock('a\n\n\n\nb');
assert.equal(joinMarkdownBlocks([block(code), block('after\n')]), code + '\nafter\n');
assert.ok(joinMarkdownBlocks([block(code), block('after\n')]).includes('a\n\n\n\nb'));
});