Skip to content

Commit

Permalink
Merge branch 'main' into ignore-todo-markers
Browse files Browse the repository at this point in the history
  • Loading branch information
markscamilleri authored Feb 8, 2024
2 parents 2dff012 + 42b83f1 commit 9f0643f
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 9 deletions.
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ const main = async () => {
getPages();
dateFormat = (await logseq.App.getUserConfigs()).preferredDateFormat;
logseq.DB.onChanged((e) => {
if (e.txMeta?.outlinerOp == "insertBlocks") {
if (e.txMeta?.outlinerOp == "insert-blocks" || e.txMeta?.outlinerOp == "insertBlocks") {
if (logseq.settings?.enableAutoParse) {
blockArray?.forEach(parseBlockForLink);
}
Expand Down
23 changes: 18 additions & 5 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const MARKER_PLACEHOLDERS = {
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) => {
//Remove regex special characters from s
Expand Down Expand Up @@ -48,10 +49,11 @@ export function replaceContentWithPageLinks(
parseSingleWordAsTag: boolean
): [string, boolean] {
// Handle content that should not be automatically linked
let codeblockReversalTracker = [];
let inlineCodeReversalTracker = [];
let propertyTracker = [];
let markdownLinkTracker = [];
const codeblockReversalTracker = [];
const inlineCodeReversalTracker = [];
const propertyTracker = [];
const markdownLinkTracker = [];
const customQueryTracker = [];

content = content.replaceAll(/```([^`]|\n)*```/gim, (match) => {
codeblockReversalTracker.push(match);
Expand All @@ -73,7 +75,7 @@ export function replaceContentWithPageLinks(

// Broken Markdown links with nested pages won't be detected by this regex and have to be fixed manually.
// Example: [[[page]] This is a broken Markdown link](http://example.com)
content = content.replaceAll(/\[[^\[\]]+\]\([^\(\)]+\)/g, (match) => {
content = content.replaceAll(/\[(([^\[\]]|\\\[|\\\])+)\]\(.*\)/g, (match) => {
markdownLinkTracker.push(match);
console.debug({ LogseqAutomaticLinker: "Markdown link found", match });
return MARKDOWN_LINK_PLACEHOLDER;
Expand All @@ -85,6 +87,13 @@ export function replaceContentWithPageLinks(
(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) => {
customQueryTracker.push(match);
console.debug({ LogseqAutomaticLinker: "Custom query found", match });
return CUSTOM_QUERY_PLACEHOLDER;
}
);

Expand Down Expand Up @@ -145,5 +154,9 @@ export function replaceContentWithPageLinks(
content = content.replaceAll(markerPlaceholder, marker);
});

customQueryTracker?.forEach((value, index) => {
content = content.replace(CUSTOM_QUERY_PLACEHOLDER, value);
});

return [content, needsUpdate];
}
77 changes: 74 additions & 3 deletions tests/functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,86 @@ describe("replaceContentWithPageLinks()", () => {

it("should preserve Markdown links", () => {
let [content, update] = replaceContentWithPageLinks(
["page", "link"],
["page", "link", "Logseq"],
`This page has a link: [page link will not be touched](http://a.com)
[another page](http://b.com) also with a link`,
[another page](http://b.com) also with a link
[\\[This\\] is a Logseq page](https://logseq.com)`,
false,
false
);
expect(content).toBe(
`This [[page]] has a [[link]]: [page link will not be touched](http://a.com)
[another page](http://b.com) also with a [[link]]`
[another page](http://b.com) also with a [[link]]
[\\[This\\] is a Logseq page](https://logseq.com)`
);
expect(update).toBe(true);
});

it("should preserve custom query scripts", () => {
const customQueries = [
`#+BEGIN_QUERY
{
:title [:h2 "In Progress"]
:query [
:find (pull ?b [*])
:where
[?b :block/uuid]
[?b :block/marker ?marker]
[(contains? #{"NOW", "DOING", "IN PROGRESS", "IN-PROGRESS"} ?marker)]
]
:result-transform (
fn [result] ( sort-by (
fn [item] ( get item :block/priority "Z" )
)
result)
)
:remove-block-children? false
:group-by-page? false
:breadcrumb-show? false
:collapsed? false
}
#+END_QUERY`,
`#+BEGIN_QUERY
{
:title [:h2 "TO DO"]
:query [
:find (pull ?b [*])
:where
[?b :block/uuid]
[?b :block/marker ?marker]
[(contains? #{"TO DO", "LATER"} ?marker)]
]
:result-transform (
fn [result] ( sort-by (
fn [item] ( get item :block/priority "Z" )
)
result)
)
:remove-block-children? false
:group-by-page? false
:breadcrumb-show? false
:collapsed? false
}
#+END_QUERY`,
];

const [content, update] = replaceContentWithPageLinks(
["In Progress", "find", "link"],
`${customQueries[0]}
Ths sentence contains a link
${customQueries[1]}`,
false,
false
);

expect(content).toEqual(
`${customQueries[0]}
Ths sentence contains a [[link]]
${customQueries[1]}`
);
expect(update).toBe(true);
});
Expand Down

0 comments on commit 9f0643f

Please sign in to comment.