import assert from 'node:assert/strict'; import test from 'node:test'; import { createEditorMarkdownParser, displayMathRanges, stripLinkTitle } from '../src/lib/editor/markdownParser.ts'; const parser = createEditorMarkdownParser(); const payload = '$x$\n$$\na+b\n$$\n[[Note]]\n[label](path with spaces)\n![image]( "title")\n- [x] task\n\n
'; const escapedCode = (html) => html.match(/
]*>([\s\S]*?)<\/code><\/pre>/)?.[1];
const unescape = (html) => html.replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/&/g, '&');
const mathPayloads = (html) => [...html.matchAll(/data-math-(?:inline|block)="([^"]*)"/g)].map(match => decodeURIComponent(unescape(match[1])));

for (const [name, opening, closing] of [
  ['backtick', '```text', '```'], ['tilde', '~~~text', '~~~'],
  ['long backtick', '````text', '````'], ['indented fence', '   ~~~text', '   ~~~'],
]) {
  test(`${name} fences preserve all literal syntax`, () => {
    const html = parser.render(`${opening}\n${payload}\n${closing}`, { wikiLinkTitles: [{ path: 'Note.md', title: 'Note' }] });
    assert.equal(unescape(escapedCode(html)), payload);
    assert.equal(mathPayloads(html).length, 0);
    assert.doesNotMatch(html, /]*data-wiki-link|]*data-checked|]*data-secret-block/);
  });
}

test('shorter and mismatched fences remain code; following math still renders', () => {
  const code = '```\n~~~\n$x$';
  const html = parser.render('````text\n' + code + '\n````\n\nActual $y$.');
  assert.equal(unescape(escapedCode(html)), code);
  assert.deepEqual(mathPayloads(html), ['y']);
});

test('fences interrupt prose without needing a blank line', () => {
  const html = parser.render('Prose\n```text\n$x$\n```\nMath $y$.');
  assert.equal(unescape(escapedCode(html)), '$x$');
  assert.deepEqual(mathPayloads(html), ['y']);
});

test('quoted and listed fences protect their contents', () => {
  const quoted = parser.render('> ~~~text\n> $x$\n> [[Note]]\n> ~~~', { wikiLinkTitles: [] });
  assert.equal(unescape(escapedCode(quoted)), '$x$\n[[Note]]');
  const listed = parser.render('- item\n\n  ```text\n  $x$\n  \n  ```');
  assert.equal(unescape(escapedCode(listed)), '$x$\n');
});

test('unclosed fences keep their payload literal', () => {
  const html = parser.render('~~~text\n$x$\n');
  assert.equal(unescape(escapedCode(html)), '$x$\n');
  assert.deepEqual(mathPayloads(html), []);
});

test('fence rendering preserves leading, internal and extra trailing blank lines', () => {
  const code = '\nfirst\n\n\nlast\n\n';
  const html = parser.render('```text\n' + code + '\n```');
  assert.equal(unescape(escapedCode(html)), code);
});

test('inline and multiline code protect math, wiki and legacy links', () => {
  const html = parser.render('`` $x$ [[Note]] [a](a b) ` `` and `line\n$x$`', { wikiLinkTitles: [] });
  assert.match(html, /]*>\$x\$ \[\[Note\]\] \[a\]\(a b\) `<\/code>/);
  assert.match(html, /]*>line \$x\$<\/code>/);
  assert.deepEqual(mathPayloads(html), []);
});

test('escaped dollars stay prose while real math retains exact escaped-dollar and backtick TeX', () => {
  const html = parser.render('Escaped \\$x$ and $a\\$b$ and $`raw`$; $10 to $50 and $ x$ stay prose.');
  assert.deepEqual(mathPayloads(html), ['a\\$b', '`raw`']);
  assert.match(html, /Escaped \$x\$/);
  assert.match(html, /\$10 to \$50/);
});

test('math payloads are consumed before wiki, link and task transformations', () => {
  const tex = '[[Note]]\n[label](path with spaces)\n```\n- [x] TeX';
  const html = parser.render('$$\n' + tex + '\n$$\n\n$x+y$', { wikiLinkTitles: [] });
  assert.deepEqual(mathPayloads(html), [tex, 'x+y']);
  assert.doesNotMatch(html, /data-wiki-link|data-checked|
/);
});

test('unclosed display math stays literal and normal fenced indentation convention stays unchanged', () => {
  assert.deepEqual(mathPayloads(parser.render('$$\nunclosed')), []);
  assert.doesNotMatch(parser.render('\tordinary tab-indented text'), /
/);
});

test('semantic math ranges do not mistake fenced or inline code for display math', () => {
  const source = '~~~text\n$$\n# code\n$$\n~~~\n\n> $$\n> x\n> ---\n> $$\n\n# Real';
  const ranges = displayMathRanges(source);
  assert.equal(ranges.length, 1);
  assert.equal(source.slice(ranges[0].from, ranges[0].to), '> $$\n> x\n> ---\n> $$\n');
  assert.deepEqual(displayMathRanges('```text\n$$\n```\n\n# Real'), []);
});

