\n## References\n[1] Code.\n\n\n## References\n[3] Actual.'; - expect([...parseReferences(source).entries.keys()]).toEqual([3]); + it('ignores apparent reference anchors in YAML, fenced code, comments, raw code, and math', () => { + const source = '---\nvalue: False. ^ref-1\n---\n```md\nFalse. ^ref-2\n```\n\n%%\nFalse. ^ref-4\n%%\n
\nFalse. ^ref-5\n\n\n$$\nFalse. ^ref-7\n$$\n8. Actual. ^ref-8'; + expect([...parseReferences(source).entries.keys()]).toEqual([8]); }); - it('detects duplicate IDs across multiple bibliography sections', () => { - const index = parseReferences('## References\n[1] First.\n## Body\ntext\n## Bibliography\n[1] Conflicting first.\n[2] Second.'); - expect(index.sections).toHaveLength(2); - expect(index.duplicates.has(1)).toBe(true); + it('ignores reference definitions inside an unterminated code fence', () => { + expect(parseReferences('```md\n1. Code. ^ref-1').entries.size).toBe(0); }); }); diff --git a/tests/citations.test.ts b/tests/citations.test.ts index 2ad4627..96df423 100644 --- a/tests/citations.test.ts +++ b/tests/citations.test.ts @@ -103,8 +103,8 @@ describe('findCitationGroups', () => { }); it('keeps separate local references instead of combining their bibliography indices', () => { - const first = '[1–3]\n\n## References\n[1] A.\n[2] B.\n[3] C.'; - const second = '[1–3]\n\n## References\n[1] X.\n[2] Y.\n[3] Z.'; + const first = '[1–3]\n\n[1] A. ^ref-1\n[2] B. ^ref-2\n[3] C. ^ref-3'; + const second = '[1–3]\n\n[1] X. ^ref-1\n[2] Y. ^ref-2\n[3] Z. ^ref-3'; const a = parseReferences(first); const b = parseReferences(second); expect(findCitationGroups(first, a, options)[0].numbers).toEqual([1, 2, 3]); @@ -114,12 +114,12 @@ describe('findCitationGroups', () => { }); it('keeps missing intermediate entries so the UI can report them', () => { - const source = '[1–10]\n\n## References\n[1] First.\n[10] Tenth.'; + const source = '[1–10]\n\n[1] First. ^ref-1\n[10] Tenth. ^ref-10'; expect(findCitationGroups(source, parseReferences(source), options)[0].numbers).toHaveLength(10); }); it('keeps ambiguous entries so the UI can report them', () => { - const source = '[1–2]\n\n## References\n[1] A.\n[1] Ambiguous.\n[2] B.'; + const source = '[1–2]\n\n[1] A. ^ref-1\n[1] Ambiguous. ^ref-1\n[2] B. ^ref-2'; const index = parseReferences(source); expect(index.duplicates.has(1)).toBe(true); expect(findCitationGroups(source, index, options)[0].numbers).toEqual([1, 2]); diff --git a/tests/main.test.ts b/tests/main.test.ts index 7d83e68..7915110 100644 --- a/tests/main.test.ts +++ b/tests/main.test.ts @@ -30,6 +30,9 @@ interface HoverLink { const bibliography = '## References\n[1] Original first. ^ref-1\n[2] Second reference. ^ref-2\n[3] Third reference. ^ref-3'; const source = `First [1, 3].\n\nSecond [2–3].\n\n${bibliography}`; +const multipleBibliographies = String.raw`Main \[[1](<#^ref-1>)–[2](<#^ref-2>)\], supplementary \[[1](<#^sref-1>)–[2](<#^sref-2>)\].` + + '\n\n## 6 参考文献(2条)\n1. Main first. ^ref-1\n2. Main second. ^ref-2' + + '\n\n## 7 Supplement\n### 7.7 补充参考文献(2条)\n1. Supplement first. ^sref-1\n2. Supplement second. ^sref-2'; const manifest: PluginManifest = { id: 'citation-range-preview', name: 'Citation Preview', version: '1.1.0', minAppVersion: '1.8.0', description: 'Test manifest.', author: 'Test', isDesktopOnly: false @@ -109,6 +112,24 @@ function mouseover(target: HTMLElement, options?: MouseEventInit): MouseEvent { return event; } +function independentlyNumberedCitations(): HTMLElement { + const paragraph = document.createElement('p'); + for (const [label, prefix] of [['Main', 'ref'], ['supplementary', 'sref']]) { + paragraph.append(`${label} `); + const sup = paragraph.appendChild(document.createElement('sup')); + sup.append('['); + for (const number of [1, 2]) { + if (number > 1) sup.append('–'); + const link = sup.appendChild(document.createElement('a')); + link.setAttribute('href', `#^${prefix}-${number}`); + link.textContent = String(number); + } + sup.append(']'); + paragraph.append(' '); + } + return paragraph; +} + beforeEach(() => installTestDomMethods(document)); afterEach(() => { for (const plugin of plugins.splice(0)) plugin.unload(); @@ -149,6 +170,109 @@ describe('native Page preview host integration', () => { expect((await render()).textContent).toContain('Saved replacement.'); }); + it('keeps equal visible citation ranges in separate bibliography scopes and preview caches', async () => { + const { plugin, process, render, trigger, file } = await setup(multipleBibliographies); + const paragraph = await process(independentlyNumberedCitations()); + const targets = Array.from(paragraph.querySelectorAll
Figures [1–10], references [1–10].
'); + const citation = { ...group('[1–10]', true), scopeId: 'main', linkTargets: ['^ref-1', '^ref-10'] }; + const triggers = decorateReadingCitations(root, [citation]); + expect(triggers).toHaveLength(1); + expect(triggers[0].element.querySelector('a')?.getAttribute('href')).toBe('#^ref-1'); + expect(root.querySelector('a[href="#^fig-1"]')?.closest('.crp-citation')).toBeNull(); + }); + + it('uses exact linked targets to distinguish equal visible numbers from different bibliographies', () => { + const root = fixture(''); + const main = { ...group('[1–10]', true, 0), scopeId: 'main', linkTargets: ['^ref-1', '^ref-10'] }; + const supplement = { ...group('[1–10]', true, 50), scopeId: 'supplement', linkTargets: ['^sref-1', '^sref-10'] }; + const triggers = decorateReadingCitations(root, [main, supplement]); + expect(triggers.map(trigger => trigger.group.scopeId)).toEqual(['supplement', 'main']); + expect(triggers.map(trigger => trigger.element.querySelector('a')?.getAttribute('href'))).toEqual(['#^sref-1', '#^ref-1']); + }); + + it('resolves relative same-note paths and encoded anchors using the source parser rules', () => { + const root = fixture(''); + const citation = { ...group('[1–10]', true), linkTargets: ['^ref-1', '^ref-10'] }; + const triggers = decorateReadingCitations(root, [citation], { sourcePath: 'Notes/Paper one.md' }); + expect(triggers).toHaveLength(1); + expect(triggers[0].element.textContent).toBe('[1–10]'); + }); + + it('requires the complete ordered target sequence while preserving repeated endpoint links', () => { + const root = fixture(''); + const range = { ...group('[1–10]', true), linkTargets: ['^ref-1', '^ref-10'] }; + const repeated = { ...group('[1–10, 1]', true, 30), linkTargets: ['^ref-1', '^ref-10', '^ref-1'] }; + const triggers = decorateReadingCitations(root, [range, repeated]); + expect(triggers).toHaveLength(1); + expect(triggers[0].group).toBe(repeated); + expect(triggers[0].element.querySelectorAll('a')).toHaveLength(3); + }); + it('supports endpoint links without outer brackets but does not decorate bare numbers', () => { const root = fixture(''); const triggers = decorateReadingCitations(root, [group('[1–10]', true)]); diff --git a/tests/scopes.test.ts b/tests/scopes.test.ts new file mode 100644 index 0000000..fdddf90 --- /dev/null +++ b/tests/scopes.test.ts @@ -0,0 +1,106 @@ +import { describe, expect, it } from 'vitest'; +import { CitationDocument } from '../src/model'; +import { DEFAULT_SETTINGS, type CitationGroup } from '../src/types'; + +const references = [ + '1. Main first. ^ref-1', + '2. Main second. ^ref-2', + '3. Main third. ^ref-3', + '', + '1. Supplement first. ^sref-1', + '2. Supplement second. ^sref-2', + '3. Supplement third. ^sref-3' +].join('\n'); + +function parse(body: string, entries = references): CitationDocument { + return new CitationDocument(`${body}\n\n${entries}`, 'Papers/Article.md', DEFAULT_SETTINGS); +} + +function texts(model: CitationDocument, group: CitationGroup): Array