Skip to content

Commit

Permalink
Merge pull request #75 from markscamilleri/ignore-todo-markers
Browse files Browse the repository at this point in the history
fix(markers): Preserve TODO markers when autolinking
  • Loading branch information
sawhney17 authored Apr 28, 2024
2 parents 014ed2d + 5b20edb commit e543235
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ const CODE_BLOCK_PLACEHOLDER = "71e46a9e-1150-49c3-a04b-0491ebe05922";
const INLINE_CODE_PLACEHOLDER = "164b97c2-beb7-4204-99b4-6ec2ddc93f9c";
const PROPERTY_PLACEHOLDER = "50220b1c-63f0-4f57-aa73-08c4d936a419";
const MARKDOWN_LINK_PLACEHOLDER = "53c65a4a-137d-44a8-8849-8ec6ca411942";
const MARKER_PLACEHOLDERS = {
NOW: "2f112da4-9248-4e2d-84d5-d9488291799f",
LATER: "be8228a3-8d31-4592-b0a5-aa43ce1cab05",
DOING: "36080c19-b7d7-4397-8ecf-2bcf670d0204",
DONE: "8d03ffae-c539-48da-891a-3020a18812f1",
CANCELED: "774f1b24-7533-4c86-93b2-ab4c2cd43b7d",
CANCELLED: "7b6a5608-b554-489b-97a3-f9043e436903",
"IN-PROGRESS": "842916b9-3f8e-4fd7-8490-6015a30a1dce",
TODO: "1f5dc7a6-9479-4692-9f67-8034088395b5",
WAIT: "d7a8bdf1-1336-4538-b35b-14459e50046e",
WAITING: "d9c67fde-12ae-41e5-9f70-9959c172154b",
};
const CUSTOM_QUERY_PLACEHOLDER = "3cf737a1-1a29-4dd1-8db5-45effa23c810";

const parseForRegex = (s: string) => {
Expand Down Expand Up @@ -69,6 +81,15 @@ export function replaceContentWithPageLinks(
return MARKDOWN_LINK_PLACEHOLDER;
});

// Replace todo markers with placeholders
content = content.replaceAll(
/^(NOW|LATER|DOING|DONE|CANCELED|CANCELLED|IN-PROGRESS|TODO|WAIT|WAITING)/gm,
(match) => {
console.debug({ LogseqAutomaticLinker: "To Do marker found", match });
return MARKER_PLACEHOLDERS[match];
}
);

content = content.replaceAll(
/#\+BEGIN_QUERY((?!#\+END_QUERY).|\n)*#\+END_QUERY/gim,
(match) => {
Expand Down Expand Up @@ -131,6 +152,9 @@ export function replaceContentWithPageLinks(
markdownLinkTracker?.forEach((value, index) => {
content = content.replace(MARKDOWN_LINK_PLACEHOLDER, value);
});
Object.entries(MARKER_PLACEHOLDERS).forEach(([marker, markerPlaceholder]) => {
content = content.replaceAll(markerPlaceholder, marker);
});

customQueryTracker?.forEach((value, index) => {
content = content.replace(CUSTOM_QUERY_PLACEHOLDER, value);
Expand Down
59 changes: 59 additions & 0 deletions tests/functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,65 @@ describe("replaceContentWithPageLinks()", () => {
expect(update).toBe(true);
});

it.each([
{
input: "NOW [#A] A started todo",
expected: "NOW [#A] A started [[todo]]",
},
{
input: "LATER [#B] A todo for later",
expected: "LATER [#B] A [[todo]] for [[Later]]",
},
{
input: "DOING [#A] Fix the todo marker issue",
expected: "DOING [#A] Fix the [[todo]] marker issue",
},
{ input: "DONE A done todo", expected: "DONE A [[Done]] [[todo]]" },
{
input: "CANCELED A canceled todo",
expected: "CANCELED A [[Canceled]] [[todo]]",
},
{
input: "CANCELLED A cancelled todo",
expected: "CANCELLED A [[Cancelled]] [[todo]]",
},
{
input: "IN-PROGRESS An in progress To Do",
expected: "IN-PROGRESS An [[In Progress]] [[To Do]]",
},
{ input: "TODO A todo", expected: "TODO A [[todo]]" },
{
input: "WAIT [#C] A todo waiting to be unblocked",
expected: "WAIT [#C] A [[todo]] [[Waiting]] to be unblocked",
},
{
input: "WAITING A waiting todo",
expected: "WAITING A [[Waiting]] [[todo]]",
},
])("should preserve the to do marker for $input", ({ input, expected }) => {
let [content, update] = replaceContentWithPageLinks(
[
"Now",
"Later",
"Doing",
"Done",
"Canceled",
"Cancelled",
"In Progress",
"In-Progress",
"To Do",
"todo",
"Wait",
"Waiting",
],
input,
false,
false
);
expect(content).toBe(expected);
expect(update).toBe(true);
});

it("should output tags when parseAsTags is configured", () => {
let [content, update] = replaceContentWithPageLinks(
["page", "multiple words"],
Expand Down

0 comments on commit e543235

Please sign in to comment.