mirror of
https://gitlab.com/ArkHost/HelixNotes.git
synced 2026-11-18 10:28:20 +01:00
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.
114 lines
6.7 KiB
JavaScript
114 lines
6.7 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import { externalNoteReferences } from '../src/lib/editor/externalNote.ts';
|
|
|
|
const ranges = raw => externalNoteReferences(raw).map(reference => ({
|
|
...reference, original: Buffer.from(raw).subarray(reference.from, reference.to).toString('utf8')
|
|
}));
|
|
|
|
test('finds Markdown attachment destinations without changing labels, titles, angles, or fragments', () => {
|
|
const raw = '\n[PDF](<files/report (1).pdf#page=2> "Title")\n[CSV][ref]\n\n[ref]: files/list.csv\n';
|
|
assert.deepEqual(ranges(raw).map(({ href, original, html }) => ({ href, original, html })), [
|
|
{ href: 'images/photo%20one.png', original: 'images/photo%20one.png', html: false },
|
|
{ href: 'files/report (1).pdf#page=2', original: 'files/report (1).pdf#page=2', html: false },
|
|
{ href: 'files/list.csv', original: 'files/list.csv', html: false }
|
|
]);
|
|
});
|
|
|
|
test('legacy space-containing destinations supported by the editor import without copying code examples', () => {
|
|
const raw = '\n[File](files/report%20"Title")\n\n~~~md\n\n~~~\n\n``\n';
|
|
assert.deepEqual(ranges(raw).map(reference => reference.href), ['images/photo one.png', 'files/report']);
|
|
});
|
|
|
|
test('unused and overridden reference definitions are not imported', () => {
|
|
assert.deepEqual(ranges('A text-only note.\n\n[unused]: missing.png\n'), []);
|
|
const raw = '![Image][photo]\n\n[photo]: images/real.png\n[PHOTO]: missing.png\n[unused]: also-missing.png\n';
|
|
assert.deepEqual(ranges(raw).map(reference => reference.href), ['images/real.png']);
|
|
});
|
|
|
|
test('full, collapsed, and shortcut references select the first matching definition', () => {
|
|
for (const use of ['![One][ Photo ]', '![photo][]', '![photo]', '[Photo][]', '[photo]']) {
|
|
const raw = `${use}\n\n[PHOTO]: images/real.png\n[ photo ]: missing.png\n`;
|
|
assert.deepEqual(ranges(raw).map(reference => reference.href), ['images/real.png'], use);
|
|
}
|
|
});
|
|
|
|
test('reference labels in code, math, and image alt text do not authorize unused definitions', () => {
|
|
const raw = '`![fake][hidden]`\n\n$[hidden]$\n\n![An [hidden] label](real.png)\n\n[hidden]: missing.png\n';
|
|
assert.deepEqual(ranges(raw).map(reference => reference.href), ['real.png']);
|
|
});
|
|
|
|
test('UTF-8 byte offsets retain Unicode, BOM, CRLF, and untouched custom frontmatter', () => {
|
|
const raw = '\uFEFF---\r\ncustom: ""\r\n---\r\n雪 📝 \r\n';
|
|
assert.equal(ranges(raw).length, 1);
|
|
assert.equal(ranges(raw)[0].original, '资源/café%20雪.png');
|
|
});
|
|
|
|
test('literal code, comments, closed display math, and HTML code do not become attachments', () => {
|
|
const raw = '``\n\n~~~~md\n\n~~~~\n\n$$\n\n$$\n\n<!-- <img src="comment.png"> -->\n\n<pre><code><img src="literal.png"></code></pre>\n\n';
|
|
assert.deepEqual(ranges(raw).map(reference => reference.href), ['real.png']);
|
|
});
|
|
|
|
test('inline math stays literal while escaped dollars and dollar-containing destinations remain links', () => {
|
|
const raw = '$$ \\$\\$ ';
|
|
assert.deepEqual(ranges(raw).map(reference => reference.href), ['real.png', 'images/$price$.png']);
|
|
});
|
|
|
|
test('remote URLs, anchors, absolute paths, and linked Markdown notes stay unchanged', () => {
|
|
const raw = ' [mail](mailto:test@example.com) [anchor](#section)\n [note](Related.MD#heading) [encoded](Related%2EMD)\n';
|
|
assert.deepEqual(ranges(raw), []);
|
|
});
|
|
|
|
test('HTML images, file links, and PDF embeds retain attribute byte ranges and decode entities', () => {
|
|
const raw = '<img alt="a > b src="missing.png"" src="images/a&b.png"> <a href=files/report.pdf>File</a>\n\n<div data-pdf-src="files/view.pdf#page=2" data-pdf-name="View"></div>';
|
|
assert.deepEqual(ranges(raw).map(({ href, original, html }) => ({ href, original, html })), [
|
|
{ href: 'images/a&b.png', original: 'images/a&b.png', html: true },
|
|
{ href: 'files/report.pdf', original: 'files/report.pdf', html: true },
|
|
{ href: 'files/view.pdf#page=2', original: 'files/view.pdf#page=2', html: true }
|
|
]);
|
|
assert.equal(ranges(raw)[2].pdf, true);
|
|
assert.equal(ranges(raw)[0].pdf, false);
|
|
});
|
|
|
|
test('links inside image alt text are not copied as additional attachments', () => {
|
|
const raw = '](real.png)';
|
|
assert.deepEqual(ranges(raw).map(reference => reference.href), ['real.png']);
|
|
});
|
|
|
|
test('escaped Markdown paths are decoded without consuming code examples or link titles', () => {
|
|
const raw = '[Report](files/report\\(1\\).pdf "Other (unused.png)")';
|
|
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']);
|
|
});
|