Tienson Qin 4 years ago
parent
commit
fe105038d7

+ 62 - 61
src/main/frontend/util/property.cljs

@@ -168,68 +168,69 @@
            key (string/lower-case (name key))
            value (string/trim (str value))
            start-idx (.indexOf lines properties-start)
-           end-idx (.indexOf lines properties-end)]
-       (cond
-         (and org? (not has-properties?))
-         (let [properties (build-properties-str format {key value})]
-           (if title
-             (str title "\n" properties body)
-             (str properties content)))
-
-         (and has-properties? (>= start-idx 0) (> end-idx 0) (> end-idx start-idx))
-         (let [exists? (atom false)
-               before (subvec lines 0 start-idx)
-               middle (doall
-                       (->> (subvec lines (inc start-idx) end-idx)
-                            (mapv (fn [text]
-                                    (let [[k v] (util/split-first ":" (subs text 1))]
-                                      (if (and k v)
-                                        (let [key-exists? (= k key)
-                                              _ (when key-exists? (reset! exists? true))
-                                              v (if key-exists? value v)]
-                                          (str ":" k ": "  (string/trim v)))
-                                        text))))))
-               middle (if @exists? middle (conj middle (str ":" key ": "  value)))
-               after (subvec lines (inc end-idx))
-               lines (concat before [properties-start] middle [properties-end] after)]
-           (string/join "\n" lines))
-
-         (not org?)
-         (let [exists? (atom false)
-               sym (if front-matter? ": " ":: ")
-               new-property-s (str key sym  value)
-               property-f (if front-matter? front-matter-property? simplified-property?)
-               groups (partition-by property-f lines)
-               compose-lines (fn []
-                               (mapcat (fn [lines]
-                                        (if (property-f (first lines))
-                                          (let [lines (doall
-                                                       (mapv (fn [text]
-                                                               (let [[k v] (util/split-first sym text)]
-                                                                 (if (and k v)
-                                                                   (let [key-exists? (= k key)
-                                                                         _ (when key-exists? (reset! exists? true))
-                                                                         v (if key-exists? value v)]
-                                                                     (str k sym  (string/trim v)))
-                                                                   text)))
+           end-idx (.indexOf lines properties-end)
+           result        (cond
+                           (and org? (not has-properties?))
+                           (let [properties (build-properties-str format {key value})]
+                             (if title
+                               (str title "\n" properties body)
+                               (str properties content)))
+
+                           (and has-properties? (>= start-idx 0) (> end-idx 0) (> end-idx start-idx))
+                           (let [exists? (atom false)
+                                 before (subvec lines 0 start-idx)
+                                 middle (doall
+                                         (->> (subvec lines (inc start-idx) end-idx)
+                                              (mapv (fn [text]
+                                                      (let [[k v] (util/split-first ":" (subs text 1))]
+                                                        (if (and k v)
+                                                          (let [key-exists? (= k key)
+                                                                _ (when key-exists? (reset! exists? true))
+                                                                v (if key-exists? value v)]
+                                                            (str ":" k ": "  (string/trim v)))
+                                                          text))))))
+                                 middle (if @exists? middle (conj middle (str ":" key ": "  value)))
+                                 after (subvec lines (inc end-idx))
+                                 lines (concat before [properties-start] middle [properties-end] after)]
+                             (string/join "\n" lines))
+
+                           (not org?)
+                           (let [exists? (atom false)
+                                 sym (if front-matter? ": " ":: ")
+                                 new-property-s (str key sym  value)
+                                 property-f (if front-matter? front-matter-property? simplified-property?)
+                                 groups (partition-by property-f lines)
+                                 compose-lines (fn []
+                                                 (mapcat (fn [lines]
+                                                           (if (property-f (first lines))
+                                                             (let [lines (doall
+                                                                          (mapv (fn [text]
+                                                                                  (let [[k v] (util/split-first sym text)]
+                                                                                    (if (and k v)
+                                                                                      (let [key-exists? (= k key)
+                                                                                            _ (when key-exists? (reset! exists? true))
+                                                                                            v (if key-exists? value v)]
+                                                                                        (str k sym  (string/trim v)))
+                                                                                      text)))
+                                                                                lines))
+                                                                   lines (if @exists? lines (conj lines new-property-s))]
+                                                               lines)
                                                              lines))
-                                                lines (if @exists? lines (conj lines new-property-s))]
-                                            lines)
-                                          lines))
-                                      groups))
-               lines (cond
-                       has-properties?
-                       (compose-lines)
-
-                       title?
-                       (cons (first lines) (cons new-property-s (rest lines)))
-
-                       :else
-                       (cons new-property-s lines))]
-           (string/join "\n" lines))
-
-         :else
-         content)))))
+                                                         groups))
+                                 lines (cond
+                                         has-properties?
+                                         (compose-lines)
+
+                                         title?
+                                         (cons (first lines) (cons new-property-s (rest lines)))
+
+                                         :else
+                                         (cons new-property-s lines))]
+                             (string/join "\n" lines))
+
+                           :else
+                           content)]
+       (string/trimr result)))))
 
 (defn insert-properties
   [format content kvs]

+ 2 - 2
src/test/frontend/db/query_dsl_test.cljs

@@ -321,7 +321,7 @@ last-modified-at:: 1609084800002"}]]
       "(not [[page 1]])"
       {:query '([?b :block/uuid]
                 (not [?b :block/path-refs [:block/name "page 1"]]))
-       :count 31}))
+       :count 33}))
 
   (testing "Between query"
     (are [x y] (= (count-only x) y)
@@ -369,7 +369,7 @@ last-modified-at:: 1609084800002"}]]
                   (and [?b :block/path-refs [:block/name "page 1"]])
                   (and [?b :block/path-refs [:block/name "page 2"]])
                   [?b])))
-       :count 34})
+       :count 36})
 
     ;; FIXME: not working
     ;; (are [x y] (= (q-count x) y)

+ 3 - 3
src/test/frontend/util/property_test.cljs

@@ -50,10 +50,10 @@
 (deftest test-insert-property
   (are [x y] (= x y)
     (property/insert-property :org "hello" "a" "b")
-    "hello\n:PROPERTIES:\n:a: b\n:END:\n"
+    "hello\n:PROPERTIES:\n:a: b\n:END:"
 
     (property/insert-property :org "hello" "a" false)
-    "hello\n:PROPERTIES:\n:a: false\n:END:\n"
+    "hello\n:PROPERTIES:\n:a: false\n:END:"
 
     (property/insert-property :org "hello\n:PROPERTIES:\n:a: b\n:END:\n" "c" "d")
     "hello\n:PROPERTIES:\n:a: b\n:c: d\n:END:"
@@ -111,7 +111,7 @@
     "hello\n:PROPERTIES:\n:foo: bar\n:nice: bingo\n:END:"
     "hello\nfoo:: bar\nnice:: bingo"
 
-    "hello\n:PROPERTIES:\n:foo: bar\n:nice: bingo\n:END:\n"
+    "hello\n:PROPERTIES:\n:foo: bar\n:nice: bingo\n:END:"
     "hello\nfoo:: bar\nnice:: bingo"
 
     "hello\n:PROPERTIES:\n:foo: bar\n:nice: bingo\n:END:\nnice"