diff --git a/src/functions.ts b/src/functions.ts index 9baa530..e331b58 100644 --- a/src/functions.ts +++ b/src/functions.ts @@ -17,6 +17,7 @@ const MARKER_PLACEHOLDERS = { }; const CUSTOM_QUERY_PLACEHOLDER = "3cf737a1-1a29-4dd1-8db5-45effa23c810"; const CUSTOM_ORG_PROPERTIES_PLACEHOLDER = "4259dbda-2620-4ee0-9886-59508fb8a1ad"; +const CUSTOM_ORG_CODE_BLOCK_PLACEHOLDER = "2e0b35ed-18b1-45d9-8b4f-ed70a0cadd98"; const parseForRegex = (s: string) => { @@ -57,6 +58,7 @@ export function replaceContentWithPageLinks( const markdownLinkTracker = []; const customQueryTracker = []; const customOrgPropertiesTracker = []; + const customOrgCodeTracker = []; content = content.replaceAll(/```[\s\S]*?```/g, (match) => { codeblockReversalTracker.push(match); @@ -111,6 +113,14 @@ export function replaceContentWithPageLinks( } ); + content = content.replaceAll( + /#\+BEGIN_SRC((?!#\+END_SRC).|\n)*#\+END_SRC/gim, + (match) => { + customOrgCodeTracker.push(match); + console.debug({ LogseqAutomaticLinker: "Custom org code block found", match }); + return CUSTOM_ORG_CODE_BLOCK_PLACEHOLDER; + }); + let needsUpdate = false; allPages.forEach((page) => { const regex = new RegExp( @@ -176,5 +186,9 @@ export function replaceContentWithPageLinks( content = content.replace(CUSTOM_ORG_PROPERTIES_PLACEHOLDER, value); }); + customOrgCodeTracker?.forEach((value, index) => { + content = content.replace(CUSTOM_ORG_CODE_BLOCK_PLACEHOLDER, value); + }); + return [content, needsUpdate]; } diff --git a/tests/functions.test.ts b/tests/functions.test.ts index adab121..0bd650a 100644 --- a/tests/functions.test.ts +++ b/tests/functions.test.ts @@ -14,6 +14,19 @@ describe("replaceContentWithPageLinks()", () => { expect(update).toBe(true); }); + it("should preserve org code blocks", () => { + let [content, update] = replaceContentWithPageLinks( + ["page"], + "page before\n#+BEGIN_SRC\npage within code block\n#+END_SRC\npage between\n#+BEGIN_SRC\nanother page within code block#+END_SRC\nand finally\n#+BEGIN_SRC\nwith backticks and page within\n#+END_SRC\npage after", + false, + false + ); + expect(content).toBe( + "[[page]] before\n#+BEGIN_SRC\npage within code block\n#+END_SRC\n[[page]] between\n#+BEGIN_SRC\nanother page within code block#+END_SRC\nand finally\n#+BEGIN_SRC\nwith backticks and page within\n#+END_SRC\n[[page]] after" + ); + expect(update).toBe(true); + }); + it("should preserve inline code", () => { let [content, update] = replaceContentWithPageLinks( ["page"],