diff --git a/src/replace-links/__tests__/replace-links.table.test.ts b/src/replace-links/__tests__/replace-links.table.test.ts index 4d98a83..9f6ae2d 100644 --- a/src/replace-links/__tests__/replace-links.table.test.ts +++ b/src/replace-links/__tests__/replace-links.table.test.ts @@ -108,6 +108,35 @@ note1 | Test Item | | | --- | --- | | [[note1]] | | +`) + }) + + it("escapes table aliases after earlier resolved wikilinks change segment offsets", () => { + const settings = { + scoped: false, + baseDir: undefined, + proximityBasedLinking: true, + } + const { candidateMap, trie } = buildCandidateTrieForTest({ + files: [{ path: "ns/note1" }], + settings, + }) + const result = replaceLinks({ + body: `[[note1]] +| note1 | | +`, + linkResolverContext: { + filePath: "journals/2022-01-01", + trie, + candidateMap, + }, + settings, + resolvedAmbiguities: new Map([ + ["[[note1]]", "very/long/path/note1|note1"], + ]), + }) + expect(result).toBe(`[[very/long/path/note1|note1]] +| [[ns/note1\\|note1]] | | `) }) }) diff --git a/src/replace-links/replace-links.ts b/src/replace-links/replace-links.ts index 4800dbf..b4ced09 100644 --- a/src/replace-links/replace-links.ts +++ b/src/replace-links/replace-links.ts @@ -647,6 +647,7 @@ export const replaceLinks = ({ // Get the current namespace const currentNamespace = getCurrentNamespace(filePath, settings.baseDir) + let bodyWithResolvedWikilinks = body const markdownOptions = { protectHeadings: settings.ignoreHeadings, @@ -723,7 +724,7 @@ export const replaceLinks = ({ ): string => { if (!text.includes("\n")) { const isInTable = !settings.ignoreMarkdownTables - && isIndexInsideMarkdownTable(body, segment.start) + && isIndexInsideMarkdownTable(bodyWithResolvedWikilinks, segment.start) return processTextSegment(text, isInTable) } @@ -742,13 +743,12 @@ export const replaceLinks = ({ const absoluteIndex = segment.start + offset const isInTable = !settings.ignoreMarkdownTables - && isIndexInsideMarkdownTable(body, absoluteIndex) + && isIndexInsideMarkdownTable(bodyWithResolvedWikilinks, absoluteIndex) return processTextSegment(line, isInTable) }) } - let bodyWithResolvedWikilinks = body if (resolvedAmbiguities) { bodyWithResolvedWikilinks = segmentMarkdown(body, markdownOptions) .map((segment) => { diff --git a/src/replace-url-with-title/__tests__/replace-url-with-title.test.ts b/src/replace-url-with-title/__tests__/replace-url-with-title.test.ts index d8d488a..85e3178 100644 --- a/src/replace-url-with-title/__tests__/replace-url-with-title.test.ts +++ b/src/replace-url-with-title/__tests__/replace-url-with-title.test.ts @@ -76,4 +76,17 @@ describe("replaceUrlWithTitle", () => { }) expect(result).toBe("~~~\nhttps://example.com\n~~~") }) + + it("should not replace angle-bracket autolinks", () => { + const body = "" + const result = replaceUrlWithTitle({ + body, + urlTitleMap: new Map( + Object.entries({ + "https://example.com": "Example Title", + }), + ), + }) + expect(result).toBe("") + }) }) diff --git a/src/replace-url-with-title/index.ts b/src/replace-url-with-title/index.ts index 7eca4cc..93d3ac0 100644 --- a/src/replace-url-with-title/index.ts +++ b/src/replace-url-with-title/index.ts @@ -50,8 +50,12 @@ export const replaceUrlWithTitle = ({ nextOccurrence - 2, nextOccurrence, ) + const precedingChar = resultBody[nextOccurrence - 1] const followingChar = resultBody[nextOccurrence + url.length] - if (precedingChars === "](" && followingChar === ")") { + if ( + (precedingChars === "](" && followingChar === ")") + || (precedingChar === "<" && followingChar === ">") + ) { shouldReplace = false }