pull/645/head
Tienson Qin 2020-08-19 18:41:22 +08:00
parent 7a8ca01ee3
commit c2df042a96
2 changed files with 54 additions and 2 deletions

View File

@ -1,6 +1,11 @@
(ns frontend.external.protocol)
(defprotocol External
(toMldocAst [this content] ; might be json or anything
(toMarkdownFiles [this content config]
"Should return a map of markdown's file name to contents."
)
(fromMldocAst [this ast]))
;; Long-term goal:
;; (toMldocAst [this content])
;; (fromMldocAst [this ast])
)

View File

@ -0,0 +1,47 @@
(ns frontend.external.roam
(:require [frontend.external.protocol :as protocol]
[cljs-bean.core :as bean]
[medley.core :as medley]
[clojure.walk :as walk]))
(defonce uid->uuid (atom {}))
(declare children->text)
(defn child->text
[{:keys [uid string children] :as child}]
(when-not (get @uid->uuid uid)
(swap! uid->uuid assoc uid (medley/random-uuid)))
(let [children-text (->> (map children->text children)
(interpose "\n")
(apply str))]
(if string
(str string "\n" children-text)
children-text)))
(defn children->text
[children]
(map child->text children))
(defn ->file
[page-data]
(let [{:keys [create-time title children edit-time]} page-data]
{:title title
:created-at create-time
:last-modified-at edit-time
:text (children->text children)}))
(defn ->files
[edn-data]
(let [pages-with-data (filter :children edn-data)]
(map ->file pages-with-data)))
(defrecord Roam []
protocol/External
(toMarkdownFiles [this content _config]
(let [data (bean/->clj (js/JSON.parse content))]
(->files data))))
;; (:create-email :create-time :title :children :edit-time :edit-email)
(defonce test-roam-json (frontend.db/get-file "same.json"))
(defonce edn-data (bean/->clj (js/JSON.parse test-roam-json)))