fix: ignore urls in replace-links

This commit is contained in:
Kodai Nakamura 2025-03-09 21:27:45 +09:00
parent a0b58d007f
commit a6b9a4824c
2 changed files with 38 additions and 2 deletions

View file

@ -44,6 +44,42 @@ describe("ignore url", () => {
});
expect(result).toBe("- https://x.com/xxxx/status/12345?t=25S02Tda");
}
{
const { candidateMap, trie } = buildCandidateTrieForTest({
files: [{ path: "http" }],
settings: {
restrictNamespace: false,
baseDir: undefined,
},
});
const result = await replaceLinks({
body: "Hello https://claude.ai/chat/xxx",
linkResolverContext: {
filePath: "journals/2022-01-01",
trie,
candidateMap,
},
});
expect(result).toBe("Hello https://claude.ai/chat/xxx");
}
{
const { candidateMap, trie } = buildCandidateTrieForTest({
files: [{ path: "http" }],
settings: {
restrictNamespace: false,
baseDir: undefined,
},
});
const result = await replaceLinks({
body: "こんにちは https://claude.ai/chat/xxx",
linkResolverContext: {
filePath: "journals/2022-01-01",
trie,
candidateMap,
},
});
expect(result).toBe("こんにちは https://claude.ai/chat/xxx");
}
});
it("multiple urls", async () => {

View file

@ -43,9 +43,9 @@ export const replaceLinks = ({
parseInt(candidate, 10) >= 1 &&
parseInt(candidate, 10) <= 12;
// Regex to protect code blocks, inline code, wikilinks, and Markdown links.
// Regex to protect code blocks, inline code, wikilinks, Markdown links, and URLs.
const protectedRegex =
/(```[\s\S]*?```|`[^`]*`|\[\[[^\]]+\]\]|\[[^\]]+\]\([^)]+\))/g;
/(```[\s\S]*?```|`[^`]*`|\[\[[^\]]+\]\]|\[[^\]]+\]\([^)]+\)|https?:\/\/[^\s]+)/g;
// Normalize the body text to NFC.
body = body.normalize("NFC");