diff --git a/src/lib/components/Editor.svelte b/src/lib/components/Editor.svelte index eb276a5..f06c872 100644 --- a/src/lib/components/Editor.svelte +++ b/src/lib/components/Editor.svelte @@ -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 diff --git a/src/lib/editor/externalNote.ts b/src/lib/editor/externalNote.ts index 838a826..11823ed 100644 --- a/src/lib/editor/externalNote.ts +++ b/src/lib/editor/externalNote.ts @@ -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 } = {}; markdown.parse(body, environment); - const tree = markdownLanguage.parser.parse(body); + const tree = editorSyntax.parse(body); const usedLabels = new Set(); const label = (node: { from: number; to: number }) => markdown.utils.normalizeReference(body.slice(node.from + 1, node.to - 1)); tree.iterate({ enter(node) { diff --git a/src/lib/editor/markdown.ts b/src/lib/editor/markdown.ts index a3d5992..f612131 100644 --- a/src/lib/editor/markdown.ts +++ b/src/lib/editor/markdown.ts @@ -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 { diff --git a/src/lib/editor/markdownParser.ts b/src/lib/editor/markdownParser.ts index c450097..c6e9aa8 100644 --- a/src/lib/editor/markdownParser.ts +++ b/src/lib/editor/markdownParser.ts @@ -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. */ diff --git a/tests/external-note-references.test.mjs b/tests/external-note-references.test.mjs index bcad398..690d4f5 100644 --- a/tests/external-note-references.test.mjs +++ b/tests/external-note-references.test.mjs @@ -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![x](my pic.png)\n\nc ` d\n\n![y](plain.png)\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('`![x](code only.png)` then ![y](real one.png)\n\n![z](next one.png)\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}![x](my pic.png)${newline}${newline}![y](other pic.png)${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 ![x](a.png)\n').map(reference => reference.href), ['a.png']); + assert.deepEqual(ranges('para\n\n\t![x](b.png)\n').map(reference => reference.href), ['b.png']); + assert.deepEqual(ranges('- item\n\n ![x](c.png)\n').map(reference => reference.href), ['c.png']); + // Fenced code stays literal, indented or not. + assert.deepEqual(ranges('```\n![x](a.png)\n```\n\n ```\n ![y](b.png)\n ```\n'), []); +}); + +test('formulas after a stray backtick no longer hide attachments inside them', () => { + const raw = 'a ` b\n\n$![x](not a file.png)$\n\n![y](real file.png)\n'; + assert.deepEqual(ranges(raw).map(reference => reference.href), ['real file.png']); +}); diff --git a/tests/markdown-roundtrip.test.mjs b/tests/markdown-roundtrip.test.mjs index 595559e..4ac32ca 100644 --- a/tests/markdown-roundtrip.test.mjs +++ b/tests/markdown-roundtrip.test.mjs @@ -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(/]*>[\s\S]*?<\/li>/)[0]; + assert.match(listItem, /
]*>const a = 1;\n\nconst b = 2;<\/code><\/pre>/, marker);
+    assert.match(listItem, /

more text<\/p>/, marker); + assert.match(listItem, /

\s*

quoted<\/p>/, marker); + assert.equal(html.match(/]/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')); +});