test('wiki lookup preserves path precedence, shallowest title and escaped attributes', () => {
  const env = { wikiLinkTitles: [
    { path: 'deep/nested/Note.md', title: 'Note' }, { path: 'folder/Note.md', title: 'NOTE' },
    { path: 'other/Note.md', title: 'Note' }, { path: 'Quoted".md', title: 'Quoted"' },
  ] };
  const html = parser.render('[[Note]] [[deep/nested/Note#Heading|Alias]] [[Quoted"]] [[Missing]]', env);
  assert.match(html, /data-path="folder\/Note\.md"/);
  assert.match(html, /data-path="deep\/nested\/Note\.md" data-title="deep\/nested\/Note#Heading"[^>]*>Alias<\/span>/);
  assert.match(html, /data-path="Quoted"\.md" data-title="Quoted""/);
  assert.match(html, /data-path="" data-title="Missing"/);
  assert.doesNotMatch(parser.render('\\[[Note]]', env), /data-wiki-link/);
  assert.doesNotMatch(parser.render('[[Note]]'), /data-wiki-link/);
});

test('wiki lookup reads titles once per titles array and refreshes when the array is replaced', () => {
  let reads = 0;
  const notes = Array.from({ length: 250 }, (_, index) => ({
    path: `folder/Note ${index}.md`, get title() { reads++; return `Note ${index}`; },
  }));
  const env = { wikiLinkTitles: notes };
  const source = '[[Note 249]] '.repeat(100);
  parser.render(source, env);
  parser.render(source, env);
  assert.equal(reads, notes.length);
  const updated = notes.slice();
  updated[249] = { path: 'Moved.md', title: 'Note 249' };
  assert.match(parser.render(source, { wikiLinkTitles: updated }), /data-path="Moved\.md"/);
});

test('ordinary and legacy links/images remain functional while unsafe schemes remain rejected', () => {
  const html = parser.render('[a]( "title") [b](path with spaces) [c](path%20"title") ![d](%20"title") ![e](///home/user/image.png)');
  assert.match(html, /href="path%20with%20spaces"/);
  assert.match(html, /href="path"/);
  assert.match(html, /src="image%20path"/);
  assert.match(html, /src="\/home\/user\/image\.png"/);
  // Links and images keep their titles, legacy destinations included; a link without one gets none.
  assert.match(html, /a<\/a>/);
  assert.match(html, /b<\/a>/);
  assert.match(html, /c<\/a>/);
  assert.match(html, /d/);
  assert.match(html, /e/);
  assert.doesNotMatch(parser.render('[bad](javascript:alert with spaces) ![bad](data:text/html;base64,foo bar)'), /href=|src=/);
});

test('compatibility destinations retain quoted parentheses and native nested URLs', () => {
  const html = parser.render('[a](path with spaces "a (title)") [b]( (title)) [c](path(one))');
  assert.match(html, /href="path%20with%20spaces"/);
  assert.match(html, /href="image%20path"/);
  assert.match(html, /href="path\(one\)"/);
});

