Skip to content

Commit

Permalink
fix: add :feature/enable-search-remove-accents
Browse files Browse the repository at this point in the history
  • Loading branch information
maxweilun1989 committed Jul 27, 2022
1 parent f26681c commit 15a0f19
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/main/frontend/components/search.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
content
(when (and content q)
(let [q-words (string/split q #" ")
lc-content (util/search-normalize content)
lc-q (util/search-normalize q)]
lc-content (util/search-normalize content (state/enable-search-remove-accents?))
lc-q (util/search-normalize q (state/enable-search-remove-accents?))]
(if (and (string/includes? lc-content lc-q)
(not (util/safe-re-find #" " q)))
(let [i (string/index-of lc-content lc-q)
Expand All @@ -46,8 +46,8 @@
result []]
(if (and (seq words) content)
(let [word (first words)
lc-word (util/search-normalize word)
lc-content (util/search-normalize content)]
lc-word (util/search-normalize word (state/enable-search-remove-accents?))
lc-content (util/search-normalize content (state/enable-search-remove-accents?))]
(if-let [i (string/index-of lc-content lc-word)]
(recur (rest words)
(subs content (+ i (count word)))
Expand Down
12 changes: 6 additions & 6 deletions src/main/frontend/search.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,20 @@
(defn fuzzy-search
[data query & {:keys [limit extract-fn]
:or {limit 20}}]
(let [query (util/search-normalize query)]
(let [query (util/search-normalize query (state/enable-search-remove-accents?))]
(->> (take limit
(sort-by :score (comp - compare)
(filter #(< 0 (:score %))
(for [item data]
(let [s (str (if extract-fn (extract-fn item) item))]
{:data item
:score (score query (util/search-normalize s))})))))
:score (score query (util/search-normalize s (state/enable-search-remove-accents?)))})))))
(map :data))))

(defn block-search
[repo q option]
(when-let [engine (get-engine repo)]
(let [q (util/search-normalize q)
(let [q (util/search-normalize q (state/enable-search-remove-accents?))
q (if (util/electron?) q (escape-str q))]
(when-not (string/blank? q)
(protocol/query engine q option)))))
Expand All @@ -111,16 +111,16 @@
(if (seq coll')
(rest coll')
(reduced false))))
(seq (util/search-normalize match))
(seq (util/search-normalize q))))))
(seq (util/search-normalize match (state/enable-search-remove-accents?)))
(seq (util/search-normalize q (state/enable-search-remove-accents?)))))))

(defn page-search
"Return a list of page names that match the query"
([q]
(page-search q 10))
([q limit]
(when-let [repo (state/get-current-repo)]
(let [q (util/search-normalize q)
(let [q (util/search-normalize q (state/enable-search-remove-accents?))
q (clean-str q)]
(when-not (string/blank? q)
(let [indice (or (get-in @indices [repo :pages])
Expand Down
4 changes: 2 additions & 2 deletions src/main/frontend/search/db.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
(defn block->index
"Convert a block to the index for searching"
[{:block/keys [uuid page content] :as block}]
(when-let [content (util/search-normalize content)]
(when-let [content (util/search-normalize content (state/enable-search-remove-accents?))]
{:id (:db/id block)
:uuid (str uuid)
:page page
Expand Down Expand Up @@ -42,7 +42,7 @@
indice))

(defn original-page-name->index
[p] {:name (util/search-normalize p)
[p] {:name (util/search-normalize p (state/enable-search-remove-accents?))
:original-name p})

(defn make-pages-indice!
Expand Down
5 changes: 5 additions & 0 deletions src/main/frontend/state.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1691,3 +1691,8 @@
(defn unlinked-dir?
[dir]
(contains? (:file/unlinked-dirs @state) dir))

(defn enable-search-remove-accents?
[]
(:feature/enable-search-remove-accents?
(get (sub-config) (get-current-repo))))
7 changes: 5 additions & 2 deletions src/main/frontend/util.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -917,8 +917,11 @@
#?(:cljs
(defn search-normalize
"Normalize string for searching (loose)"
[s]
(removeAccents (.normalize (string/lower-case s) "NFKC"))))
[s remove-accents?]
(let [normalize-str (.normalize (string/lower-case s) "NFKC")]
(if remove-accents?
(removeAccents normalize-str)
normalize-str))))

#?(:cljs
(defn file-name-sanity
Expand Down
3 changes: 3 additions & 0 deletions templates/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
;; Enable Block timestamp
:feature/enable-block-timestamps? false

;; Enable remove accents when search
:feature/enable-search-remove-accents? true

;; Disable Built-in Scheduled and deadline Query
;; :feature/disable-scheduled-and-deadline-query? true

Expand Down

0 comments on commit 15a0f19

Please sign in to comment.