Skip to content

Commit

Permalink
feat: Markdown export
Browse files Browse the repository at this point in the history
  • Loading branch information
motform committed Oct 6, 2024
1 parent e1bc5f1 commit 465f097
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
25 changes: 9 additions & 16 deletions src/org/motform/multiverse/components/library.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
[org.motform.multiverse.util :as util]
[re-frame.core :as rf]))


(defn export-markdown []
(util/download-file
(->> @(rf/subscribe [:db/stories]) (map story/->md) (str/join "\n\n"))
(str "multiverse-library-export-" (nano-id) ".md")
"text/markdown"))

(defn open-story [id]
(rf/dispatch [:story/active id])
(rf/dispatch [:page/active :page/story])
Expand All @@ -18,7 +25,7 @@
{:on-pointer-down #(open-story id)}
[:td (or (util/title-case title) "Generating title...")]
[:td id]
[:td model]
[:td (name model)]
[:td (or prompt-version "N/A")]
[:td (count sentences)]
[:td (util/format-date updated)]]))
Expand All @@ -38,20 +45,6 @@
^{:key (get-in story [:story/meta :story/id])}
[LibraryItemRow story])]])

(defn export-library
"SOURCE: https://gist.github.com/zoren/cc74758198b503b1755b75d1a6b376e7"
[]
(let [stories @(rf/subscribe [:db/stories])
md (->> stories (map story/->md) (str/join "\n\n"))
library (js/Blob. #js [(prn-str md)] #js {:type "application/edn"})
file-name (str "multiverse-library-export-" (nano-id) ".md")
edn-url (js/URL.createObjectURL library)
anchor (doto (js/document.createElement "a")
(-> .-href (set! edn-url))
(-> .-download (set! file-name)))]
(.click anchor)
(js/URL.revokeObjectURL edn-url)))

(defn LibraryToggles []
[:section.h-stack.spaced.centered
[:p>a.source-code {:href "https://github.com/motform/multiverse" :target "_bank"}
Expand All @@ -62,7 +55,7 @@
(rf/dispatch [:library/clear]))}
"empty library"]
[:button.button-secondary.rounded.shadow-medium
{:on-pointer-down #(export-library)}
{:on-pointer-down #(export-markdown)}
"export library"]]])

(defn Empty []
Expand Down
6 changes: 3 additions & 3 deletions src/org/motform/multiverse/story.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@
(let [meta (:story/meta story)]
(str
"# " (:story/title meta) "\n"
"---"
"---\n"
"id:" (:story/id meta) "\n"
"model:" (:story/model meta) "\n"
"model:" (name (:story/model meta)) "\n"
"prompt:" (:story/prompt meta) "\n"
"date:" (util/format-date (:story/updated meta)) "\n"
"---"
"---\n"
(longest-sentence story))))
16 changes: 14 additions & 2 deletions src/org/motform/multiverse/util.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,20 @@

(defn spinner-small []
[:div.v-stack.centered
[:svg {:height 21
:width 54}
[:svg {:height 21 :width 54}
[:circle.spinner-small-1 {:cx 6 :cy 6 :r 5 :fill "var(--spinner-fill)"}]
[:circle.spinner-small-2 {:cx 26 :cy 6 :r 5 :fill "var(--spinner-fill)"}]
[:circle.spinner-small-3 {:cx 46 :cy 6 :r 5 :fill "var(--spinner-fill)"}]]])

;; DOM

(defn download-file
"SOURCE: https://gist.github.com/zoren/cc74758198b503b1755b75d1a6b376e7"
[^String data ^String file-name ^String type]
(let [blob (js/Blob. #js [data] #js {:type type})
data-url (js/URL.createObjectURL blob)
anchor (doto (js/document.createElement "a")
(-> .-href (set! data-url))
(-> .-download (set! file-name)))]
(.click anchor)
(js/URL.revokeObjectURL data-url)))

0 comments on commit 465f097

Please sign in to comment.