From feefacdc7dca0f15235aff17ca1ed8ff360919cf Mon Sep 17 00:00:00 2001 From: Kodai Nakamura Date: Sun, 16 Feb 2025 12:53:27 +0900 Subject: [PATCH] refactor: enhance link resolution logic and improve alias handling in replaceLinks --- ...replace-links.namespace-resolution.test.ts | 69 +++++++++++++------ src/replace-links/replace-links.ts | 44 +++++++++++- 2 files changed, 89 insertions(+), 24 deletions(-) diff --git a/src/replace-links/__tests__/replace-links.namespace-resolution.test.ts b/src/replace-links/__tests__/replace-links.namespace-resolution.test.ts index 9f30498..5f5bd94 100644 --- a/src/replace-links/__tests__/replace-links.namespace-resolution.test.ts +++ b/src/replace-links/__tests__/replace-links.namespace-resolution.test.ts @@ -225,27 +225,54 @@ describe("replaceLinks - namespace resolution", () => { }); it("should resolve aliases", async () => { - const { candidateMap, trie } = buildCandidateTrieForTest({ - fileNames: [ - "namespace/xx/yy/link", - "namespace/xx/link", - "namespace/link2", - ], - aliasMap: { - "namespace/xx/yy/link": ["alias"], - }, - restrictNamespace: false, - baseDir: undefined, - }); - const result = await replaceLinks({ - body: "alias", - linkResolverContext: { - filePath: "namespace/xx/current-file", - trie, - candidateMap, - }, - }); - expect(result).toBe("[[namespace/xx/link|alias]]"); + { + const { candidateMap, trie } = buildCandidateTrieForTest({ + fileNames: [ + "namespace/xx/yy/link", + "namespace/xx/link", + "namespace/link2", + ], + aliasMap: { + "namespace/xx/link": ["alias"], + }, + restrictNamespace: false, + baseDir: undefined, + }); + const result = await replaceLinks({ + body: "alias", + linkResolverContext: { + filePath: "namespace/xx/current-file", + trie, + candidateMap, + }, + }); + expect(result).toBe("[[namespace/xx/link|alias]]"); + } + { + const { candidateMap, trie } = buildCandidateTrieForTest({ + fileNames: [ + "namespace/xx/yy/zz/link", + "namespace/xx/yy/link", + "namespace/xx/link", + "namespace/link", + "namespace/link2", + ], + aliasMap: { + "namespace/xx/yy/link": ["alias"], + }, + restrictNamespace: false, + baseDir: undefined, + }); + const result = await replaceLinks({ + body: "alias", + linkResolverContext: { + filePath: "namespace/xx/yy/current-file", + trie, + candidateMap, + }, + }); + expect(result).toBe("[[namespace/xx/yy/link|alias]]"); + } }); }); }); diff --git a/src/replace-links/replace-links.ts b/src/replace-links/replace-links.ts index f941a51..70e5914 100644 --- a/src/replace-links/replace-links.ts +++ b/src/replace-links/replace-links.ts @@ -193,7 +193,25 @@ export const replaceLinks = async ({ } // Replace the candidate with the wikilink format. - result += `[[${candidateData.canonical}]]`; + let linkPath = candidateData.canonical; + const hasAlias = linkPath.includes("|"); + let alias = ""; + + if (hasAlias) { + [linkPath, alias] = linkPath.split("|"); + } + + // Remove pages/ prefix when baseDir is set + if (settings.baseDir && linkPath.startsWith("pages/")) { + linkPath = linkPath.slice("pages/".length); + } + + // Remove base/ prefix when in base directory + if (settings.baseDir && linkPath.startsWith(settings.baseDir + "/")) { + linkPath = linkPath.slice((settings.baseDir + "/").length); + } + + result += hasAlias ? `[[${linkPath}|${alias}]]` : `[[${linkPath}]]`; i += candidate.length; continue outer; } @@ -245,7 +263,17 @@ export const replaceLinks = async ({ if (filteredCandidates.length === 1) { const candidateData = filteredCandidates[0][1]; - result += `[[${candidateData.canonical}]]`; + let linkPath = candidateData.canonical; + // Remove pages/ prefix when baseDir is set + if (settings.baseDir && linkPath.startsWith("pages/")) { + linkPath = linkPath.slice("pages/".length); + } + + // Remove base/ prefix when in base directory + if (settings.baseDir && linkPath.startsWith(settings.baseDir + "/")) { + linkPath = linkPath.slice((settings.baseDir + "/").length); + } + result += `[[${linkPath}]]`; i += word.length; continue outer; } else if (filteredCandidates.length > 1) { @@ -351,7 +379,17 @@ export const replaceLinks = async ({ } } if (bestCandidate !== null) { - result += `[[${bestCandidate[1].canonical}]]`; + let linkPath = bestCandidate[1].canonical; + // Remove pages/ prefix when baseDir is set + if (settings.baseDir && linkPath.startsWith("pages/")) { + linkPath = linkPath.slice("pages/".length); + } + + // Remove base/ prefix when in base directory + if (settings.baseDir && linkPath.startsWith(settings.baseDir + "/")) { + linkPath = linkPath.slice((settings.baseDir + "/").length); + } + result += `[[${linkPath}]]`; i += word.length; continue outer; }