Advanced query for a list of all the links in current page

Hi there, I would like to know if someone can help me:
When I’m editing documents in Logseq, I often randomly create new links in current block to summarize my current thoughts. Is there any query that can list all these links (just the link) of current page, and make a list? This would help me organize the index more conveniently.

Chatgpt give me some very weird ones, and the following is invalid:

#+BEGIN_QUERY
{:title "All Links in Current Page"
 :query [:find (distinct ?link)
         :in $ ?current-page
         :where
         [?p :block/name ?current-page]
         [?b :block/page ?p]
         [?b :block/content ?content]
         [(re-seq #"\[\[([^\]]+)\]\]" ?content) ?matches]
         [(:block/name ?m) ?matches]
         [?m ?link]]
 :inputs [:current-page]
 :collapsed? false
 :result-transform (fn [result]
                     [:ul 
                      (for [link result]
                        [:li [:a {:href (str "#/page/" link)} link]])])}
#+END_QUERY

Could you get more specific? Maybe share:

  • some example blocks
  • how the list of their links should look like
1 Like
  • Agree that is an odd query
    • Here’s a reworked query from discord so this can be discovered by others searching
    • Surprisingly if you change :result-transform in your query to :view it is a valid query view
    • If you load a page with this query onto the right sidebar, you may find it worthwhile to change :current-page input to :query-page
#+BEGIN_QUERY
{:title "🔙 All Links in Current Page"
 :inputs [:current-page]
 :query [:find ?link                    ; only care about link name
         :in $ ?current
         :where 
           [?p :block/name ?current]    ; get current page
           [?b :block/page ?p]          ; block must be on current page
           [?b :block/refs ?refpage]    ; blocks with references
           [?refpage :block/name ?link] ; reference page name
 ]
 :view (fn [result]
     [:ul (for [link result]
       [:li [:a {:href (str "#/page/" link)} link]]
     )])
}
#+END_QUERY
1 Like

I’m sorry if I wasn’t clear!!

An example page:

links list I need would be:

  • [[October 23rd, 2020]]
  • [[Readwise]]
  • [[logsequser]]

It would be better if there was no journal link.

Thanks for reply!

Thank you very much for your reply! (and sorry for my lack of clarity)

I tried this query, and I’m very happy that it works! Regarding this, I would like to optimize it further to achieve the following two goals:

  1. not only extract the content of the link, but also include [], which opens the corresponding page.
  2. avoid journal links

I’ve tried the following query after modifying it. It does 1 but not 2 (my journal title format is yyyyy-mm-dd), and I was wondering if I could ask you more about this? Thank you very much!

#+BEGIN_QUERY
{:title "🔙 All Non-Journal Links in Current Page"
 :inputs [:current-page]
 :query [:find ?link                    ; only care about link name
         :in $ ?current
         :where 
           [?p :block/name ?current]    ; get current page
           [?b :block/page ?p]          ; block must be on current page
           [?b :block/refs ?refpage]    ; blocks with references
           [?refpage :block/name ?link] ; reference page name
           (not [(clojure.string/starts-with? ?link "journal")]) ; exclude journal pages
 ]
 :view (fn [result]
     [:ul (for [link result]
       [:li [:a {:href (str "#/page/" link)} (str "[[" link "]]")]] ; display link with double brackets
     )])
}
#+END_QUERY
  • Based on your description, @autokludge’s query looks good enough.
  • For excluding journals:
    • add a line:
      (not [?refpage :block/journal-day]) ; not a journal
      
    • right above existing line:
      [?refpage :block/name ?link] ; reference page name
      
1 Like

I tested this query, and it does what I need it to do very well! Thank you very much!

This query is very useful for social science reading and synthesis of notes. I often have to work with long reading notes and randomly leave links in blocks, and it’s quite important for me to be able to see what I’ve written in one query. Thank you both very much.