Skip to content

Commit

Permalink
add org code block
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravenso committed Nov 14, 2024
1 parent b2a4fa1 commit fba71f1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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];
}
13 changes: 13 additions & 0 deletions tests/functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down

0 comments on commit fba71f1

Please sign in to comment.