sawhney17 / logseq-automatic-linker

MIT License
87 stars 16 forks source link

fix(custom-queries): Preserve custom queries when autolinking #72

Closed markscamilleri closed 4 months ago

markscamilleri commented 5 months ago

Fixes #27 and #70

Problem

I encountered a problem where, if custom queries have words that have pages associated with them, then the automatic linker will link the query and wreck havoc.

For example, given the pages find, In Progress and NOW exist, if the following query is found in a page:

#+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

This currently gets transformed to:

#+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

Solution

This Pull Request aims to address this issue and stop queries from being messed with. This is done by using regex to ignore content that is between the #+BEGIN_QUERY and #+END_QUERY prefix and suffix.

Documntation

In a real-world example, the query that I have used as an example (whcih is a query I use to see what I have in progress), actually gets transformed so the markers are showing the current date as stated in #70 and #27, for example:

#+BEGIN_QUERY
{
  :title [:h2 "[[In Progress]]"]
  :query [
    :[[find]] (pull ?b [*])
    :where
      [?b :block/uuid]
      [?b :block/marker ?marker]
      [(contains? #[[2024-01-21]] ?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

I was unable to reproduce this issue for the automated tests. However, I was also unable to reproduce this issue while manually testing this fix, so hopefully this also fixes #70.