Tutorial: Find all Pages linked (recursively or direct) (Advanced Query)

(RECURSIVE) All pages the current refers to and so on

#+BEGIN_QUERY
{:query [:find ?tag
   :in $ ?current %
   :where
     [?p :block/name ?current]
     (pr ?p ?tag)
   ]
 :inputs [:current-page]
 :result-transform (fn [result] (sort result))
 :view (fn [tags] [:div (for [tag (flatten tags)] [:a.tag.mr-1 {:href (str "#/page/" tag)} (str "[[" tag "]] ")] )] )
 :rules [
   [(pr ?p ?tag)
     [?b :block/page ?p]
     [?b :block/refs ?ref]
     [?ref :block/name ?tag]
   ]
   [(pr ?p ?tag)
     [?b :block/page ?p]
     [?b :block/refs ?ref]
     (pr ?ref ?tag)
   ]
 ]
}
#+END_QUERY

Pages to which the current one refers directly

#+BEGIN_QUERY
{:query [:find ?tag
   :in $ ?current
   :where
     [?p :block/name ?current]
     [?b :block/page ?p]
     [?b :block/refs ?ref]
     [?ref :block/name ?tag]
 ]
 :inputs [:current-page]
 :result-transform (fn [result] (sort result))
 :view (fn [tags] [:div (for [tag (flatten tags)] [:a.tag.mr-1 {:href (str "#/page/" tag)} (str "[[" tag "]] ")] )] )
}
#+END_QUERY

Pages that refer to the current directly

#+BEGIN_QUERY
{:query [:find ?tag
   :in $ ?current
   :where
     [?p :block/name ?current]
     [?b :block/refs ?p]
     [?b :block/page ?page]
     [?page :block/name ?tag]
 ]
 :inputs [:current-page]
 :result-transform (fn [result] (sort result))
 :view (fn [tags] [:div (for [tag (flatten tags)] [:a.tag.mr-1 {:href (str "#/page/" tag)} (str "[[" tag "]] ")] )] )
}
#+END_QUERY