Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const MARKER_PLACEHOLDERS = {
WAITING: "d9c67fde-12ae-41e5-9f70-9959c172154b",
};
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) => {
//Remove regex special characters from s
Expand Down Expand Up @@ -54,6 +57,8 @@ export function replaceContentWithPageLinks(
const propertyTracker = [];
const markdownLinkTracker = [];
const customQueryTracker = [];
const customOrgPropertiesTracker = [];
const customOrgCodeTracker = [];

content = content.replaceAll(/```[\s\S]*?```/g, (match) => {
codeblockReversalTracker.push(match);
Expand Down Expand Up @@ -99,6 +104,23 @@ export function replaceContentWithPageLinks(
}
);

content = content.replaceAll(
/:PROPERTIES:((?!:END:).|\n)*:END:/gim,
(match) => {
customOrgPropertiesTracker.push(match);
console.debug({ LogseqAutomaticLinker: "Custom org properties found", match });
return CUSTOM_ORG_PROPERTIES_PLACEHOLDER;
}
);

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 @@ -160,5 +182,13 @@ export function replaceContentWithPageLinks(
content = content.replace(CUSTOM_QUERY_PLACEHOLDER, value);
});

customOrgPropertiesTracker?.forEach((value, index) => {
content = content.replace(CUSTOM_ORG_PROPERTIES_PLACEHOLDER, value);
});

customOrgCodeTracker?.forEach((value, index) => {
content = content.replace(CUSTOM_ORG_CODE_BLOCK_PLACEHOLDER, value);
});

return [content, needsUpdate];
}
25 changes: 22 additions & 3 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 All @@ -29,17 +42,23 @@ describe("replaceContentWithPageLinks()", () => {

it("should preserve properties", () => {
let [content, update] = replaceContentWithPageLinks(
["page", "price"],
["page", "price","test","alias"],
`Some page here with price
price:: 123
page:: this is a property`,
page:: this is a property
:PROPERTIES:
:test: 12312321
:END:`,
false,
false
);
expect(content).toBe(
`Some [[page]] here with [[price]]
price:: 123
page:: this is a property`
page:: this is a property
:PROPERTIES:
:test: 12312321
:END:`
);
expect(update).toBe(true);
});
Expand Down