Skip to content

Instantly share code, notes, and snippets.

@tombarys
Last active February 27, 2024 09:48
Show Gist options
  • Save tombarys/7f5a8d71032039083f4fa6e032b40d84 to your computer and use it in GitHub Desktop.
Save tombarys/7f5a8d71032039083f4fa6e032b40d84 to your computer and use it in GitHub Desktop.
Better-search for Roam (V 2.11 improved + sorting)
(ns better-search-V2.1
(:require [reagent.core :as r]
[roam.datascript :as rd]
[roam.util :refer [parse]]))
(defn query-list [s-type search]
"creates a list of pages"
(sort (case s-type
:starts (rd/q '[:find ?fulltitle
:in $ ?search
:where [?e :node/title ?fulltitle]
[(clojure.string/starts-with? ?fulltitle ?search)]] search)
:ends (rd/q '[:find ?fulltitle
:in $ ?search
:where [?e :node/title ?fulltitle]
[(clojure.string/ends-with? ?fulltitle ?search)]] search)
:includes (rd/q '[:find ?fulltitle
:in $ ?search
:where [?e :node/title ?fulltitle]
[(clojure.string/includes? ?fulltitle ?search)]] search)
[])))
(defn page-title-link [page-title]
"returns proper [[Roam page]] link"
[:div (parse (str "[[" (first page-title) "]]"))])
(defn main [& [_ s-type search & _]]
[:div
(if (and (not= search nil)
(not= search "")
(contains? #{:starts :ends :includes} s-type))
(map page-title-link (query-list s-type search))
[:div [:font {:color "red"} [:b "Error in search parameters. Both are mandatory and searched text has to be in quotes."]]
[:span "\nFirst: :starts | :ends | :includes. Second: \"the searched text\".\nExample: "] [:code "{{roam/render: ((_your_uid_)) :starts \"PN/\"}}"]])])
@tombarys
Copy link
Author

tombarys commented Jun 2, 2022

Improved version 2.1 does not need entering your-graph-name and properly parses page links! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment