mirror of
https://github.com/kdnk/obsidian-automatic-linker.git
synced 2026-07-22 12:00:30 +00:00
refactor: enhance link resolution logic and improve alias handling in replaceLinks
This commit is contained in:
parent
b70ce45bf0
commit
feefacdc7d
2 changed files with 89 additions and 24 deletions
|
|
@ -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]]");
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue