mirror of
https://gitlab.com/ArkHost/HelixNotes.git
synced 2026-11-19 10:38:20 +01:00
fix: keep list blocks, folded callouts and opened notes intact; speed up saving
Keep a second paragraph, code block or quote inside its list item when a note is saved, indented under the item. Import attachment links that follow a stray backtick and images indented by four spaces. Keep a folded callout that was revealed from the Outline open while it is edited. Stop treating the editor's first normalization of a freshly loaded note as an edit, so opening a note no longer saves or reformats it. Join serialized blocks in linear time, so autosaving a large note no longer freezes the page.
This commit is contained in:
1 parent
ea4a5eae26
commit
4aa72e4c76
6 files changed
+180
-30
No files matched your search
@@ -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 { serializeInlineMarkdownKeepingDollars, serializeCodeBlock, joinMarkdownBlocks, type MarkdownBlock } from '$lib/editor/markdown';
|
||||
import { serializeInlineMarkdownKeepingDollars, serializeCodeBlock, joinMarkdownBlocks, joinListItemBlocks, type ListItemBlock, 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';
|
||||
@@ -578,7 +578,11 @@
|
||||
if (element instanceof HTMLElement) {
|
||||
for (let parent = element.parentElement; parent && parent !== editor.view.dom; parent = parent.parentElement) {
|
||||
if (parent.matches('[data-type="details"]')) openDetailsEl(parent);
|
||||
else if (parent.matches('.callout.is-folded')) parent.classList.remove('is-folded');
|
||||
else if (parent.matches('.callout.is-folded')) {
|
||||
// Reveal without changing the saved fold state; the node view keeps it open while it lives.
|
||||
parent.setAttribute('data-revealed', '');
|
||||
parent.classList.remove('is-folded');
|
||||
}
|
||||
}
|
||||
}
|
||||
editor.commands.setTextSelection(target.pos + 1);
|
||||
@@ -1348,7 +1352,7 @@
|
||||
const apply = (n: any) => {
|
||||
dom.className = 'callout';
|
||||
dom.classList.toggle('is-foldable', !!n.attrs.foldable);
|
||||
dom.classList.toggle('is-folded', !!n.attrs.folded);
|
||||
dom.classList.toggle('is-folded', !!n.attrs.folded && !dom.hasAttribute('data-revealed'));
|
||||
dom.setAttribute('data-callout', n.attrs.type || 'note');
|
||||
dom.setAttribute('data-callout-group', calloutGroup(n.attrs.type));
|
||||
};
|
||||
@@ -1404,9 +1408,13 @@
|
||||
foldBtn.addEventListener('mousedown', (e) => e.preventDefault());
|
||||
foldBtn.addEventListener('click', (e) => {
|
||||
e.preventDefault();
|
||||
const nowFolded = !dom.classList.contains('is-folded');
|
||||
// A callout revealed from the Outline is open on screen but still folded in the note.
|
||||
const revealed = dom.hasAttribute('data-revealed');
|
||||
dom.removeAttribute('data-revealed');
|
||||
const nowFolded = revealed || !dom.classList.contains('is-folded');
|
||||
if (editor.isEditable) {
|
||||
updateAttr({ folded: nowFolded, foldable: true });
|
||||
if (revealed) dom.classList.add('is-folded');
|
||||
} else {
|
||||
dom.classList.toggle('is-folded', nowFolded);
|
||||
dom.classList.add('is-foldable');
|
||||
@@ -4143,21 +4151,15 @@
|
||||
}
|
||||
|
||||
function serializeListItem(node: any): string {
|
||||
const parts: string[] = [];
|
||||
node.forEach((child: any) => {
|
||||
if (child.type.name === 'paragraph') {
|
||||
parts.push(serializeInline(child));
|
||||
} else if (child.type.name === 'bulletList' || child.type.name === 'orderedList' || child.type.name === 'taskList') {
|
||||
// Indent nested lists so markdown parsers recognize nesting
|
||||
// Use 4 spaces - works for both bullet (- ) and ordered (1. ) parent markers
|
||||
const nested = serializeNode(child).replace(/\n$/, '');
|
||||
const indented = nested.split('\n').map((line: string) => ' ' + line).join('\n');
|
||||
parts.push(indented);
|
||||
} else {
|
||||
parts.push(serializeNode(child));
|
||||
}
|
||||
const blocks: ListItemBlock[] = [];
|
||||
node.forEach((child: any, _offset: number, index: number) => {
|
||||
const isList = child.type.name === 'bulletList' || child.type.name === 'orderedList' || child.type.name === 'taskList';
|
||||
blocks.push({
|
||||
isList,
|
||||
text: index === 0 && child.type.name === 'paragraph' ? serializeInline(child) : serializeNode(child),
|
||||
});
|
||||
});
|
||||
return parts.join('\n') + '\n';
|
||||
return joinListItemBlocks(blocks);
|
||||
}
|
||||
|
||||
function inlineMathPayloads(markdown: string): string[] {
|
||||
@@ -5370,6 +5372,13 @@
|
||||
if (showInfo) scheduleCounts();
|
||||
},
|
||||
});
|
||||
// TipTap normalizes a newly created document on its first transaction (it adds a paragraph
|
||||
// after a final list, for example). Run that now so it is not taken for the user's first
|
||||
// edit, which would save, and possibly reformat, a note that was only opened.
|
||||
ignoreNextUpdate = true;
|
||||
editor.view.dispatch(editor.state.tr);
|
||||
ignoreNextUpdate = false;
|
||||
clearEditorHistory();
|
||||
editorReady = true;
|
||||
if (showOutline) updateOutline();
|
||||
// Pre-load note titles for wiki-link autocomplete
|
||||
|
||||
@@ -11,6 +11,10 @@ export interface ExternalNoteReference {
|
||||
}
|
||||
|
||||
const markdown = createEditorMarkdownParser();
|
||||
// The editor never treats indented text as a code block, so an indented image is still an image here.
|
||||
const editorSyntax = (markdownLanguage.parser as unknown as {
|
||||
configure(spec: { remove: string[] }): typeof markdownLanguage.parser;
|
||||
}).configure({ remove: ['IndentedCode'] });
|
||||
|
||||
/** Byte ranges for local attachment destinations, without reserializing the user's Markdown. */
|
||||
export function externalNoteReferences(raw: string): ExternalNoteReference[] {
|
||||
@@ -22,7 +26,7 @@ export function externalNoteReferences(raw: string): ExternalNoteReference[] {
|
||||
const math = [...displayMathRanges(body), ...inlineMathRanges(body)];
|
||||
const environment: { references?: Record<string, { href: string }> } = {};
|
||||
markdown.parse(body, environment);
|
||||
const tree = markdownLanguage.parser.parse(body);
|
||||
const tree = editorSyntax.parse(body);
|
||||
const usedLabels = new Set<string>();
|
||||
const label = (node: { from: number; to: number }) => markdown.utils.normalizeReference(body.slice(node.from + 1, node.to - 1));
|
||||
tree.iterate({ enter(node) {
|
||||
|
||||
@@ -36,17 +36,52 @@ const EMPTY_PARAGRAPH_MARKER = '<!-- -->';
|
||||
export function joinMarkdownBlocks(blocks: readonly MarkdownBlock[]): string {
|
||||
const entries = [...blocks];
|
||||
while (entries.length > 0 && entries[entries.length - 1].isEmpty) entries.pop();
|
||||
let result = '';
|
||||
const parts: string[] = [];
|
||||
// Only the last two characters decide the separator. Reading them from the growing result would
|
||||
// flatten it on every block, which makes joining a large note quadratic.
|
||||
let tail = '';
|
||||
const append = (piece: string) => {
|
||||
parts.push(piece);
|
||||
tail = (tail + piece).slice(-2);
|
||||
};
|
||||
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';
|
||||
if (index > 0 && tail !== '\n\n') append(tail.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;
|
||||
if (index === 0) append(text);
|
||||
else append((entry.isImage || tail === '\n\n' ? '' : '\n') + text);
|
||||
});
|
||||
return result.trim() + '\n';
|
||||
return parts.join('').trim() + '\n';
|
||||
}
|
||||
|
||||
export interface ListItemBlock {
|
||||
text: string;
|
||||
/** A nested bullet, numbered or task list. */
|
||||
isList: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Join the serialized children of one list item. The first block follows the list marker; a nested
|
||||
* list is indented four spaces directly under it; any other block (a further paragraph, a code
|
||||
* block, a quote) is separated by a blank line and indented the same way so it stays in the item.
|
||||
*/
|
||||
export function joinListItemBlocks(blocks: readonly ListItemBlock[]): string {
|
||||
const parts: string[] = [];
|
||||
blocks.forEach((block, index) => {
|
||||
if (block.isList) {
|
||||
parts.push(block.text.replace(/\n$/, '').split('\n').map((line) => ' ' + line).join('\n'));
|
||||
} else if (index === 0) {
|
||||
parts.push(block.text);
|
||||
} else {
|
||||
const body = block.text.replace(/\n+$/, '');
|
||||
if (body.trim() === '') return;
|
||||
parts.push('', body.split('\n').map((line) => (line ? ' ' + line : line)).join('\n'));
|
||||
}
|
||||
});
|
||||
return parts.join('\n') + '\n';
|
||||
}
|
||||
|
||||
export function serializeCodeBlock(code: string, language: string = ''): string {
|
||||
|
||||
@@ -342,20 +342,50 @@ const mathBlocks = MarkdownIt({ html: true });
|
||||
installMath(mathBlocks);
|
||||
mathBlocks.inline.ruler.before('link', 'helix_legacy_link', legacyLink);
|
||||
|
||||
/**
|
||||
* Inline tokens of each blank-line-separated block, parsed on its own like the editor does, with
|
||||
* source ranges relative to the whole note. Inline syntax such as a code span cannot pair across
|
||||
* blocks, so one stray backtick must not hide the links and formulas after it.
|
||||
*/
|
||||
function inlineTokensByBlock(source: string): Token[] {
|
||||
const tokens: Token[] = [];
|
||||
let start = 0;
|
||||
let offset = 0;
|
||||
const flush = (end: number) => {
|
||||
if (end <= start) return;
|
||||
const found: Token[] = [];
|
||||
mathBlocks.inline.parse(source.slice(start, end), mathBlocks, {}, found);
|
||||
for (const token of found) {
|
||||
if (token.meta?.sourceHref) {
|
||||
const { from, to } = token.meta.sourceHref;
|
||||
token.meta = { ...token.meta, sourceHref: { from: from + start, to: to + start } };
|
||||
} else if (token.type === 'helix_math_inline') {
|
||||
token.meta = { ...token.meta, from: token.meta.from + start, to: token.meta.to + start };
|
||||
}
|
||||
tokens.push(token);
|
||||
}
|
||||
};
|
||||
for (const line of source.split('\n')) {
|
||||
if (/^[ \t\r]*$/.test(line)) {
|
||||
flush(offset);
|
||||
start = offset + line.length + 1;
|
||||
}
|
||||
offset += line.length + 1;
|
||||
}
|
||||
flush(source.length);
|
||||
return tokens;
|
||||
}
|
||||
|
||||
/** Source destinations for the editor's legacy space/title link compatibility. */
|
||||
export function legacyLinkRanges(source: string): { from: number; to: number }[] {
|
||||
if (!source.includes('](')) return [];
|
||||
const tokens: Token[] = [];
|
||||
mathBlocks.inline.parse(source, mathBlocks, {}, tokens);
|
||||
return tokens.filter(token => token.meta?.sourceHref).map(token => token.meta.sourceHref);
|
||||
return inlineTokensByBlock(source).filter(token => token.meta?.sourceHref).map(token => token.meta.sourceHref);
|
||||
}
|
||||
|
||||
/** Inline math coordinates use the same delimiter and escape rules as rendering. */
|
||||
export function inlineMathRanges(source: string): { from: number; to: number }[] {
|
||||
if (!source.includes('$')) return [];
|
||||
const tokens: Token[] = [];
|
||||
mathBlocks.inline.parse(source, mathBlocks, {}, tokens);
|
||||
return tokens.filter(token => token.type === 'helix_math_inline').map(token => token.meta);
|
||||
return inlineTokensByBlock(source).filter(token => token.type === 'helix_math_inline').map(token => token.meta);
|
||||
}
|
||||
|
||||
/** Source coordinates of semantic display-math blocks, including nested containers. */
|
||||
|
||||
@@ -80,3 +80,34 @@ test('escaped Markdown paths are decoded without consuming code examples or link
|
||||
assert.equal(ranges(raw)[0].href, 'files/report(1).pdf');
|
||||
assert.equal(ranges(raw)[0].original, 'files/report\\(1\\).pdf');
|
||||
});
|
||||
|
||||
test('a stray backtick in one paragraph does not hide attachments in later paragraphs', () => {
|
||||
const raw = 'a ` b\n\n\n\nc ` d\n\n\n';
|
||||
assert.deepEqual(ranges(raw).map(reference => [reference.href, reference.original]), [
|
||||
['my pic.png', 'my pic.png'], ['plain.png', 'plain.png']
|
||||
]);
|
||||
// Real code spans still hide what they contain, and only within their own paragraph.
|
||||
assert.deepEqual(ranges('`` then \n\n\n').map(reference => reference.href),
|
||||
['real one.png', 'next one.png']);
|
||||
});
|
||||
|
||||
test('byte offsets stay correct for legacy links after other blocks, blank lines and CRLF', () => {
|
||||
for (const newline of ['\n', '\r\n']) {
|
||||
const raw = `# Héllo${newline}${newline}text ${newline}${newline} ${newline}${newline}${newline}${newline}`;
|
||||
const found = ranges(raw);
|
||||
assert.deepEqual(found.map(reference => reference.original), ['my pic.png', 'other pic.png'], JSON.stringify(newline));
|
||||
}
|
||||
});
|
||||
|
||||
test('indented images are imported because the editor never treats indented text as code', () => {
|
||||
assert.deepEqual(ranges('para\n\n \n').map(reference => reference.href), ['a.png']);
|
||||
assert.deepEqual(ranges('para\n\n\t\n').map(reference => reference.href), ['b.png']);
|
||||
assert.deepEqual(ranges('- item\n\n \n').map(reference => reference.href), ['c.png']);
|
||||
// Fenced code stays literal, indented or not.
|
||||
assert.deepEqual(ranges('```\n\n```\n\n ```\n \n ```\n'), []);
|
||||
});
|
||||
|
||||
test('formulas after a stray backtick no longer hide attachments inside them', () => {
|
||||
const raw = 'a ` b\n\n$$\n\n\n';
|
||||
assert.deepEqual(ranges(raw).map(reference => reference.href), ['real file.png']);
|
||||
});
|
||||
@@ -14,7 +14,7 @@ const { code } = await transformWithEsbuild(source, 'markdown.ts', {
|
||||
format: 'esm',
|
||||
target: 'esnext'
|
||||
});
|
||||
const { joinMarkdownBlocks, serializeCodeBlock, serializeInlineMarkdown, serializeInlineMarkdownKeepingDollars } = await import(
|
||||
const { joinListItemBlocks, joinMarkdownBlocks, serializeCodeBlock, serializeInlineMarkdown, serializeInlineMarkdownKeepingDollars } = await import(
|
||||
`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`
|
||||
);
|
||||
|
||||
@@ -475,3 +475,44 @@ test('fuzz: saving text and formulas keeps exactly what was written, whatever th
|
||||
assert.equal(readBack(saved), readBack(baseline), JSON.stringify({ saved, baseline, merged: authored(merged) }));
|
||||
}
|
||||
});
|
||||
|
||||
const item = (text, isList = false) => ({ text, isList });
|
||||
|
||||
test('blocks after a list item\'s first paragraph stay inside the item, separated by a blank line', () => {
|
||||
assert.equal(joinListItemBlocks([item('only')]), 'only\n');
|
||||
assert.equal(joinListItemBlocks([item('first'), item('second\n')]), 'first\n\n second\n');
|
||||
assert.equal(
|
||||
joinListItemBlocks([item('step'), item('```sh\nrun\n\n\nagain\n```\n'), item('after\n')]),
|
||||
'step\n\n ```sh\n run\n\n\n again\n ```\n\n after\n',
|
||||
);
|
||||
assert.equal(joinListItemBlocks([item('quoted'), item('> a\n>\n> b\n')]), 'quoted\n\n > a\n >\n > b\n');
|
||||
assert.equal(joinListItemBlocks([item('first'), item('\n'), item(' \n'), item('real\n')]), 'first\n\n real\n');
|
||||
});
|
||||
|
||||
test('nested lists keep their direct four-space indentation under the item', () => {
|
||||
assert.equal(joinListItemBlocks([item('a'), item('- b\n', true)]), 'a\n - b\n');
|
||||
assert.equal(joinListItemBlocks([item('a'), item('- b\n', true), item('```\nc\n```\n')]), 'a\n - b\n\n ```\n c\n ```\n');
|
||||
});
|
||||
|
||||
test('joined list items parse back with their code, paragraphs and quotes still inside the item', () => {
|
||||
for (const marker of ['- ', '1. ', '10. ', '- [ ] ']) {
|
||||
const text = marker + joinListItemBlocks([item('step'), item('```js\nconst a = 1;\n\nconst b = 2;\n```\n'), item('more text\n'), item('> quoted\n')]);
|
||||
const html = editorMarkdown.render(text + '- next\n');
|
||||
const listItem = html.match(/<li[^>]*>[\s\S]*?<\/li>/)[0];
|
||||
assert.match(listItem, /<pre><code[^>]*>const a = 1;\n\nconst b = 2;<\/code><\/pre>/, marker);
|
||||
assert.match(listItem, /<p>more text<\/p>/, marker);
|
||||
assert.match(listItem, /<blockquote>\s*<p>quoted<\/p>/, marker);
|
||||
assert.equal(html.match(/<li[ >]/g).length, 2, `${marker}: the following item is a separate item`);
|
||||
}
|
||||
});
|
||||
|
||||
test('joining a very large note takes linear time', () => {
|
||||
const blocks = Array.from({ length: 30000 }, (_, index) => block(`Paragraph ${index} with some words in it.\n`));
|
||||
for (let index = 0; index < blocks.length; index += 200) blocks[index] = block('- a\n- b\n\n');
|
||||
const started = performance.now();
|
||||
const joined = joinMarkdownBlocks(blocks);
|
||||
assert.ok(performance.now() - started < 500, 'building the note must not re-read the growing text for every block');
|
||||
assert.ok(joined.startsWith('- a\n- b\n\nParagraph 1 with'));
|
||||
assert.equal(joined.endsWith('Paragraph 29999 with some words in it.\n'), true);
|
||||
assert.ok(!joined.includes('\n\n\n'));
|
||||
});
|
||||
Reference in new issue
Block a user