Browse Source

feat: highlight page content fts result

Junyi 3 years ago
parent
commit
95d79877c0

+ 24 - 42
src/main/frontend/components/search.cljs

@@ -22,7 +22,8 @@
             [frontend.context.i18n :refer [t]]
             [frontend.date :as date]
             [reitit.frontend.easy :as rfe]
-            [frontend.modules.shortcut.core :as shortcut]))
+            [frontend.modules.shortcut.core :as shortcut]
+            [frontend.util.text :as text-util]))
 
 (defn highlight-exact-query
   [content q]
@@ -62,41 +63,23 @@
                              (conj result [:span content])))]
             [:p {:class "m-0"} elements]))))))
 
-;; (def fts-mark)
-
-;; (defn highlight-page-content-query
-;;   [content q]
-;;   (if (or (string/blank? content) (string/blank? q))
-;;     content
-;;     (if (and (string/includes? )
-;;              (not (util/safe-re-find #" " q)))
-;;       (let [i (string/index-of lc-content lc-q)
-;;             [before after] [(subs content 0 i) (subs content (+ i (count q)))]]
-;;         [:div
-;;          (when-not (string/blank? before)
-;;            [:span before])
-;;          [:mark.p-0.rounded-none (subs content i (+ i (count q)))]
-;;          (when-not (string/blank? after)
-;;            [:span after])])
-;;       (let [elements (loop [words q-words
-;;                             content content
-;;                             result []]
-;;                        (if (and (seq words) content)
-;;                          (let [word (first words)
-;;                                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)))
-;;                                     (vec
-;;                                      (concat result
-;;                                              [[:span (subs content 0 i)]
-;;                                               [:mark.p-0.rounded-none (subs content i (+ i (count word)))]])))
-;;                              (recur nil
-;;                                     content
-;;                                     result)))
-;;                          (conj result [:span content])))]
-;;         [:p {:class "m-0"} elements]))))
+(defn highlight-page-content-query
+  "Return hiccup of highlighted page content FTS result"
+  [content q]
+  (when-not (or (string/blank? content) (string/blank? q))
+    [:div (loop [content content
+                 result  []]
+            (let [_ (prn "content" content "result" result)
+                  [b-cut hl-cut e-cut] (text-util/cut-by content "$pfts_2lqh>$" "$<pfts_2lqh$")
+                  hiccups-add [(when-not (string/blank? b-cut)
+                                 [:span b-cut])
+                               (when-not (string/blank? hl-cut)
+                                 [:mark.p-0.rounded-none hl-cut])]
+                  hiccups-add (remove nil? hiccups-add)
+                  new-result (concat result hiccups-add)]
+              (if-not (string/blank? e-cut)
+                (recur e-cut new-result)
+                new-result)))]))
 
 (rum/defc search-result-item
   [icon content]
@@ -105,7 +88,7 @@
    [:.self-center content]])
 
 (rum/defc page-content-search-result-item
-  [repo uuid snippet _q search-mode]
+  [repo uuid format snippet q search-mode]
   [:div
    (when (not= search-mode :page)
      [:div {:class "mb-1" :key "parents"}
@@ -116,9 +99,7 @@
                         (clojure.core/uuid uuid)
                         {:indent? false})])
    [:div {:class "font-medium" :key "content"}
-    ;; (highlight-page-content-query snippet q)
-    snippet
-    ]])
+    (highlight-page-content-query (search-handler/sanity-search-content format snippet) q)]])
 
 (rum/defc block-search-result-item
   [repo uuid format content q search-mode]
@@ -309,13 +290,14 @@
        :page-content
        (let [{:block/keys [snippet uuid]} data  ;; content here is normalized
              repo (state/sub :git/current-repo)
-             page (model/query-block-by-uuid uuid)] ;; it's actually a page
+             page (model/query-block-by-uuid uuid)  ;; it's actually a page
+             format (db/get-page-format page)]
          [:span {:data-block-ref uuid}
           (search-result-item {:name "page"
                                :title (t :search-item/page)
                                :extension? true}
                               (if page
-                                (page-content-search-result-item repo uuid snippet search-q search-mode)
+                                (page-content-search-result-item repo uuid format snippet search-q search-mode)
                                 (do (log/error "search result with non-existing uuid: " data)
                                     (str "Cache is outdated. Please click the 'Re-index' button in the graph's dropdown menu."))))])
 

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

@@ -45,8 +45,7 @@
                         page-db-id)
            opts (if page-db-id (assoc opts :page (str page-db-id)) opts)]
        (p/let [blocks (search/block-search repo q opts)
-               pages-content (search/page-content-search repo q opts)
-               _ (prn pages-content)]
+               pages-content (search/page-content-search repo q opts)]
          (let [result (merge
                        {:blocks blocks
                         :has-more? (= limit (count blocks))}

+ 18 - 0
src/main/frontend/util/text.cljs

@@ -118,6 +118,24 @@
              []
              ks))))
 
+(defn cut-by
+  "Cut string by specifid wrapping symbols, only match the first occurrence.
+     value - string to cut"
+  [value before end]
+  (let [b-pos (string/index-of value before)
+        b-len (count before)]
+    (if b-pos
+      (let [b-cut (subs value 0 b-pos)
+            m-cut (subs value (+ b-pos b-len))
+            e-len (count end)
+            e-pos (string/index-of m-cut end)]
+        (if e-pos
+          (let [e-cut (subs m-cut (+ e-pos e-len))
+                m-cut (subs m-cut 0 e-pos)]
+            [b-cut m-cut e-cut])
+          [b-cut m-cut nil]))
+      [value nil nil])))
+
 (defn get-graph-name-from-path
   [path]
   (when (string? path)

+ 34 - 0
src/test/frontend/util/text_test.cljs

@@ -57,3 +57,37 @@
     '(false false false false false false true true true true true true)
     (map #(text-util/wrapped-by? "prop::value" % "::" "") (take 12 (range)))
     ))
+
+
+(deftest test-cut-by
+  []
+  (are [x y] (= x y)
+    ["" "" ""]
+    (text-util/cut-by "[[]]" "[[" "]]")
+
+    ["" "abc" ""]
+    (text-util/cut-by "[[abc]]" "[[" "]]")
+
+    ["012 " "6" " [[2]]"]
+    (text-util/cut-by "012 [[6]] [[2]]" "[[" "]]")
+
+    ["" "prop" "value"]
+    (text-util/cut-by "prop::value" "" "::")
+
+    ["prop" "" "value"]
+    (text-util/cut-by "prop::value" "::" "")
+
+    ["some" "content" "here"]
+    (text-util/cut-by "some $pfts>$content$pfts<$ here" "$pfts>$" "$pfts<$")
+
+    ["some" "content$pft" nil]
+    (text-util/cut-by "some $pfts>$content$pft" "$pfts>$" "$pfts<$")
+
+    ["some $pf" nil nil]
+    (text-util/cut-by "some $pf" "$pfts>$" "$pfts<$")
+
+    ["" "content" ""]
+    (text-util/cut-by "$pfts>$content$pfts<$" "$pfts>$" "$pfts<$")
+    
+    ["" "content$p" nil]
+    (text-util/cut-by "$pfts>$content$p" "$pfts>$" "$pfts<$")))