From ea4a5eae263544a2a9bcfbdf22230cb8efc3283f Mon Sep 17 00:00:00 2001 From: Yuri Karamian Date: Wed, 7 Oct 2026 22:25:16 +0200 Subject: [PATCH] fix: keep dollar signs, aligned images, attachment names and code spaces intact Write a literal $ as typed unless plain dollars could create, remove or re-pair an inline formula. Save portable image paths when a centered, aligned or styled block is written. Percent-encode a newly stored attachment's path before it is inserted, and keep a Windows drive letter literal in paths outside the vault. Keep whitespace-only code spans in simple list items, task items and table cells. Compare Version History in bounded time with a synchronous line diff and an edit limit. Beyond the limit, shared lines at the start and end stay unchanged and the rest is shown as removed and added. --- src/lib/components/Editor.svelte | 46 ++++++++++----- src/lib/editor/markdown.ts | 30 +++++++++- src/lib/editor/markdownParser.ts | 14 ++++- src/lib/utils/history-comparison.test.mjs | 46 +++++++++++++++ src/lib/utils/history-comparison.ts | 33 +++++++++-- src/lib/utils/paths.ts | 7 ++- tests/attachment-uri.test.mjs | 34 +++++++++++ tests/markdown-parser.test.mjs | 18 ++++++ tests/markdown-roundtrip.test.mjs | 69 ++++++++++++++++++++++- 9 files changed, 271 insertions(+), 26 deletions(-) diff --git a/src/lib/components/Editor.svelte b/src/lib/components/Editor.svelte index ed6b283..eb276a5 100644 --- a/src/lib/components/Editor.svelte +++ b/src/lib/components/Editor.svelte @@ -58,14 +58,14 @@ 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, joinMarkdownBlocks, type MarkdownBlock } from '$lib/editor/markdown'; + import { serializeInlineMarkdownKeepingDollars, 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'; import { externalNoteReferences } from '$lib/editor/externalNote'; import { tagIterationKey } from '$lib/utils/tag-styles'; import { replaceWithWikiLink } from '$lib/editor/wikiLinks'; - import { assetSourceToMarkdown, assetUrlToLocalPath, resolveFileReference, noteAssetRoot, isTrashedNote } from '$lib/utils/paths'; + import { assetSourceToMarkdown, assetUrlToLocalPath, encodeMarkdownPath, resolveFileReference, noteAssetRoot, isTrashedNote } from '$lib/utils/paths'; import GraphView from './GraphView.svelte'; import TagSuggestInput from './TagSuggestInput.svelte'; import TagLabel from './TagLabel.svelte'; @@ -117,15 +117,20 @@ } return false; } + // The parser marks blocks holding code spans so whitespace-only code survives DOM import. + const keepCodeWhitespace = { + parseHTML(this: any) { + return (this.parent?.() ?? []).flatMap((rule: any) => [ + { ...rule, tag: `${rule.tag}[data-helix-code]`, preserveWhitespace: 'full' as const }, + rule, + ]); + }, + }; const EditorStarterKit = StarterKit.extend({ addExtensions() { - return this.parent!().map(extension => extension instanceof TiptapNode && (extension.name === 'paragraph' || extension.name === 'heading') ? extension.extend({ - parseHTML() { - return (this.parent?.() ?? []).flatMap(rule => [ - { ...rule, tag: `${rule.tag}[data-helix-code]`, preserveWhitespace: 'full' as const }, - rule, - ]); - }, + return this.parent!().map(extension => extension instanceof TiptapNode && extension.name === 'listItem' ? extension.extend(keepCodeWhitespace) + : extension instanceof TiptapNode && (extension.name === 'paragraph' || extension.name === 'heading') ? extension.extend({ + ...keepCodeWhitespace, renderHTML(props) { return this.parent!({ ...props, @@ -1034,12 +1039,14 @@ } const CustomTableCell = TableCell.extend({ + ...keepCodeWhitespace, addAttributes() { return { ...this.parent?.(), ...cellColorAttributes() }; }, }); const CustomTableHeader = TableHeader.extend({ + ...keepCodeWhitespace, addAttributes() { return { ...this.parent?.(), ...cellColorAttributes() }; }, @@ -4024,6 +4031,8 @@ function serializeHTMLNode(node: ProseMirrorNode): string { const container = document.createElement('div'); container.appendChild(DOMSerializer.fromSchema(editor!.schema).serializeNode(node)); + // The editor shows images through machine-specific asset URLs; notes store portable paths. + for (const image of container.querySelectorAll('img[src]')) image.setAttribute('src', stripAssetSrc(image.getAttribute('src') ?? '')); return container.innerHTML; } @@ -4151,8 +4160,13 @@ return parts.join('\n') + '\n'; } + function inlineMathPayloads(markdown: string): string[] { + const children = mdit.parseInline(markdown, {})[0]?.children ?? []; + return children.filter(token => token.type === 'helix_math_inline').map(token => token.content); + } + function serializeInline(node: any): string { - return serializeInlineMarkdown(node, stripAssetSrc); + return serializeInlineMarkdownKeepingDollars(node, stripAssetSrc, inlineMathPayloads); } function autofocus(el: HTMLElement) { @@ -5063,7 +5077,7 @@ EditorStarterKit.configure({ code: { HTMLAttributes: { style: 'white-space: pre-wrap' } }, codeBlock: false, link: false, underline: false }), FocusedPlaceholder, TaskList, - TaskItem.configure({ nested: true }), + TaskItem.extend(keepCodeWhitespace).configure({ nested: true }), Table.configure({ resizable: true }), TableRow, CustomTableCell, @@ -6389,18 +6403,20 @@ : await saveAttachment(origin.vault!, name, data); if (!canInsert(origin)) return; const label = `${name} (${Math.round(data.length / 1024)} kB)`; + // The stored name may contain #, %, spaces or parentheses; every use below is a URI or Markdown destination. + const destination = encodeMarkdownPath(relativePath); if (origin.sourceMode) { const { from, to } = source.state.selection.main; const escaped = (kind === 'image' ? name : label).replace(/[[\]\\]/g, '\\$&'); - source.replace(`${kind === 'image' ? '!' : ''}[${escaped}](${relativePath}) `, from, to); + source.replace(`${kind === 'image' ? '!' : ''}[${escaped}](${destination}) `, from, to); } else if (editor) { // Async completion may insert into its origin, but must never take focus. - if (kind === 'image') editor.commands.setImage({ src: resolveImageSrc(relativePath) }); + if (kind === 'image') editor.commands.setImage({ src: resolveImageSrc(destination) }); else if (kind === 'pdf' && !isMobile && $appConfig?.pdf_preview) { - editor.commands.insertContent({ type: 'pdfEmbed', attrs: { src: relativePath, name } }); + editor.commands.insertContent({ type: 'pdfEmbed', attrs: { src: destination, name } }); } else { editor.commands.insertContent([ - { type: 'text', text: label, marks: [{ type: 'link', attrs: { href: relativePath } }] }, + { type: 'text', text: label, marks: [{ type: 'link', attrs: { href: destination } }] }, { type: 'text', text: ' ' }, ]); } diff --git a/src/lib/editor/markdown.ts b/src/lib/editor/markdown.ts index 8b92be5..a3d5992 100644 --- a/src/lib/editor/markdown.ts +++ b/src/lib/editor/markdown.ts @@ -149,9 +149,15 @@ function commonMarkCount(active: MarkdownMark[], next: MarkdownMark[]): number { return index; } +export interface InlineSerializeOptions { + /** Leave `$` unescaped (only backslashes before it are doubled); callers must check it cannot form math. */ + literalDollars?: boolean; +} + export function serializeInlineMarkdown( node: ProseMirrorNode, normalizeAssetSource: AssetSourceNormalizer = identityAssetSource, + options: InlineSerializeOptions = {}, ): string { let result = ''; let activeMarks: MarkdownMark[] = []; @@ -188,7 +194,9 @@ export function serializeInlineMarkdown( if (index === 0) { text = text.replace(/^[\t\u2003]+/, (whitespace) => ' '.repeat(whitespace.length)); } - text = text.replace(/\\*\$/g, (match) => '\\'.repeat((match.length - 1) * 2 + 1) + '$'); + text = options.literalDollars + ? text.replace(/\\+(?=\$)/g, (backslashes) => backslashes + backslashes) + : text.replace(/\\*\$/g, (match) => '\\'.repeat((match.length - 1) * 2 + 1) + '$'); if (index + 1 < node.childCount && node.child(index + 1).type.name === 'mathInline') { text = text.replace(/\\+$/, (backslashes) => backslashes + backslashes); } @@ -220,3 +228,23 @@ export function serializeInlineMarkdown( transitionMarks([]); return result; } + +/** + * Like serializeInlineMarkdown, but writes a `$` as typed unless that could change the note's + * math. `inlineMathPayloads` returns the TeX of each inline formula the parser finds in a string; + * if plain dollars would add, remove or re-pair a formula, every `$` is escaped as before. + */ +export function serializeInlineMarkdownKeepingDollars( + node: ProseMirrorNode, + normalizeAssetSource: AssetSourceNormalizer, + inlineMathPayloads: (markdown: string) => string[], +): string { + const escaped = serializeInlineMarkdown(node, normalizeAssetSource); + if (!escaped.includes('$')) return escaped; + const plain = serializeInlineMarkdown(node, normalizeAssetSource, { literalDollars: true }); + // A `$$` could start a display formula in a later block, so only single dollars are left alone. + if (plain.includes('$$')) return escaped; + const expected = inlineMathPayloads(escaped); + const actual = inlineMathPayloads(plain); + return actual.length === expected.length && actual.every((tex, index) => tex === expected[index]) ? plain : escaped; +} diff --git a/src/lib/editor/markdownParser.ts b/src/lib/editor/markdownParser.ts index 7fea537..c450097 100644 --- a/src/lib/editor/markdownParser.ts +++ b/src/lib/editor/markdownParser.ts @@ -257,16 +257,28 @@ export function createEditorMarkdownParser(): MarkdownIt { md.core.ruler.after('inline', 'helix_images', state => { let paragraph: Token | null = null; + let cell: Token | null = null; + // Tight list items render no

