Files
logseq/src/main/frontend/search/db.cljs
2021-04-08 13:52:47 +08:00

61 lines
1.9 KiB
Clojure

(ns frontend.search.db
(:refer-clojure :exclude [empty?])
(:require [frontend.text :as text]
[frontend.db :as db]
[frontend.state :as state]
[cljs-bean.core :as bean]
["fuse.js" :as fuse]
[clojure.string :as string]))
(defonce indices (atom nil))
(defn empty?
[repo]
(nil? (get @indices repo)))
(defn block->index
[{:block/keys [uuid content format] :as block}]
(when-let [result (->> (text/remove-level-spaces content format)
(text/remove-properties!))]
{:id (:db/id block)
:uuid (str uuid)
:content result}))
(defn build-blocks-indice
[repo]
(->> (db/get-all-block-contents)
(map block->index)
(remove nil?)
(bean/->js)))
(defn make-blocks-indice!
[repo]
(let [blocks (build-blocks-indice repo)
indice (fuse. blocks
(clj->js {:keys ["uuid" "content"]
:shouldSort true
:tokenize true
:minMatchCharLength 2
:distance 1000
:threshold 0.35}))]
(swap! indices assoc-in [repo :blocks] indice)
indice))
(defn make-pages-indice!
[]
(when-let [repo (state/get-current-repo)]
(let [pages (->> (db/get-pages (state/get-current-repo))
(remove string/blank?)
(map (fn [p] {:name p}))
(bean/->js))
indice (fuse. pages
(clj->js {:keys ["name"]
:shouldSort true
:tokenize true
:minMatchCharLength 2
:distance 1000
:threshold 0.35
}))]
(swap! indices assoc-in [repo :pages] indice)
indice)))