Browse Source

fix: Can't rename pages with nested titles

close #3245
Tienson Qin 4 years ago
parent
commit
03cfc60a7e
2 changed files with 25 additions and 11 deletions
  1. 16 0
      src/main/frontend/format/block.cljs
  2. 9 11
      src/main/frontend/handler/page.cljs

+ 16 - 0
src/main/frontend/format/block.cljs

@@ -718,6 +718,22 @@
                    (assoc :block/warning :multiple-blocks))]
                    (assoc :block/warning :multiple-blocks))]
        (if uuid (assoc block :block/uuid uuid) block)))))
        (if uuid (assoc block :block/uuid uuid) block)))))
 
 
+(defn parse-title-and-body
+  [format pre-block? content]
+  (def content content)
+  (let [ast (format/to-edn content format nil)
+        content (if pre-block? content
+                    (str (config/get-block-pattern format) " " (string/triml content)))
+        content (property/remove-properties format content)
+        ast (->> (format/to-edn content format nil)
+                 (map first))
+        title (when (heading-block? (first ast))
+                (:title (second (first ast))))]
+    (cond->
+      {:block/body (vec (if title (rest ast) ast))}
+      title
+      (assoc :block/title title))))
+
 (defn macro-subs
 (defn macro-subs
   [macro-content arguments]
   [macro-content arguments]
   (loop [s macro-content
   (loop [s macro-content

+ 9 - 11
src/main/frontend/handler/page.cljs

@@ -340,11 +340,8 @@
         page-ids (->> (map :block/page blocks)
         page-ids (->> (map :block/page blocks)
                       (remove nil?)
                       (remove nil?)
                       (set))
                       (set))
-        tx       (->> (map (fn [{:block/keys [uuid title content properties] :as block}]
-                             (let [title      (let [title' (walk-replace-old-page! title old-original-name new-name)]
-                                                (when-not (= title' title)
-                                                  title'))
-                                   content    (let [content' (replace-old-page! content old-original-name new-name)]
+        tx       (->> (map (fn [{:block/keys [uuid title content properties format pre-block?] :as block}]
+                             (let [content    (let [content' (replace-old-page! content old-original-name new-name)]
                                                 (when-not (= content' content)
                                                 (when-not (= content' content)
                                                   content'))
                                                   content'))
                                    properties (let [properties' (walk-replace-old-page! properties old-original-name new-name)]
                                    properties (let [properties' (walk-replace-old-page! properties old-original-name new-name)]
@@ -352,12 +349,13 @@
                                                   properties'))]
                                                   properties'))]
                                (when (or title content properties)
                                (when (or title content properties)
                                  (util/remove-nils-non-nested
                                  (util/remove-nils-non-nested
-                                  {:block/uuid       uuid
-                                   :block/title      title
-                                   :block/content    content
-                                   :block/properties properties
-                                   :block/refs (rename-update-block-refs! (:block/refs block) (:db/id page) (:db/id to-page))
-                                   :block/path-refs (rename-update-block-refs! (:block/path-refs block) (:db/id page) (:db/id to-page))})))) blocks)
+                                  (merge
+                                   {:block/uuid       uuid
+                                    :block/content    content
+                                    :block/properties properties
+                                    :block/refs (rename-update-block-refs! (:block/refs block) (:db/id page) (:db/id to-page))
+                                    :block/path-refs (rename-update-block-refs! (:block/path-refs block) (:db/id page) (:db/id to-page))}
+                                   (block/parse-title-and-body format pre-block? content)))))) blocks)
                       (remove nil?))]
                       (remove nil?))]
     (db/transact! repo tx)
     (db/transact! repo tx)
     (doseq [page-id page-ids]
     (doseq [page-id page-ids]