test('task and empty-paragraph handling uses real block structure, including nested and empty tasks', () => {
  const html = parser.render('- [x] **done**\n- [ ]\n  - [ ] nested\n\n\n# Next');
  assert.equal([...html.matchAll(/data-type="taskList"/g)].length, 2);
  assert.equal([...html.matchAll(/data-type="taskItem"/g)].length, 3);
  assert.match(html, /data-checked="true"[^>]*>[\s\S]*done<\/strong>/);
  assert.match(html, /

<\/p>\n

Next<\/h1>/); assert.doesNotMatch(parser.render('`- [x] not a task` and ``'), /data-checked|

<\/p>/); }); test('image-only gaps remain outside code and do not alter inline images', () => { assert.match(parser.render('Text\n\n![a](image.png)'), /

Text<\/p>\n

<\/p>\n

<\/p>/); }); test('only actual closed secret fences become secret blocks, not literal examples', () => { assert.match(parser.render('```helix-secret\nexample-payload\n```'), /data-secret-block="example-payload"/); const literal = '```helix-secret\nexample-payload\n```'; assert.equal(unescape(escapedCode(parser.render('````text\n' + literal + '\n````'))), literal); assert.doesNotMatch(parser.render('```helix-secret\nunclosed'), /data-secret-block/); }); test('invalid final secret-fence markers remain literal code', () => { for (const closing of [' ```', '\t```', '```\u00a0']) { const html = parser.render('```helix-secret\nexample-payload\n' + closing); assert.doesNotMatch(html, /data-secret-block/); assert.equal(unescape(escapedCode(html)), 'example-payload\n' + closing); } }); test('code whitespace is marked for DOM import without changing soft or hard line-break semantics', () => { const html = parser.render('# ` `\n\nbefore `trailing `\nnext \nlast'); assert.match(html, /

/); assert.match(html, /

/); assert.match(html, /<\/code> next
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, /

  • \[x\] done<\/li>\s*
  • \[ \] todo<\/li>/); const bullets = parser.render('- [x] done\n- [ ] todo'); assert.equal([...bullets.matchAll(/data-type="taskItem"/g)].length, 2); assert.match(bullets, /
      /); const nested = parser.render('1. one\n - [x] nested task'); 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 /); }); test('stripLinkTitle behaves exactly like the title expression it replaced, on every short combination', () => { const oracle = (destination) => destination.replace(/(?:\s|%20)+(?:"[^"]*"|'[^']*')(?:\s|%20)*$/u, '').trim(); const tokens = ['%20', ' ', '"', "'", 'a', '\t', '%2', '<']; let checked = 0; const walk = (prefix, depth) => { const input = prefix.trim(); // the parser trims the destination first assert.equal(stripLinkTitle(input), oracle(input), JSON.stringify(input)); checked++; if (depth === 0) return; for (const token of tokens) walk(prefix + token, depth - 1); }; walk('', 6); assert.ok(checked > 200000); for (const title of ['page.png "Caption"', "page.png 'Caption'", 'page.png%20"Caption"', 'a "b" "c"', 'a"b"', 'x "it\'s"%20%20']) { assert.equal(stripLinkTitle(title), oracle(title), title); } }); test('a link destination with a very long run of spaces is parsed in linear time', () => { for (const spaces of [5000, 40000]) { const started = performance.now(); parser.render('[a](x' + ' '.repeat(spaces) + 'y)'); assert.ok(performance.now() - started < 300, `${spaces} spaces took ${Math.round(performance.now() - started)} ms`); } // Titles still work next to long whitespace. assert.match(parser.render('![a](images/photo one.png "Caption")'), /src="images\/photo%20one.png"/); }); test('a line with thousands of unclosed link starts is parsed in linear time', () => { // Each start used to read to the end of the line looking for its `)`: about 1 s at 4000 starts. for (const unit of ['![a](x.png "a ', '![a](x.png (t ', '[a](x.png "a ', '![a]( ']) { for (const closer of ['', ')']) { const started = performance.now(); parser.render(unit.repeat(8000) + closer); assert.ok(performance.now() - started < 400, `${JSON.stringify(unit)} x 8000${closer} took ${Math.round(performance.now() - started)} ms`); } } // Legacy links still resolve after many failed starts on the same line, and on later lines. const noise = '[a]( '.repeat(500); assert.match(parser.render(noise + '[doc](my file.md) and ![pic](photo one.png "Caption")'), /href="my%20file.md".*src="photo%20one.png"/s); assert.match(parser.render(noise + '\n[doc](my file.md)'), /href="my%20file.md"/); }); test('angle-bracket text that is not HTML stays text', () => { assert.equal(parser.render('returns List or Map here'), '

      returns List<String> or Map<K,V> here

      \n'); assert.equal(parser.render('Run `x` as then '), '

      Run x as <username> then <my-tag a="1">

      \n'); // A placeholder alone on a line would otherwise be an HTML block. assert.equal(parser.render('\n\nafter'), '

      <GroupName>

      \n

      after

      \n'); assert.equal(parser.render('\ntext \n'), '

      <Foo>
      text
      </Foo>

      \n'); }); test('real HTML elements are still HTML', () => { assert.equal(parser.render('a bold and
      and K'), '

      a bold and
      and K

      \n'); assert.equal(parser.render('
      there
      '), '
      there
      '); assert.equal(parser.render('
      S\n\nbody\n\n
      '), '
      S\n

      body

      \n
      '); }); test('HTML comments are kept as text, the blank-line marker is not', () => { assert.equal(parser.render('a b'), '

      a <!-- note to self --> b

      \n'); assert.equal(parser.render('before\n\n\n\nafter'), '

      before

      \n

      <!--
      line one
      line two
      -->

      \n

      after

      \n'); assert.equal(parser.render('one\n\n\n\ntwo'), '

      one

      \n

      \n

      two

      \n'); assert.equal(parser.render('x y'), '

      x y

      \n'); }); test('an empty paragraph marker glued to an image by older versions still shows the image', () => { const html = parser.render('Before\n\n![](a.png)\n![](b.png)\n\nAfter'); assert.match(html, /

      <\/p>/); assert.equal((html.match(/After<\/p>/); const indented = parser.render(' ![](a.png)'); assert.equal((indented.match(//); // Anything else after the marker is still raw HTML, and code keeps the line as written. assert.match(parser.render('text'), /^text/); assert.equal(unescape(escapedCode(parser.render('```\n![](a.png)\n```'))), '![](a.png)'); });