Просмотр исходного кода

fix: invalid pages and whiteboard pages from import

- some pages are missing block/journal? and block/original-name
- removed another file graph only attribute
- fixed whiteboard block schema missing :block/path-refs
- pulled out importing block to its own fn

Part of LOG-2990
Gabriel Horner 1 год назад
Родитель
Сommit
3cb980bf91

+ 2 - 1
deps/db/src/logseq/db/frontend/malli_schema.cljs

@@ -230,7 +230,8 @@
     [[:block/content :string]
      [:block/parent :int]
      ;; These blocks only associate with pages of type "whiteboard"
-     [:block/page :int]]
+     [:block/page :int]
+     [:block/path-refs {:optional true} [:set :int]]]
     page-or-block-attrs)))
 
 (def closed-value-block

+ 55 - 48
deps/graph-parser/src/logseq/graph_parser.cljs

@@ -149,6 +149,52 @@ Options available:
                     tags)))
     block))
 
+(defn- update-imported-block
+  [conn block]
+  (prn ::block block)
+  (let [remove-keys (fn [m pred] (into {} (remove (comp pred key) m)))]
+    (-> block
+        ((fn [block']
+           (cond
+             (seq (:block/macros block'))
+             (update block' :block/macros
+                     (fn [macros]
+                       (mapv (fn [m]
+                               (-> m
+                                   (update :block/properties
+                                           (fn [props]
+                                             (update-keys #(get-pid @conn %) props)))
+                                   (assoc :block/uuid (d/squuid))))
+                             macros)))
+
+             (:block/pre-block? block')
+             block'
+
+             :else
+             (update-in block' [:block/properties]
+                        (fn [props]
+                          (-> props
+                              (update-keys (fn [k]
+                                             (if-let [new-key (get-pid @conn k)]
+                                               new-key
+                                               k)))
+                              (remove-keys keyword?)))))))
+        update-block-with-invalid-tags
+        ((fn [block']
+           (if (seq (:block/refs block'))
+             (update block' :block/refs
+                     (fn [refs]
+                       (mapv #(assoc % :block/format :markdown) refs)))
+             block')))
+        sqlite-util/block-with-timestamps
+        ;; FIXME: Remove when properties are supported
+        (assoc :block/properties {})
+        ;; TODO: org-mode content needs to be handled
+        (assoc :block/format :markdown)
+        ;; TODO: pre-block? can be removed once page properties are imported
+        (dissoc :block/pre-block? :block/properties-text-values :block/properties-order
+                :block/invalid-properties))))
+
 (defn import-file-to-db-graph
   "Parse file and save parsed data to the given db graph."
   [conn file content {:keys [delete-blocks-fn extract-options skip-db-transact?]
@@ -186,11 +232,17 @@ Options available:
                                   (seq))
                  ;; To prevent "unique constraint" on datascript
               block-ids (set/union (set block-ids) (set block-refs-ids))
-              pages (map #(-> %
+              pages (map #(-> (merge {:block/journal? false} %)
+                              ;; Fix pages missing :block/original-name. Shouldn't happen
+                              ((fn [m]
+                                 (if-not (:block/original-name m)
+                                   (assoc m :block/original-name (:block/name m))
+                                   m)))
                               sqlite-util/block-with-timestamps
                               ;; TODO: org-mode content needs to be handled
                               (assoc :block/format :markdown)
-                              (dissoc :block/properties-text-values :block/properties-order :block/invalid-properties)
+                              (dissoc :block/properties-text-values :block/properties-order :block/invalid-properties
+                                      :block/whiteboard?)
                               update-block-with-invalid-tags
                               ;; FIXME: Remove when properties are supported
                               (assoc :block/properties {}))
@@ -205,52 +257,7 @@ Options available:
                                                       :block/format :markdown
                                                       ;; fixme: missing properties
                                                       :block/properties {(get-pid @conn :ls-type) :whiteboard-page})))))
-              remove-keys (fn [m pred]
-                            (into {} (remove (comp pred key) m)))
-              blocks (map (fn [block]
-                            (prn ::block block)
-                            (-> (cond
-                                  (seq (:block/macros block))
-                                  (update block :block/macros
-                                          (fn [macros]
-                                            (mapv (fn [m]
-                                                    (-> m
-                                                        (update :block/properties
-                                                                (fn [props]
-                                                                  (update-keys #(get-pid @conn %) props)))
-                                                        (assoc :block/uuid (d/squuid))))
-                                                  macros)))
-
-                                  (:block/pre-block? block)
-                                  block
-
-                                  :else
-                                  (update-in block [:block/properties]
-                                             (fn [props]
-                                               (-> props
-                                                   (update-keys (fn [k]
-                                                                  (if-let [new-key (get-pid @conn k)]
-                                                                    new-key
-                                                                    k)))
-                                                   (remove-keys keyword?)))))
-                                update-block-with-invalid-tags
-                                ((fn [block]
-                                   (if (seq (:block/refs block))
-                                     (update block :block/refs
-                                             (fn [refs]
-                                               (mapv #(assoc % :block/format :markdown) refs)))
-                                     block)))
-                                sqlite-util/block-with-timestamps
-                                ;; FIXME: Remove when properties are supported
-                                (assoc :block/properties {})
-                                ;; TODO: org-mode content needs to be handled
-                                (assoc :block/format :markdown)
-                                ;; TODO: pre-block? can be removed once page properties are imported
-                                (dissoc :block/pre-block? :block/properties-text-values :block/properties-order
-                                        :block/invalid-properties)))
-                          blocks)
-
-
+              blocks (map #(update-imported-block conn %) blocks)
               pages-index (map #(select-keys % [:block/name]) pages)]
 
           {:tx (concat refs whiteboard-pages pages-index delete-blocks pages block-ids blocks)