Last active
February 27, 2024 09:48
-
-
Save tombarys/7f5a8d71032039083f4fa6e032b40d84 to your computer and use it in GitHub Desktop.
Better-search for Roam (V 2.11 improved + sorting)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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/\"}}"]])]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Improved version 2.1 does not need entering
your-graph-name
and properly parses page links! :)