import test from 'node:test'; import assert from 'node:assert/strict'; import { extractLinks, isAttachment, parseWikilink, resolver } from '../src/index.mjs'; const PATHS = ['Book One/Characters/Mara Vell.md', 'Book One/Characters/Tomas.md', 'Book One/Places/Harbor Town.md', 'Book One/Chapters/Chapter 01.md', 'Book One/Places/Notes.md', 'Book One/Characters/Notes.md', 'Mara Vell.md']; const resolve = resolver(PATHS, { vaultPrefix: 'books' }); const FROM = 'Book One/Chapters/Chapter 01.md'; test('resolver step 1: a path from the repo root, with or without .md', () => { assert.deepEqual(resolve(FROM, 'Book One/Places/Harbor Town'), { path: 'Book One/Places/Harbor Town.md', step: 'root' }); assert.deepEqual(resolve(FROM, 'Book One/Places/Harbor Town.md'), { path: 'Book One/Places/Harbor Town.md', step: 'root' }); // Root wins over shortest: Mara Vell.md at the root, not the character page. assert.deepEqual(resolve(FROM, 'Mara Vell'), { path: 'Mara Vell.md', step: 'root' }); }); test('resolver step 2: the same path with vaultPrefix/ stripped', () => { assert.deepEqual(resolve(FROM, 'books/Book One/Characters/Tomas'), { path: 'Book One/Characters/Tomas.md', step: 'vault-prefix' }); assert.deepEqual(resolver(PATHS)(FROM, 'books/Book One/Characters/Tomas'), { path: null, step: 'missing' }); }); test('resolver step 3: a path relative to the linking file, never above the repo root', () => { assert.deepEqual(resolve(FROM, '../Places/Harbor Town'), { path: 'Book One/Places/Harbor Town.md', step: 'relative' }); assert.deepEqual(resolve('Book One/Places/Harbor Town.md', './Notes'), { path: 'Book One/Places/Notes.md', step: 'relative' }); assert.deepEqual(resolve(FROM, '../../../Book One/Places/Harbor Town'), { path: null, step: 'missing' }); }); test('resolver step 4: a base name that matches exactly one indexed file', () => { assert.deepEqual(resolve(FROM, 'Tomas'), { path: 'Book One/Characters/Tomas.md', step: 'shortest' }); assert.deepEqual(resolve(FROM, 'Harbor Town.md'), { path: 'Book One/Places/Harbor Town.md', step: 'shortest' }); assert.deepEqual(resolve(FROM, 'Notes'), { path: null, step: 'ambiguous', count: 2 }); assert.deepEqual(resolve(FROM, 'Nobody'), { path: null, step: 'missing' }); // A partial path is not a base name. assert.deepEqual(resolve(FROM, 'Places/Harbor Town'), { path: null, step: 'missing' }); }); test('a link into another of her repos resolves to nothing', () => { assert.deepEqual(resolve(FROM, 'bean-brain/Journal/Today'), { path: null, step: 'missing' }); assert.deepEqual(resolve(FROM, '../bean-brain/Journal/Today'), { path: null, step: 'missing' }); }); test('wikilink forms, embeds, and links skipped inside code', () => { assert.deepEqual(parseWikilink('[[A/B#Head|Label]]'), { file: 'A/B', heading: 'Head', label: 'Label', embed: false }); assert.deepEqual(parseWikilink('![[pic.png]]'), { file: 'pic.png', heading: null, label: null, embed: true }); assert.equal(parseWikilink('[[a]] and more'), null); const lines = [[1, '[[One]] [[Two|2]] [[Three#H]] ![[Four]] [[#Self]]'], [2, '```js'], [3, '[[Fenced]]'], [4, '```'], [5, 'Inline `[[Code]]` and [md](Five.md) and [web](https://example.invalid/x)']]; assert.deepEqual(extractLinks(lines).map(l => [l.file, l.line]), [['One', 1], ['Two', 1], ['Three', 1], ['Four', 1], ['', 1]]); assert.deepEqual(extractLinks(lines, { markdown: true }).filter(l => l.markdown).map(l => l.file), ['Five.md']); }); test('only a body embed whose target has an extension other than .md, in any case, is an attachment', () => { const att = text => isAttachment(parseWikilink(text)); // By name alone: the reader asks only of an embed that resolved to nothing (sage-1900). for (const s of ['![[cover.png]]', '![[cover.PNG]]', '![[Art/map.pdf|Map]]', '![[Board.canvas]]', '![[clip.mp4#t=5]]', '![[Dr.Quinn]]']) assert.equal(att(s), true, s); for (const s of ['![[missing-note]]', '![[note.md]]', '![[Note.MD]]', '![[Chapter 1.5]]', '![[Mr. Smith]]', '![[A.b/Note]]', '[[cover.png]]', '[[Board.canvas]]', '[[Dr.Quinn]]', '[[#Self]]']) assert.equal(att(s), false, s); assert.equal(isAttachment({ ...parseWikilink('![[portrait.jpg]]'), field: 'relationships' }), false, 'a front matter value'); assert.equal(isAttachment({ file: 'pic.png', markdown: true, embed: false }), false, 'a markdown link'); });