Skip to content

Commit

Permalink
Merge pull request #72 from markscamilleri/ignore-advanced-queries
Browse files Browse the repository at this point in the history
fix(custom-queries): Preserve custom queries when autolinking
  • Loading branch information
sawhney17 authored Feb 8, 2024
2 parents e3c5922 + 85fedd8 commit c246b2e
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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];
}
69 changes: 69 additions & 0 deletions tests/functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down

0 comments on commit c246b2e

Please sign in to comment.