support parsing edn file as pages

pull/6112/head
Peng Xiao 2022-07-10 01:39:34 +08:00
parent d3dae5fddc
commit 06bfdee7b4
6 changed files with 61 additions and 32 deletions

View File

@ -23,35 +23,41 @@
:as options}] :as options}]
(db-set-file-content! conn file content) (db-set-file-content! conn file content)
(let [format (gp-util/get-format file) (let [format (gp-util/get-format file)
file-content [{:file/path file}] file-content [(cond-> {:file/path file}
{:keys [tx ast]} new?
(if (contains? gp-config/mldoc-support-formats format) ;; TODO: use file system timestamp?
(let [extract-options' (merge {:block-pattern (gp-config/get-block-pattern format) (assoc :file/created-at (date-time-util/time-ms)))]
:date-formatter "MMM do, yyyy" extract-options' (merge {:block-pattern (gp-config/get-block-pattern format)
:supported-formats (gp-config/supported-formats)} :date-formatter "MMM do, yyyy"
extract-options :supported-formats (gp-config/supported-formats)}
{:db @conn}) extract-options
{:keys [pages blocks ast]} {:db @conn})
(extract/extract file content extract-options') {:keys [pages blocks ast]
delete-blocks (delete-blocks-fn (first pages) file) :or {pages []
block-ids (map (fn [block] {:block/uuid (:block/uuid block)}) blocks) blocks []
block-refs-ids (->> (mapcat :block/refs blocks) ast []}}
(filter (fn [ref] (and (vector? ref) (cond (contains? gp-config/mldoc-support-formats format)
(= :block/uuid (first ref))))) (extract/extract file content extract-options')
(map (fn [ref] {:block/uuid (second ref)}))
(seq)) ;; File can be saved as plain edn (mostly, the whiteboard files)
;; To prevent "unique constraint" on datascript (= format :edn)
block-ids (set/union (set block-ids) (set block-refs-ids)) (gp-util/safe-read-string content)
pages (extract/with-ref-pages pages blocks)
pages-index (map #(select-keys % [:block/name]) pages)] :else nil)
;; does order matter?
{:tx (concat file-content pages-index delete-blocks pages block-ids blocks) delete-blocks (delete-blocks-fn (first pages) file)
:ast ast}) block-ids (map (fn [block] {:block/uuid (:block/uuid block)}) blocks)
{:tx file-content}) block-refs-ids (->> (mapcat :block/refs blocks)
tx (concat tx [(cond-> {:file/path file} (filter (fn [ref] (and (vector? ref)
new? (= :block/uuid (first ref)))))
;; TODO: use file system timestamp? (map (fn [ref] {:block/uuid (second ref)}))
(assoc :file/created-at (date-time-util/time-ms)))])] (seq))
;; To prevent "unique constraint" on datascript
block-ids (set/union (set block-ids) (set block-refs-ids))
pages (extract/with-ref-pages pages blocks)
pages-index (map #(select-keys % [:block/name]) pages)
tx (concat file-content pages-index delete-blocks pages block-ids blocks)]
{:tx {:tx
(d/transact! conn (gp-util/remove-nils tx) (select-keys options [:new-graph? :from-disk?])) (d/transact! conn (gp-util/remove-nils tx) (select-keys options [:new-graph? :from-disk?]))
:ast ast})) :ast ast}))

View File

@ -2,7 +2,9 @@
"Util fns shared between graph-parser and rest of app. Util fns only rely on "Util fns shared between graph-parser and rest of app. Util fns only rely on
clojure standard libraries." clojure standard libraries."
(:require [clojure.walk :as walk] (:require [clojure.walk :as walk]
[clojure.string :as string])) [clojure.string :as string]
[logseq.graph-parser.log :as log]
[clojure.reader :as reader]))
(defn safe-re-find (defn safe-re-find
"Copy of frontend.util/safe-re-find. Too basic to couple to main app" "Copy of frontend.util/safe-re-find. Too basic to couple to main app"
@ -153,3 +155,11 @@
[file] [file]
(when file (when file
(normalize-format (keyword (string/lower-case (last (string/split file #"\."))))))) (normalize-format (keyword (string/lower-case (last (string/split file #"\.")))))))
(defn safe-read-string
[content]
(try
(reader/read-string content)
(catch :default e
(log/error :parse/read-string-failed e)
{})))

View File

@ -153,7 +153,7 @@
files [[file-path new-content]]] files [[file-path new-content]]]
(push-to-write-chan files))) (push-to-write-chan files)))
(defn save-tree (defn save-tree!
[page-block tree] [page-block tree]
{:pre [(map? page-block)]} {:pre [(map? page-block)]}
(let [ok-handler #(save-tree-aux! page-block tree) (let [ok-handler #(save-tree-aux! page-block tree)

View File

@ -31,7 +31,7 @@
(nil? (:block/file page-block))) (nil? (:block/file page-block)))
(let [tree (tree/blocks->vec-tree repo blocks (:block/name page-block))] (let [tree (tree/blocks->vec-tree repo blocks (:block/name page-block))]
(if page-block (if page-block
(file/save-tree page-block tree) (file/save-tree! page-block tree)
(js/console.error (str "can't find page id: " page-db-id))))))) (js/console.error (str "can't find page id: " page-db-id)))))))
(defn write-files! (defn write-files!

View File

@ -0,0 +1 @@
(ns frontend.modules.outliner.whiteboard)

View File

@ -0,0 +1,12 @@
(ns frontend.modules.file.core-test
(:require [cljs.test :refer [use-fixtures] :as test]
[frontend.test.fixtures :as fixtures]
[frontend.test.helper :as helper]))
(def test-db helper/test-db)
(use-fixtures :each
fixtures/load-test-env
fixtures/react-components
fixtures/reset-db)