Sfoglia il codice sorgente

enhance: use search normalize

Junyi Du 3 anni fa
parent
commit
ebd8e94c23

+ 1 - 1
src/main/frontend/components/editor.cljs

@@ -117,7 +117,7 @@
               matched-pages (when-not (string/blank? q)
                               (editor-handler/get-matched-pages q))
               matched-pages (cond
-                              (contains? (set (map string/lower-case matched-pages)) (string/trim q))
+                              (contains? (set (map util/search-normalize matched-pages)) (util/search-normalize (string/trim q)))
                               matched-pages
 
                               (empty? matched-pages)

+ 2 - 2
src/main/frontend/components/search.cljs

@@ -105,8 +105,8 @@
           search-mode (state/sub :search/mode)
           new-page (if (or
                         (and (seq pages)
-                             (= (util/safe-page-name-sanity-lc search-q)
-                                (util/safe-page-name-sanity-lc (:data (first pages)))))
+                             (= (util/safe-search-normalize search-q)
+                                (util/safe-search-normalize (:data (first pages)))))
                         (nil? result)
                         all?)
                      []

+ 2 - 1
src/main/frontend/handler/editor.cljs

@@ -1785,6 +1785,7 @@
         (text/wrapped-by? value pos before end)))))
 
 (defn get-matched-pages
+  "Return matched page names"
   [q]
   (let [block (state/get-edit-block)
         editing-page (and block
@@ -1793,7 +1794,7 @@
         pages (search/page-search q 20)]
     (if editing-page
       ;; To prevent self references
-      (remove (fn [p] (= (string/lower-case p) editing-page)) pages)
+      (remove (fn [p] (= (util/page-name-sanity-lc p) editing-page)) pages)
       pages)))
 
 (defn get-matched-blocks

+ 4 - 2
src/main/frontend/search.cljs

@@ -100,6 +100,7 @@
     (protocol/transact-blocks! engine data)))
 
 (defn exact-matched?
+  "Check if two strings the same thing"
   [q match]
   (when (and (string? q) (string? match))
     (boolean
@@ -109,10 +110,11 @@
           (if (seq coll')
             (rest coll')
             (reduced false))))
-      (seq (string/lower-case match))
-      (seq (string/lower-case q))))))
+      (seq (util/search-normalize match))
+      (seq (util/search-normalize q))))))
 
 (defn page-search
+  "Return a list of page names that match the query"
   ([q]
    (page-search q 10))
   ([q limit]

+ 7 - 2
src/main/frontend/util.cljc

@@ -1143,13 +1143,18 @@
   (.normalize s "NFC"))
 
 (defn search-normalize
-  "Normalize string for searching"
+  "Normalize string for searching (loose)"
   [s]
   (.normalize (string/lower-case s) "NFKD")
 )
 
+(defn safe-search-normalize
+  [s]
+  (if (string? s)
+    (.normalize (string/lower-case s) "NFKD") s))
+
 (defn page-name-sanity
-  "Sanitize the page-name for file name"
+  "Sanitize the page-name for file name (strict)"
   ([page-name]
    (page-name-sanity page-name false))
   ([page-name replace-slash?]