From e6167138522ef650378eb4d0d9906e101b657eeb Mon Sep 17 00:00:00 2001 From: Mark Said Camilleri Date: Sun, 21 Jan 2024 01:01:39 +0000 Subject: [PATCH 1/5] Added tests for custom queries --- tests/functions.test.ts | 69 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/tests/functions.test.ts b/tests/functions.test.ts index a30db3f..d0ad47f 100644 --- a/tests/functions.test.ts +++ b/tests/functions.test.ts @@ -59,6 +59,75 @@ describe("replaceContentWithPageLinks()", () => { 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); + }); + it("should output tags when parseAsTags is configured", () => { let [content, update] = replaceContentWithPageLinks( ["page", "multiple words"], From 85fedd86f49a45afe1ca48debd416fadcf91b523 Mon Sep 17 00:00:00 2001 From: Mark Said Camilleri Date: Sun, 21 Jan 2024 01:01:59 +0000 Subject: [PATCH 2/5] Added preservation for custom queries --- src/functions.ts | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/functions.ts b/src/functions.ts index 6b3104a..ec8a28d 100644 --- a/src/functions.ts +++ b/src/functions.ts @@ -3,6 +3,7 @@ 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 CUSTOM_QUERY_PLACEHOLDER = "3cf737a1-1a29-4dd1-8db5-45effa23c810"; const parseForRegex = (s: string) => { //Remove regex special characters from s @@ -36,10 +37,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); @@ -67,6 +69,15 @@ export function replaceContentWithPageLinks( return MARKDOWN_LINK_PLACEHOLDER; }); + 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; + } + ); + let needsUpdate = false; allPages.forEach((page) => { const regex = new RegExp( @@ -121,5 +132,9 @@ export function replaceContentWithPageLinks( content = content.replace(MARKDOWN_LINK_PLACEHOLDER, value); }); + customQueryTracker?.forEach((value, index) => { + content = content.replace(CUSTOM_QUERY_PLACEHOLDER, value); + }); + return [content, needsUpdate]; } From d3ad6b89aa24399810b08261bdd3ec3ea30dca49 Mon Sep 17 00:00:00 2001 From: Mark Said Camilleri Date: Sun, 21 Jan 2024 02:36:21 +0000 Subject: [PATCH 3/5] Allowing for escaped square brackets in markdown links --- src/functions.ts | 2 +- tests/functions.test.ts | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/functions.ts b/src/functions.ts index 6b3104a..8f3982d 100644 --- a/src/functions.ts +++ b/src/functions.ts @@ -61,7 +61,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; diff --git a/tests/functions.test.ts b/tests/functions.test.ts index a30db3f..c0f3dbf 100644 --- a/tests/functions.test.ts +++ b/tests/functions.test.ts @@ -46,15 +46,17 @@ 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); }); From 1cc20cad5b56ee1a7eafa247fbc454f30f008d9b Mon Sep 17 00:00:00 2001 From: sharath Date: Sat, 27 Jan 2024 12:29:03 +0100 Subject: [PATCH 4/5] changed insertBlocks to insert-blocks --- index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.ts b/index.ts index e79b923..1d3fd0b 100644 --- a/index.ts +++ b/index.ts @@ -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") { if (logseq.settings?.enableAutoParse) { blockArray?.forEach(parseBlockForLink); } From 08c16d77bcf9c3c293d188bd39e558dddda88dbb Mon Sep 17 00:00:00 2001 From: Aryan Sawhney <80150109+sawhney17@users.noreply.github.com> Date: Thu, 8 Feb 2024 20:42:25 +0400 Subject: [PATCH 5/5] Update index.ts --- index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.ts b/index.ts index 1d3fd0b..e6ce4e3 100644 --- a/index.ts +++ b/index.ts @@ -149,7 +149,7 @@ const main = async () => { getPages(); dateFormat = (await logseq.App.getUserConfigs()).preferredDateFormat; logseq.DB.onChanged((e) => { - if (e.txMeta?.outlinerOp == "insert-blocks") { + if (e.txMeta?.outlinerOp == "insert-blocks" || e.txMeta?.outlinerOp == "insertBlocks") { if (logseq.settings?.enableAutoParse) { blockArray?.forEach(parseBlockForLink); }