Skip to content

Commit

Permalink
fix(import): roam research block refs
Browse files Browse the repository at this point in the history
  • Loading branch information
tiensonqin committed Nov 4, 2020
1 parent a72ca41 commit 2414906
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/main/frontend/components/block.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,7 @@
show-page?
page-name)
(let [parents-atom (atom parents)
component [:div.block-parents.flex-row.flex
component [:div.block-parents.flex-row.flex-1
(when show-page?
[:a {:href (rfe/href :page {:name page-name})}
(or (:page/original-name page)
Expand Down
4 changes: 2 additions & 2 deletions src/main/frontend/components/external.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
(set! (.-onload reader)
(fn [e]
(let [text (.. e -target -result)]
(external-handler/import-from-roam-json! text)
(reset! *importing? false))))
(external-handler/import-from-roam-json! text
#(reset! *importing? false)))))
(.readAsText reader file)))
(notification/show! "Please choose a JSON file."
:error))))}]
Expand Down
7 changes: 4 additions & 3 deletions src/main/frontend/external/roam.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
;; TODO: 5. Roam attributes -> properties
;; TODO: 6. hiccup

(defonce uid-pattern #"\(\(([a-zA-Z0-9_\\-]{10})\)\)")
(defonce uid-pattern #"\(\(([a-zA-Z0-9_\\-]{6,24})\)\)")
(defonce macro-pattern #"\{\{([^{}]+)\}\}")

(defn uid-transform
Expand Down Expand Up @@ -55,7 +55,9 @@
(map last)
(distinct)
(set))]
(reset! all-refed-uids uids))))
(reset! all-refed-uids uids)
(doseq [uid uids]
(swap! uid->uuid assoc uid (medley/random-uuid))))))

(defn transform
[text]
Expand All @@ -65,7 +67,6 @@
(uid-transform)
(macro-transform)))

;; #"(([a-zA-Z0-9_\\-]{9}))"
(declare children->text)
(defn child->text
[{:keys [uid string children] :as child} level]
Expand Down
71 changes: 40 additions & 31 deletions src/main/frontend/handler/external.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,53 @@
(:require [frontend.external :as external]
[frontend.handler.file :as file-handler]
[frontend.handler.notification :as notification]
[frontend.handler.common :as common-handler]
[frontend.state :as state]
[frontend.date :as date]
[frontend.config :as config]
[clojure.string :as string]
[frontend.db :as db]))

(defn index-files!
[repo files error-files]
(doseq [file files]
(let [title (:title file)
journal? (date/valid-journal-title? title)]
(try
(when-let [text (:text file)]
(let [path (str (if journal?
config/default-journals-directory
config/default-pages-directory)
"/"
(if journal?
(date/journal-title->default title)
(string/replace title "/" "-"))
".md")]
(file-handler/alter-file repo path text {})
(when journal?
(let [page-name (string/lower-case title)]
(db/transact! repo
[{:page/name page-name
:page/journal? true
:page/journal-day (date/journal-title->int title)}])))))
(catch js/Error e
(swap! error-files conj file))))))
[repo files git-add-cb]
(let [titles (->> files
(map :title)
(map :text)
(remove nil?))
files (map (fn [file]
(let [title (:title file)
journal? (date/valid-journal-title? title)]
(when-let [text (:text file)]
(let [path (str (if journal?
config/default-journals-directory
config/default-pages-directory)
"/"
(if journal?
(date/journal-title->default title)
(string/replace title "/" "-"))
".md")]
[path text]))))
files)]
(file-handler/alter-files repo files {:add-history? false
:update-status? false
:reset? true
:git-add-cb git-add-cb})
(let [journal-pages-tx (let [titles (filter date/valid-journal-title? titles)]
(map
(fn [title]
(let [page-name (string/lower-case title)]
{:page/name page-name
:page/journal? true
:page/journal-day (date/journal-title->int title)}))
titles))]
(when (seq journal-pages-tx)
(db/transact! repo journal-pages-tx)))))

;; TODO: compute the dependencies
;; TODO: Should it merge the roam daily notes with the month journals
(defn import-from-roam-json!
[data]
[data finished-ok-handler]
(when-let [repo (state/get-current-repo)]
(let [files (external/to-markdown-files :roam data {})
error-files (atom #{})]
(index-files! repo files error-files)
(when (seq @error-files)
(index-files! repo @error-files (atom nil))))))
(let [files (external/to-markdown-files :roam data {})]
(index-files! repo files
(fn []
(common-handler/check-changed-files-status)
(finished-ok-handler))))))
65 changes: 43 additions & 22 deletions src/main/frontend/handler/file.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,16 @@
[repo path content {:keys [reset? re-render-root? add-history? update-status?]
:or {reset? true
re-render-root? false
add-history? true}}]
add-history? true
update-status? false}}]
(let [original-content (db/get-file-no-sub repo path)]
(if reset?
(db/reset-file! repo path content)
(db/set-file-content! repo path content))
(util/p-handle
(fs/write-file (util/get-repo-dir repo) path content)
(fn [_]
(git-handler/git-add repo path)
(git-handler/git-add repo path update-status?)
(when (= path (str config/app-name "/" config/config-file))
(restore-config! repo true))
(when (= path (str config/app-name "/" config/custom-css-file))
Expand All @@ -132,26 +133,46 @@
(js/console.error error)))))

(defn alter-files
[repo files]
(let [files-tx (mapv (fn [[path content]]
(let [original-content (db/get-file-no-sub repo path)]
[path original-content content])) files)]
(-> (p/all
(doall
(map
(fn [[path content]]
(db/set-file-content! repo path content)
(util/p-handle
(fs/write-file (util/get-repo-dir repo) path content)
(fn [_]
(git-handler/git-add repo path))
(fn [error]
(println "Write file failed, path: " path ", content: " content)
(js/console.error error))))
files)))
(p/then (fn [_result]
(ui-handler/re-render-file!)
(history/add-history! repo files-tx))))))
([repo files]
(alter-files repo files {}))
([repo files {:keys [add-history? update-status? git-add-cb reset?]
:or {add-history? true
update-status? true
reset? false}}]
(let [files-tx (mapv (fn [[path content]]
(let [original-content (db/get-file-no-sub repo path)]
[path original-content content])) files)]
(-> (p/all
(doall
(map
(fn [[path content]]
(if reset?
(db/reset-file! repo path content)
(db/set-file-content! repo path content))
(util/p-handle
(fs/write-file (util/get-repo-dir repo) path content)
(fn [_])
(fn [error]
(println "Write file failed, path: " path ", content: " content)
(js/console.error error))))
files)))
(p/then (fn [_result]
(->
(p/all
(doall
(map
(fn [[path content]]
(git-handler/git-add repo path update-status?))
files)))
(p/then (fn [_]
(when git-add-cb
(git-add-cb))))
(p/catch (fn [error]
(println "Git add failed")
(js/console.error error))))
(ui-handler/re-render-file!)
(when add-history?
(history/add-history! repo files-tx))))))))

(defn remove-file!
[repo file]
Expand Down
3 changes: 1 addition & 2 deletions src/main/frontend/handler/git.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@
([repo-url file]
(git-add repo-url file true))
([repo-url file update-status?]
(-> (p/let [_result (git/add repo-url file)]
(-> (p/let [result (git/add repo-url file)]
(when update-status?
(common-handler/check-changed-files-status)))
(p/catch (fn [error]
(println "git add '" file "' failed: " error)
(js/console.error error))))))

(defn get-latest-commit
([repo-url handler]
(get-latest-commit repo-url handler 1))
Expand Down

0 comments on commit 2414906

Please sign in to comment.