, so a simple item (open, hidden paragraph, inline, close) carries the marker. + const items: { token: Token; index: number; hasCode: boolean }[] = []; let lines: string[] | undefined; - for (const token of state.tokens) { + for (const [index, token] of state.tokens.entries()) { if (token.type === 'paragraph_open' || token.type === 'heading_open') paragraph = token; else if (token.type === 'paragraph_close' || token.type === 'heading_close') paragraph = null; + else if (token.type === 'th_open' || token.type === 'td_open') cell = token; + else if (token.type === 'th_close' || token.type === 'td_close') cell = null; + else if (token.type === 'list_item_open') items.push({ token, index, hasCode: false }); + else if (token.type === 'list_item_close') { + const item = items.pop(); + if (item?.hasCode && index - item.index === 4) item.token.attrSet('data-helix-code', ''); + } if (token.type !== 'inline') continue; for (const child of token.children ?? []) { // ProseMirror's DOM parser otherwise collapses or drops whitespace-only code spans. if (child.type === 'code_inline') { child.attrSet('style', 'white-space: pre-wrap'); paragraph?.attrSet('data-helix-code', ''); + cell?.attrSet('data-helix-code', ''); + if (paragraph?.hidden && items.length) items[items.length - 1].hasCode = true; } if (child.type === 'image') { const src = child.attrGet('src'); diff --git a/src/lib/utils/history-comparison.test.mjs b/src/lib/utils/history-comparison.test.mjs index d3138e4..08cda53 100644 --- a/src/lib/utils/history-comparison.test.mjs +++ b/src/lib/utils/history-comparison.test.mjs @@ -292,3 +292,49 @@ test('a stripped current body matches a snapshot that keeps its file whitespace' [['removed', 'body'], ['added', 'edited']], ); }); + +const numbered = (count, prefix) => Array.from({ length: count }, (_, index) => `${prefix} ${index}`).join('\n') + '\n'; + +test('a heavily rewritten note is compared quickly and still reports every change', async () => { + const started = performance.now(); + const comparison = await compare(numbered(1000, 'old line'), numbered(1000, 'new text')); + assert.ok(performance.now() - started < 1500, 'the comparison must not wait on timers per edit'); + assert.equal(comparison.added, 1000); + assert.equal(comparison.removed, 1000); +}); + +test('beyond the diff limit, shared start and end lines stay unchanged and the middle is replaced', async () => { + const head = numbered(100, 'same start'); + const tail = numbered(100, 'same end'); + const current = head + numbered(2500, 'old middle') + tail; + const snapshot = head + numbered(1800, 'new middle') + tail; + const started = performance.now(); + const comparison = await compare(current, snapshot); + assert.ok(performance.now() - started < 1500); + assert.equal(comparison.removed, 2500); + assert.equal(comparison.added, 1800); + assert.equal(rowsOf(comparison).filter((row) => row.kind === 'same').length, 200); + assert.equal(rowsOf(comparison)[0].text, 'same start 0'); + assert.equal(rowsOf(comparison).at(-1).text, 'same end 99'); +}); + +test('the coarse fallback handles CRLF, missing final newlines and empty sides', async () => { + const big = numbered(2000, 'a').replace(/\n/g, '\r\n'); + const other = numbered(2000, 'b'); + const crlf = await compare(big, other); + assert.equal(crlf.added + crlf.removed, 4000); + const noFinalNewline = await compare(numbered(1500, 'a').slice(0, -1), numbered(1500, 'b').slice(0, -1)); + assert.equal(noFinalNewline.added, 1500); + assert.equal(rowsOf(noFinalNewline).at(-1).newline, false); + const empty = await compare('', numbered(2000, 'b')); + assert.deepEqual([empty.added, empty.removed], [2000, 0]); + const emptied = await compare(numbered(2000, 'b'), ''); + assert.deepEqual([emptied.added, emptied.removed], [0, 2000]); +}); + +test('differences under the limit still use the minimal line diff', async () => { + const current = numbered(5000, 'line'); + const snapshot = current.replace('line 10\n', 'edited 10\n').replace('line 4000\n', 'edited 4000\n'); + const comparison = await compare(current, snapshot); + assert.deepEqual([comparison.added, comparison.removed], [2, 2]); +}); diff --git a/src/lib/utils/history-comparison.ts b/src/lib/utils/history-comparison.ts index 0ca8729..8abd0ff 100644 --- a/src/lib/utils/history-comparison.ts +++ b/src/lib/utils/history-comparison.ts @@ -44,17 +44,38 @@ export function comparableBody(body: string): string { return trimmed === '' ? '' : trimmed + '\n'; } +/** Diff cost grows with the square of the edit distance; beyond this, a coarse diff is shown instead. */ +const MAX_EDIT_LENGTH = 1000; + +/** Always-correct fallback: shared leading and trailing lines stay unchanged, the middle is replaced. */ +function coarseChanges(current: string, snapshot: string): Change[] { + const lines = (text: string) => text.replace(/\r\n/g, '\n').match(/[^\n]*\n|[^\n]+$/g) ?? []; + const before = lines(current); + const after = lines(snapshot); + let start = 0; + while (start < before.length && start < after.length && before[start] === after[start]) start++; + let end = 0; + while (end < before.length - start && end < after.length - start + && before[before.length - 1 - end] === after[after.length - 1 - end]) end++; + const part = (items: string[], kind: 'same' | 'added' | 'removed'): Change[] => items.length + ? [{ value: items.join(''), count: items.length, added: kind === 'added', removed: kind === 'removed' }] + : []; + return [ + ...part(before.slice(0, start), 'same'), + ...part(before.slice(start, before.length - end), 'removed'), + ...part(after.slice(start, after.length - end), 'added'), + ...part(before.slice(before.length - end), 'same'), + ]; +} + /** Added/removed rows describe restoring the snapshot over the current editor body. */ export async function compareHistory( currentBody: string, snapshotBody: string, ): Promise { - const changes = await new Promise((resolve) => { - diffLines(currentBody, snapshotBody, { - stripTrailingCr: true, - callback: resolve, - }); - }); + // jsdiff's callback mode waits on a timer for every step, which takes seconds on large changes. + const changes = diffLines(currentBody, snapshotBody, { stripTrailingCr: true, maxEditLength: MAX_EDIT_LENGTH }) + ?? coarseChanges(currentBody, snapshotBody); const chunks: HistoryChunk[] = []; let added = 0; diff --git a/src/lib/utils/paths.ts b/src/lib/utils/paths.ts index 7589c04..54a4736 100644 --- a/src/lib/utils/paths.ts +++ b/src/lib/utils/paths.ts @@ -61,8 +61,11 @@ export function resolveFileReference(source: string, notePath: string | null, va return { path: resolveVaultFilePath(path, notePath, vaultRoot), suffix: boundary < 0 ? '' : source.slice(boundary) }; } -function encodeMarkdownPath(path: string): string { - return path.replace(/\\/g, '/').split('/').map(segment => encodeURIComponent(segment) +/** Percent-encode each path segment for a Markdown destination; a Windows drive prefix stays literal. */ +export function encodeMarkdownPath(path: string): string { + const normalized = path.replace(/\\/g, '/'); + const drive = /^[A-Za-z]:(?=\/|$)/.exec(normalized)?.[0] ?? ''; + return drive + normalized.slice(drive.length).split('/').map(segment => encodeURIComponent(segment) .replace(/[!'()*]/g, character => `%${character.charCodeAt(0).toString(16).toUpperCase()}`)).join('/'); } diff --git a/tests/attachment-uri.test.mjs b/tests/attachment-uri.test.mjs index bf4c10a..38260b6 100644 --- a/tests/attachment-uri.test.mjs +++ b/tests/attachment-uri.test.mjs @@ -70,3 +70,37 @@ test('an unconfigured external note resolves its own relative assets without bor assert.equal(assetPath(f.resolve('.helixnotes/attachments/image.png')), '/external/.helixnotes/attachments/image.png'); assert.equal(assetPath(f.resolve('images/image.png')), '/external/images/image.png'); }); + +for (const name of ['Screenshot #1.png', '100% (final).png', 'a%20b #c?d.png', "café 雪 + 'x'.png"]) { + test(`a newly stored attachment ${JSON.stringify(name)} resolves to its own file and saves back unchanged`, () => { + const f = fixture(); + // The backend returns the raw stored path, which uploadAsset must encode before using it as a destination. + const stored = `.helixnotes/attachments/0b1c2d3e_${name}`; + const destination = paths.encodeMarkdownPath(stored); + assert.equal(imageSource(`![x](${destination})`), destination, 'the encoded destination parses as one image'); + const asset = f.resolve(destination); + assert.equal(assetPath(asset), `/vault/${stored}`); + assert.equal(new URL(asset).search, ''); + assert.equal(paths.assetSourceToMarkdown(asset, '/vault/Projects/Note.md', '/vault'), destination); + }); +} + +test('uploadAsset encodes the stored path before every Markdown or URI use', () => { + const start = source.indexOf('async function uploadAsset'); + const body = source.slice(start, source.indexOf('async function insertFile')); + assert.match(body, /const destination = encodeMarkdownPath\(relativePath\)/); + assert.equal(body.replace('encodeMarkdownPath(relativePath)', '').includes('relativePath)'), false); + assert.equal((body.match(/\$\{relativePath\}|src: relativePath|href: relativePath|resolveImageSrc\(relativePath\)/g) ?? []).length, 0); +}); + +test('Windows drive letters stay literal when a path outside the vault is written to Markdown', () => { + assert.equal(paths.encodeMarkdownPath('C:/Users/me/pic one.png'), 'C:/Users/me/pic%20one.png'); + assert.equal(paths.encodeMarkdownPath('C:\\Users\\me\\a#b.png'), 'C:/Users/me/a%23b.png'); + assert.equal(paths.encodeMarkdownPath('/home/me/pic one.png'), '/home/me/pic%20one.png'); + assert.equal(paths.encodeMarkdownPath('relative/a:b.png'), 'relative/a%3Ab.png'); + assert.equal( + paths.assetSourceToMarkdown('http://asset.localhost/C%3A%2FUsers%2Fme%2Fpic%20one.png', '/v/Note.md', '/v'), + 'C:/Users/me/pic%20one.png', + ); + assert.equal(paths.resolveFileReference('C:/Users/me/pic%20one.png', '/v/Note.md', '/v').path, 'C:/Users/me/pic one.png'); +}); diff --git a/tests/markdown-parser.test.mjs b/tests/markdown-parser.test.mjs index 8ae3343..47540e7 100644 --- a/tests/markdown-parser.test.mjs +++ b/tests/markdown-parser.test.mjs @@ -187,3 +187,21 @@ test('task markers only turn bullet-list items into tasks, never numbered items' assert.equal([...nested.matchAll(/data-type="taskItem"/g)].length, 1); assert.doesNotMatch(nested, /]*data-type/); }); + +test('code spans mark simple list items and table cells so whitespace-only code survives DOM import', () => { + assert.match(parser.render('- a ` ` b\n- plain'), /

  • a ]*> <\/code> b<\/li>\s*
  • plain<\/li>/); + assert.match(parser.render('1. one ` ` two'), /
  • one /); + assert.match(parser.render('- [ ] task ` ` x'), /
  • /); + assert.match(parser.render('| a | b |\n| - | - |\n| ` ` | c |'), /]*> <\/code><\/td>\s*c<\/td>/); + assert.match(parser.render('| ` ` | b |\n| - | - |\n| x | y |'), //); +}); + +test('list items with nested blocks are not marked, so no stray whitespace text appears around them', () => { + const nested = parser.render('- a `x`\n - nested `y`'); + assert.match(nested, /
  • a nested /); + assert.doesNotMatch(parser.render('- item `x`\n\n ```js\n code\n ```'), /
  • \s*

    loose /); +}); diff --git a/tests/markdown-roundtrip.test.mjs b/tests/markdown-roundtrip.test.mjs index bce8dbc..595559e 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 } = await import( +const { joinMarkdownBlocks, serializeCodeBlock, serializeInlineMarkdown, serializeInlineMarkdownKeepingDollars } = await import( `data:text/javascript;base64,${Buffer.from(code).toString('base64')}` ); @@ -408,3 +408,70 @@ test('joining never collapses blank lines inside code blocks', () => { 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')); }); + +const mathPayloads = (markdown) => (editorMarkdown.parseInline(markdown, {})[0]?.children ?? []) + .filter((token) => token.type === 'helix_math_inline').map((token) => token.content); +const keepDollars = (children) => serializeInlineMarkdownKeepingDollars(schema.node('paragraph', null, children), undefined, mathPayloads); +const mathAtom = (tex) => schema.node('mathInline', { tex }); +// What a reader of the saved Markdown sees: plain text and formulas, in order. +const readBack = (markdown) => (editorMarkdown.parseInline(markdown, {})[0]?.children ?? []).map((token) => + token.type === 'helix_math_inline' ? `\u0000${token.content}\u0000` : token.type === 'text' ? token.content : '').join(''); +const authored = (children) => children.map((child) => child.type.name === 'mathInline' ? `\u0000${child.attrs.tex}\u0000` : child.text).join(''); + +test('dollar signs that cannot form a formula are saved as typed', () => { + for (const text of ['Cost $5 and $6 total', 'Price is $5.', 'Only $', '$ spaced $', 'a$b', '5$ and 6$', 'two $5 or $10']) { + assert.equal(keepDollars([schema.text(text)]), text, text); + } +}); + +test('dollar signs that would form a formula stay escaped, so literal text is never turned into math', () => { + for (const text of ['$x$', 'cost $x$ here', '$a+b$', 'x $y$ z $w$', '$$', 'a $$ b']) { + const saved = keepDollars([schema.text(text)]); + assert.deepEqual(mathPayloads(saved), [], `${text} -> ${saved}`); + assert.equal(readBack(saved), text, `${text} -> ${saved}`); + } +}); + +test('real formulas keep their dollars and are not re-paired with neighbouring text', () => { + const nodes = [schema.text('cost $5 and '), mathAtom('x'), schema.text(' then $6')]; + const saved = keepDollars(nodes); + assert.deepEqual(mathPayloads(saved), ['x']); + assert.equal(readBack(saved), authored(nodes)); + const pairing = [schema.text('$a'), mathAtom('x'), schema.text('b')]; + const pairingSaved = keepDollars(pairing); + assert.deepEqual(mathPayloads(pairingSaved), ['x'], pairingSaved); + assert.equal(readBack(pairingSaved), authored(pairing)); +}); + +test('backslashes before an unescaped dollar are doubled so the dollar stays literal', () => { + for (const text of ['a\\$5', 'a\\\\$5', 'path\\ $5 and $6']) { + const saved = keepDollars([schema.text(text)]); + assert.equal(readBack(saved), text, `${text} -> ${saved}`); + } +}); + +test('fuzz: saving text and formulas keeps exactly what was written, whatever the dollars do', () => { + const alphabet = ['a', 'b', '1', '5', ' ', '$', '$', '\\', '*', '_', '`', 'x', '.', '\n'.slice(0, 0) || ' ']; + let seed = 12345; + const random = (n) => { seed = (seed * 1103515245 + 12345) & 0x7fffffff; return seed % n; }; + for (let round = 0; round < 4000; round++) { + const children = []; + for (let part = 0, count = 1 + random(4); part < count; part++) { + if (random(5) === 0) children.push(mathAtom(['x', 'a+b', '5', 'y z'][random(4)])); + else { + const text = Array.from({ length: 1 + random(8) }, () => alphabet[random(alphabet.length)]).join(''); + if (text.length) children.push(schema.text(text)); + } + } + // Adjacent text nodes would merge; adjacent atoms are covered by the serializer's own encoding rules. + const merged = children.reduce((list, child) => { + const last = list[list.length - 1]; + if (last?.isText && child.isText) list[list.length - 1] = schema.text(last.text + child.text); else list.push(child); + return list; + }, []); + if (!merged.length) continue; + const saved = keepDollars(merged); + const baseline = serializeInlineMarkdown(schema.node('paragraph', null, merged)); + assert.equal(readBack(saved), readBack(baseline), JSON.stringify({ saved, baseline, merged: authored(merged) })); + } +});