Skip to content

Commit

Permalink
Change export to not depend on printing - close logseq#4211
Browse files Browse the repository at this point in the history
  • Loading branch information
logseq-cldwalker authored and andelf committed Feb 23, 2022
1 parent 3517090 commit b0babf2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .clj-kondo/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
:consistent-alias
{:aliases {datascript.core d
datascript.transit dt
datascript.db ddb}}}
datascript.db ddb
lambdaisland.glogi log}}}

:hooks {:analyze-call {rum.core/defc hooks.rum/defc
rum.core/defcs hooks.rum/defcs}}
Expand Down
2 changes: 1 addition & 1 deletion src/main/frontend/db/model.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@
(= k (:db/id (:block/left block))))
blocks)
(map #(select-keys % [:db/id :block/level :block/content :block/file])))}))))]
(util/pprint duplicates)))
#_(util/pprint duplicates)))
(assert (= (count blocks) (count (set (map :block/left blocks)))) "Each block should have a different left node"))

(let [left->blocks (reduce (fn [acc b] (assoc acc (:db/id (:block/left b)) b)) {} blocks)]
Expand Down
25 changes: 15 additions & 10 deletions src/main/frontend/handler/export.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
[frontend.util :as util]
[frontend.format.mldoc :as mldoc]
[goog.dom :as gdom]
[promesa.core :as p]))
[promesa.core :as p])
(:import [goog.string StringBuffer]))

(defn- get-page-content
[repo page]
Expand Down Expand Up @@ -464,17 +465,21 @@
(str "_" (quot (util/time-ms) 1000))
(str "." (string/lower-case (name extension)))))

(defn- export-repo-as-edn-str [repo]
(when-let [conn (db/get-conn repo)]
(let [sb (StringBuffer.)]
(pprint/pprint (blocks conn) (StringBufferWriter. sb))
(str sb))))

(defn export-repo-as-edn-v2!
[repo]
(when-let [conn (db/get-conn repo)]
(let [edn-str (with-out-str
(pprint/pprint
(blocks conn)))
data-str (str "data:text/edn;charset=utf-8," (js/encodeURIComponent edn-str))]
(when-let [anchor (gdom/getElement "download-as-edn-v2")]
(.setAttribute anchor "href" data-str)
(.setAttribute anchor "download" (file-name repo :edn))
(.click anchor)))))
(when-let [data-str (some->> (export-repo-as-edn-str repo)
js/encodeURIComponent
(str "data:text/edn;charset=utf-8,"))]
(when-let [anchor (gdom/getElement "download-as-edn-v2")]
(.setAttribute anchor "href" data-str)
(.setAttribute anchor "download" (file-name repo :edn))
(.click anchor))))

(defn- nested-update-id
[vec-tree]
Expand Down
15 changes: 11 additions & 4 deletions src/test/frontend/handler/export_test.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(ns frontend.handler.export-test
;; namespace local config for private function tests
{:clj-kondo/config {:linters {:private-call {:level :off}}}}
(:require [cljs.test :refer [async deftest use-fixtures are]]
(:require [cljs.test :refer [async deftest use-fixtures are is]]
[clojure.edn :as edn]
[frontend.handler.export :as export]
[frontend.db.config :as config]
[frontend.handler.repo :as repo-handler]
Expand Down Expand Up @@ -52,9 +51,17 @@
(deftest export-files-as-markdown
(are [expect files]
(= expect
(export/export-files-as-markdown (state/get-current-repo) files true))
(@#'export/export-files-as-markdown (state/get-current-repo) files true))
[["pages/page1.md" "- 1\n\t- 2\n\t\t- 3\n\t\t- 3\n- 4"]]
[{:path "pages/page1.md" :content (:file/content (nth test-files 0)) :names ["page1"] :format :markdown}]

[["pages/page2.md" "- 3\n\t- 1\n\t\t- 2\n\t\t\t- 3\n\t\t\t- 3\n\t- 4"]]
[{:path "pages/page2.md" :content (:file/content (nth test-files 1)) :names ["page2"] :format :markdown}]))

(deftest export-repo-as-edn-str
(let [edn-output (edn/read-string
(@#'export/export-repo-as-edn-str (state/get-current-repo)))]
(is (= #{:version :blocks} (set (keys edn-output)))
"Correct top-level keys")
(is (= ["page1" "page2"] (map :block/page-name (:blocks edn-output)))
"Correct pages")))

0 comments on commit b0babf2

Please sign in to comment.