Browse Source

fix: properties insert

Tienson Qin 4 years ago
parent
commit
a449ca96ad
2 changed files with 35 additions and 31 deletions
  1. 4 0
      src/main/frontend/format/mldoc.cljs
  2. 31 31
      src/main/frontend/util/property.cljs

+ 4 - 0
src/main/frontend/format/mldoc.cljs

@@ -267,3 +267,7 @@
 (defn plain->text
   [plains]
   (string/join (map last plains)))
+
+(defn properties?
+  [ast]
+  (contains? #{"Properties" "Property_Drawer"} (ffirst ast)))

+ 31 - 31
src/main/frontend/util/property.cljs

@@ -157,6 +157,9 @@
               (not (string/blank? (str value))))
      (let [ast (mldoc/->edn content (mldoc/default-config format))
            title? (mldoc/block-with-title? (ffirst (map first ast)))
+           has-properties? (or (and title?
+                                    (mldoc/properties? (second ast)))
+                               (mldoc/properties? (first ast)))
            lines (string/split-lines content)
            [title body] (if title?
                           [(first lines) (string/join "\n" (rest lines))]
@@ -167,13 +170,13 @@
            start-idx (.indexOf lines properties-start)
            end-idx (.indexOf lines properties-end)]
        (cond
-         (and org? (not (contains-properties? content)))
+         (and org? (not has-properties?))
          (let [properties (build-properties-str format {key value})]
            (if title
              (str title "\n" properties body)
              (str properties content)))
 
-         (and (>= start-idx 0) (> end-idx 0) (> end-idx start-idx))
+         (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
@@ -197,35 +200,32 @@
                new-property-s (str key sym  value)
                property-f (if front-matter? front-matter-property? simplified-property?)
                groups (partition-by property-f lines)
-               no-properties? (and (= 1 (count groups))
-                                   (not (property-f (ffirst groups))))
-               lines (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))
-                             groups)
-               lines (if no-properties?
-                       (cond
-                         (string/blank? content)
-                         [new-property-s]
-
-                         title?
-                         (cons (first lines) (cons new-property-s (rest lines)))
-
-                         :else
-                         (cons new-property-s lines))
-                       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))
+                                      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