|
@@ -108,21 +108,22 @@
|
|
|
:title - page's title (original name)
|
|
|
:children - tree
|
|
|
"
|
|
|
- [{:keys [id title children] :as tree}]
|
|
|
+ [{:keys [uuid title children] :as tree}]
|
|
|
(let [has-children? (seq children)
|
|
|
page-format (some-> tree (:children) (first) (:format))]
|
|
|
(try (page/create! title {:redirect? false
|
|
|
- :format page-format
|
|
|
- :uuid id})
|
|
|
+ :format page-format
|
|
|
+ :uuid uuid})
|
|
|
(catch js/Error e
|
|
|
(notification/show! (str "Error happens when creating page " title ":\n"
|
|
|
e
|
|
|
"\nSkipped and continue the remaining import.") :error)))
|
|
|
(when has-children?
|
|
|
- (let [page-block (db/entity [:block/name (util/page-name-sanity-lc title)])]
|
|
|
+ (let [page-block (db/entity [:block/name (util/page-name-sanity-lc title)])
|
|
|
+ first-child (first (:block/_left page-block)) ]
|
|
|
;; Missing support for per block format (or deprecated?)
|
|
|
(try (editor/insert-block-tree children page-format
|
|
|
- {:target-block page-block
|
|
|
+ {:target-block first-child
|
|
|
:sibling? true
|
|
|
:keep-uuid? true})
|
|
|
(catch js/Error e
|
|
@@ -145,37 +146,34 @@
|
|
|
"Not rely on file system - backend compatible.
|
|
|
tree-translator-fn: translate exported tree structure to the desired tree for import"
|
|
|
[data tree-translator-fn]
|
|
|
- (when-let [_repo (state/get-current-repo)]
|
|
|
- (try (->> (:blocks data)
|
|
|
- (map tree-translator-fn)
|
|
|
- (pre-transact-uuids)
|
|
|
- (mapv create-page-with-exported-tree!))
|
|
|
- (editor/set-blocks-id! (db/get-all-referenced-blocks-uuid))
|
|
|
- (catch js/Error e
|
|
|
- (notification/show! (str "Error happens when importing:\n" e) :error)))))
|
|
|
+ (try (->> (:blocks data)
|
|
|
+ (map tree-translator-fn)
|
|
|
+ (pre-transact-uuids)
|
|
|
+ (mapv create-page-with-exported-tree!))
|
|
|
+ (editor/set-blocks-id! (db/get-all-referenced-blocks-uuid))
|
|
|
+ (catch js/Error e
|
|
|
+ (notification/show! (str "Error happens when importing:\n" e) :error))))
|
|
|
|
|
|
(defn tree-vec-translate-edn
|
|
|
"Actions to do for loading edn tree structure.
|
|
|
1) Removes namespace `:block/` from all levels of the `tree-vec`
|
|
|
2) Rename all :block/page-name to :title
|
|
|
- 3) Rename all :block/id to :uuid
|
|
|
- 4) Dissoc all :properties"
|
|
|
+ 3) Rename all :block/id to :uuid"
|
|
|
([tree-vec]
|
|
|
- (let [rm-kw-ns-fn #(-> %
|
|
|
+ (let [kw-trans-fn #(-> %
|
|
|
str
|
|
|
(string/replace ":block/page-name" ":block/title")
|
|
|
(string/replace ":block/id" ":block/uuid")
|
|
|
(string/replace ":block/" "")
|
|
|
keyword)
|
|
|
- transform-map (fn [form]
|
|
|
- (if (map? form)
|
|
|
- ;; build a new map with the same keys but without the namespace
|
|
|
- (reduce-kv (fn [acc k v]
|
|
|
- (if (not= :block/properties k)
|
|
|
- (assoc acc (rm-kw-ns-fn k) v)
|
|
|
- acc)) {} form)
|
|
|
+ map-trans-fn (fn [acc k v]
|
|
|
+ (assoc acc (kw-trans-fn k) v))
|
|
|
+ tree-trans-fn (fn [form]
|
|
|
+ (if (and (map? form)
|
|
|
+ (:block/id form))
|
|
|
+ (reduce-kv map-trans-fn {} form)
|
|
|
form))]
|
|
|
- (walk/postwalk transform-map tree-vec))))
|
|
|
+ (walk/postwalk tree-trans-fn tree-vec))))
|
|
|
|
|
|
(defn import-from-edn!
|
|
|
[raw finished-ok-handler]
|
|
@@ -186,27 +184,27 @@
|
|
|
"Actions to do for loading json tree structure.
|
|
|
1) Rename all :id to :uuid
|
|
|
2) Rename all :page-name to :title
|
|
|
- 3) Dissoc all :properties
|
|
|
- 4) Rename all :format \"markdown\" to :format `:markdown`"
|
|
|
+ 3) Rename all :format \"markdown\" to :format `:markdown`"
|
|
|
([tree-vec]
|
|
|
- (let [rm-kw-ns-fn #(-> %
|
|
|
+ (let [kw-trans-fn #(-> %
|
|
|
str
|
|
|
(string/replace ":page-name" ":title")
|
|
|
(string/replace ":id" ":uuid")
|
|
|
(string/replace #"^:" "")
|
|
|
keyword)
|
|
|
- transform-map (fn [form]
|
|
|
- (if (map? form)
|
|
|
- (reduce-kv (fn [acc k v]
|
|
|
- (if (not= :properties k)
|
|
|
- (let [k (rm-kw-ns-fn k)
|
|
|
- v (if (= k :format) (keyword v) v)]
|
|
|
- (assoc acc k v))
|
|
|
- acc)) {} form)
|
|
|
- form))
|
|
|
- _ (prn tree-vec)
|
|
|
- _ (prn (walk/postwalk transform-map tree-vec))]
|
|
|
- (walk/postwalk transform-map tree-vec))))
|
|
|
+ map-trans-fn (fn [acc k v]
|
|
|
+ (cond (= :format k)
|
|
|
+ (assoc acc (kw-trans-fn k) (keyword v))
|
|
|
+ (= :id k)
|
|
|
+ (assoc acc (kw-trans-fn k) (uuid v))
|
|
|
+ :else
|
|
|
+ (assoc acc (kw-trans-fn k) v)))
|
|
|
+ tree-trans-fn (fn [form]
|
|
|
+ (if (and (map? form)
|
|
|
+ (:id form))
|
|
|
+ (reduce-kv map-trans-fn {} form)
|
|
|
+ form))]
|
|
|
+ (walk/postwalk tree-trans-fn tree-vec))))
|
|
|
|
|
|
(defn import-from-json!
|
|
|
[raw finished-ok-handler]
|