diff --git a/src/lib/markdown/wikilink-plugin.ts b/src/lib/markdown/wikilink-plugin.ts index 46281a4..a0927e6 100644 --- a/src/lib/markdown/wikilink-plugin.ts +++ b/src/lib/markdown/wikilink-plugin.ts @@ -43,16 +43,21 @@ export function parseWikilinkTarget( if (!rawTarget) return null; + const sectionSuffix = section ? `#${section}` : ""; + // Cross-wiki detection: wiki/note or wiki/ const separatorIdx = rawTarget.indexOf("/"); if (separatorIdx > 0) { const wikiSlug = rawTarget.slice(0, separatorIdx).trim(); const noteSlug = rawTarget.slice(separatorIdx + 1).trim(); if (wikiSlug) { + const defaultLabel = noteSlug + ? `${wikiSlug}/${noteSlug}${sectionSuffix}` + : wikiSlug; return { wikiSlug, noteSlug, - label: rawLabel || (noteSlug ? `${wikiSlug}/${noteSlug}` : wikiSlug), + label: rawLabel || defaultLabel, ...(section ? { section } : {}), }; } @@ -61,7 +66,7 @@ export function parseWikilinkTarget( return { ...(currentWikiSlug ? { wikiSlug: currentWikiSlug } : {}), noteSlug: rawTarget, - label: rawLabel || rawTarget, + label: rawLabel || `${rawTarget}${sectionSuffix}`, ...(section ? { section } : {}), }; } diff --git a/tests/lib/markdown.test.ts b/tests/lib/markdown.test.ts index c0ae096..053a4b9 100644 --- a/tests/lib/markdown.test.ts +++ b/tests/lib/markdown.test.ts @@ -82,13 +82,13 @@ describe("wikilinks", () => { test("renders same-wiki link with section anchor", () => { const { html } = renderMarkdown("see [[my-page#introduction]]", WIKI); expect(html).toContain('href="/wiki/test-wiki/my-page#introduction"'); - expect(html).toContain(">my-page<"); + expect(html).toContain(">my-page#introduction<"); }); test("renders cross-wiki link with section anchor", () => { const { html } = renderMarkdown("see [[other-wiki/my-page#details]]", WIKI); expect(html).toContain('href="/wiki/other-wiki/my-page#details"'); - expect(html).toContain(">other-wiki/my-page<"); + expect(html).toContain(">other-wiki/my-page#details<"); }); test("renders wiki home page link", () => { @@ -261,7 +261,7 @@ describe("extractWikilinks", () => { { wikiSlug: "test-wiki", noteSlug: "page", - label: "page", + label: "page#section", section: "section", }, ]); @@ -272,18 +272,15 @@ describe("extractWikilinks", () => { { wikiSlug: "docs", noteSlug: "page", - label: "docs/page", + label: "docs/page#intro", section: "intro", }, ]); }); - test("strips #heading suffix when recording the slug", () => { - expect(extractWikilinks("jump to [[my-page#Section]]")).toEqual([ - "my-page", - ]); - expect(extractWikilinks("[[a#one]] and [[a#two]] and [[a]]")).toEqual([ - "a", - ]); + test("dedupes [[note#heading]] occurrences against the same note", () => { + const result = extractWikilinks("[[a#one]] and [[a#two]] and [[a]]"); + expect(result).toHaveLength(1); + expect(result[0]?.noteSlug).toBe("a"); }); });