Browse Source

frontend.text conversion WIP

Removed extract-level-spaces b/c it's not used
Gabriel Horner 3 years ago
parent
commit
f529777d78

+ 1 - 1
src/main/frontend/format/block.cljs

@@ -425,7 +425,7 @@
                   (utf8/substring utf8-content
                                   (:start_pos meta)))
         content (when content
-                  (let [content (text/remove-level-spaces content format)]
+                  (let [content (text/remove-level-spaces content format (config/get-block-pattern format))]
                     (if (or (:pre-block? block)
                             (= (:format block) :org))
                       content

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

@@ -1314,7 +1314,7 @@
 
 (defn- clean-content!
   [format content]
-  (->> (text/remove-level-spaces content format)
+  (->> (text/remove-level-spaces content format (config/get-block-pattern format))
        (drawer/remove-logbook)
        (property/remove-properties format)
        string/trim))

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

@@ -1,5 +1,6 @@
 (ns frontend.handler.route
   (:require [clojure.string :as string]
+            [frontend.config :as config]
             [frontend.date :as date]
             [frontend.db :as db]
             [frontend.handler.ui :as ui-handler]
@@ -82,7 +83,7 @@
       (if block?
         (if-let [block (db/entity [:block/uuid (medley/uuid name)])]
           (let [content (text/remove-level-spaces (:block/content block)
-                                                  (:block/format block))]
+                                                  (:block/format block) (config/get-block-pattern (:block/format block)))]
             (if (> (count content) 48)
               (str (subs content 0 48) "...")
               content))

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

@@ -1,5 +1,6 @@
 (ns frontend.handler.search
   (:require [clojure.string :as string]
+            [frontend.config :as config]
             [frontend.db :as db]
             [frontend.handler.notification :as notification-handler]
             [frontend.search :as search]
@@ -21,7 +22,7 @@
 (defn sanity-search-content
   "Convert a block to the display contents for searching"
   [format content]
-  (->> (text/remove-level-spaces content format)
+  (->> (text/remove-level-spaces content format (config/get-block-pattern format))
        (drawer/remove-logbook)
        (property/remove-built-in-properties format)))
 
@@ -78,4 +79,4 @@
        (p/let [_ (search/rebuild-indices! repo)]
          (notification-handler/show!
           "Stale search cache detected. Search indices rebuilt successfully!"
-          :success))))))
+          :success))))))

+ 7 - 17
src/main/frontend/text.cljs

@@ -1,6 +1,5 @@
 (ns frontend.text
-  (:require [frontend.config :as config]
-            [frontend.util :as util]
+  (:require [frontend.util :as util]
             [clojure.string :as string]
             [clojure.set :as set]
             [logseq.graph-parser.mldoc :as gp-mldoc]
@@ -187,15 +186,6 @@
      :else
      s)))
 
-(defn extract-level-spaces
-  [text format]
-  (if-not (string/blank? text)
-    (let [pattern (util/format
-                   "^[%s]+\\s?"
-                   (config/get-block-pattern format))]
-      (gp-util/safe-re-find (re-pattern pattern) text))
-    ""))
-
 (defn- remove-level-space-aux!
   [text pattern space? trim-left?]
   (let [pattern (util/format
@@ -207,11 +197,11 @@
     (string/replace-first text (re-pattern pattern) "")))
 
 (defn remove-level-spaces
-  ([text format]
-   (remove-level-spaces text format false true))
-  ([text format space?]
-   (remove-level-spaces text format space? true))
-  ([text format space? trim-left?]
+  ([text format block-pattern]
+   (remove-level-spaces text format block-pattern false true))
+  ([text format block-pattern space?]
+   (remove-level-spaces text format block-pattern space? true))
+  ([text format block-pattern space? trim-left?]
    (when format
      (cond
        (string/blank? text)
@@ -222,7 +212,7 @@
        text
 
        :else
-       (remove-level-space-aux! text (config/get-block-pattern format) space? trim-left?)))))
+       (remove-level-space-aux! text block-pattern space? trim-left?)))))
 
 (defn build-data-value
   [col]

+ 5 - 17
src/test/frontend/text_test.cljs

@@ -1,5 +1,6 @@
 (ns frontend.text-test
   (:require [cljs.test :refer [are deftest testing]]
+            [frontend.config :as config]
             [frontend.text :as text]))
 
 (deftest test-get-page-name
@@ -74,35 +75,22 @@
     "#tag1,#tag2" #{"tag1" "tag2"}
     "[[Jan 26th, 2021]], hello" #{"hello" "Jan 26th, 2021"}))
 
-(deftest extract-level-spaces
-  []
-  (testing "markdown"
-    (are [x y] (= (text/extract-level-spaces x :markdown) y)
-      "- foobar" "- "
-      "--   foobar" "-- "
-      "---------------------   foobar" "--------------------- "))
-  (testing "org mode"
-    (are [x y] (= (text/extract-level-spaces x :org) y)
-      "* foobar" "* "
-      "**   foobar" "** "
-      "*********************  foobar" "********************* ")))
-
 (deftest remove-level-spaces
   []
   (testing "markdown"
-    (are [x y] (= (text/remove-level-spaces x :markdown true) y)
+    (are [x y] (= (text/remove-level-spaces x :markdown (config/get-block-pattern :markdown) true) y)
       "- foobar" "foobar"
       " - foobar" "foobar"))
   (testing "markdown without spaces between the `#` and title"
-    (are [x y] (= (text/remove-level-spaces x :markdown) y)
+    (are [x y] (= (text/remove-level-spaces x :markdown (config/get-block-pattern :markdown)) y)
       "-foobar" "foobar"))
   (testing "org"
-    (are [x y] (= (text/remove-level-spaces x :org true) y)
+    (are [x y] (= (text/remove-level-spaces x :org (config/get-block-pattern :org) true) y)
       "* foobar" "foobar"
       "**   foobar" "foobar"
       "*********************   foobar" "foobar"))
   (testing "org without spaces between the `#` and title"
-    (are [x y] (= (text/remove-level-spaces x :org) y)
+    (are [x y] (= (text/remove-level-spaces x :org (config/get-block-pattern :org)) y)
       "*foobar" "foobar"
       "**foobar" "foobar"
       "*********************foobar" "foobar")))