Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wire up frontend to use nbb export and html
- Add tests for export - Refactor export to be more readable - Add error notification for export that user can see - Remove unused :ui/show-recent? state - Cleanup and move escape fns to proper locations
- Loading branch information
1 parent
66fdb73
commit 9ac17d4
Showing
19 changed files
with
330 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{:linters | ||
{:aliased-namespace-symbol {:level :warning} | ||
:namespace-name-mismatch {:level :warning} | ||
:used-underscored-binding {:level :warning} | ||
|
||
:consistent-alias | ||
{:aliases {clojure.string string}}} | ||
|
||
:lint-as | ||
{logseq.publish-spa.test.helper/deftest-async clojure.test/deftest | ||
promesa.core/let clojure.core/let} | ||
:skip-comments true | ||
:output {:progress true}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/.clj-kondo/.cache | ||
.nbb | ||
/tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{:paths ["src"] | ||
:deps | ||
{logseq/db {:local/root "../db"}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
{:deps | ||
{:paths ["src"] | ||
:deps | ||
{logseq/graph-parser | ||
;; Nbb bug. Should just be "../graph-parser" | ||
{:local/root "../../../../graph-parser"}}} | ||
{:local/root "../../../../graph-parser"} | ||
io.github.nextjournal/nbb-test-runner | ||
{:git/sha "60ed57aa04bca8d604f5ba6b28848bd887109347" | ||
#_#_:local/root "../../../../../../nbb-test-runner"}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,93 @@ | ||
(ns logseq.publish-spa.export | ||
(:require ["fs-extra$default" :as fs] | ||
["path" :as path] | ||
(:require ["fs-extra" :as fse] | ||
["path" :as node-path] | ||
["fs" :as fs] | ||
[promesa.core :as p])) | ||
|
||
(defn handle-export-publish-assets | ||
[html app-path custom-css-path export-css-path repo-path asset-filenames output-path] | ||
(let [root-dir output-path | ||
static-dir (path/join root-dir "static") | ||
assets-from-dir (path/join repo-path "assets") | ||
assets-to-dir (path/join root-dir "assets") | ||
index-html-path (path/join root-dir "index.html")] | ||
(p/let [_ (. fs ensureDir static-dir) | ||
_ (. fs ensureDir assets-to-dir) | ||
_ (p/all (concat | ||
[(. fs writeFile index-html-path html) | ||
(def js-files | ||
"js files from publishing release build" | ||
["main.js" "code-editor.js" "excalidraw.js" "tldraw.js"]) | ||
|
||
(def static-dirs | ||
"dirs under static dir to copy over" | ||
["css" "fonts" "icons" "img" "js"]) | ||
|
||
(. fs copy (path/join app-path "404.html") (path/join root-dir "404.html"))] | ||
(defn- default-notification | ||
[msg] | ||
(if (= (:type msg) "success") | ||
(js/console.log (:payload msg)) | ||
(js/console.error (:payload msg)))) | ||
|
||
(defn- cleanup-js-dir | ||
"Moves used js files to the correct dir and removes unused js files" | ||
[output-static-dir] | ||
(let [publishing-dir (node-path/join output-static-dir "js" "publishing")] | ||
(p/let [_ (p/all (map (fn [file] | ||
(fs/rmSync (node-path/join output-static-dir "js" file) #js {:force true})) | ||
js-files)) | ||
_ (p/all (map (fn [file] | ||
(fs/renameSync | ||
(node-path/join publishing-dir file) | ||
(node-path/join output-static-dir "js" file))) | ||
js-files)) | ||
;; remove publishing-dir | ||
_ (p/all (map (fn [file] | ||
(fs/rmSync (node-path/join publishing-dir file))) | ||
(fs/readdirSync publishing-dir))) | ||
_ (fs/rmdirSync publishing-dir) | ||
;; remove source map files | ||
_ (p/all (map (fn [file] | ||
(fs/rmSync (node-path/join output-static-dir "js" (str file ".map")) #js {:force true})) | ||
["main.js" "code-editor.js" "excalidraw.js"]))]))) | ||
|
||
(defn- copy-static-files-and-assets | ||
[static-dir repo-path output-dir {:keys [log-error-fn asset-filenames] | ||
:or {asset-filenames [] | ||
log-error-fn js/console.error}}] | ||
(let [assets-from-dir (node-path/join repo-path "assets") | ||
assets-to-dir (node-path/join output-dir "assets") | ||
output-static-dir (node-path/join output-dir "static")] | ||
(p/let [_ (fs/mkdirSync assets-to-dir #js {:recursive true}) | ||
_ (p/all (concat | ||
[(fse/copy (node-path/join static-dir "404.html") (node-path/join output-dir "404.html"))] | ||
|
||
(map | ||
(fn [filename] | ||
(-> (. fs copy (path/join assets-from-dir filename) (path/join assets-to-dir filename)) | ||
(-> (fse/copy (node-path/join assets-from-dir filename) (node-path/join assets-to-dir filename)) | ||
(p/catch | ||
(fn [e] | ||
;; TODO: Make into a callback | ||
(println "Failed to copy" | ||
(str {:from (path/join assets-from-dir filename) | ||
:to (path/join assets-to-dir filename)}) | ||
e))))) | ||
(log-error-fn "Failed to copy" | ||
(str {:from (node-path/join assets-from-dir filename) | ||
:to (node-path/join assets-to-dir filename)}) | ||
e))))) | ||
asset-filenames) | ||
|
||
(map | ||
(fn [part] | ||
(. fs copy (path/join app-path part) (path/join static-dir part))) | ||
["css" "fonts" "icons" "img" "js"]))) | ||
export-css (if (fs/existsSync export-css-path) (. fs readFile export-css-path) "") | ||
_ (. fs writeFile (path/join static-dir "css" "export.css") export-css) | ||
custom-css (if (fs/existsSync custom-css-path) (. fs readFile custom-css-path) "") | ||
_ (. fs writeFile (path/join static-dir "css" "custom.css") custom-css) | ||
js-files ["main.js" "code-editor.js" "excalidraw.js" "tldraw.js"] | ||
_ (p/all (map (fn [file] | ||
(. fs removeSync (path/join static-dir "js" file))) | ||
js-files)) | ||
_ (p/all (map (fn [file] | ||
(. fs moveSync | ||
(path/join static-dir "js" "publishing" file) | ||
(path/join static-dir "js" file))) | ||
js-files)) | ||
_ (. fs removeSync (path/join static-dir "js" "publishing")) | ||
;; remove source map files | ||
;; TODO: ugly, replace with ls-files and filter with ".map" | ||
_ (p/all (map (fn [file] | ||
(. fs removeSync (path/join static-dir "js" (str file ".map")))) | ||
["main.js" "code-editor.js" "excalidraw.js"]))] | ||
|
||
;; TODO: Make into a callback | ||
(println | ||
:notification | ||
{:type "success" | ||
:payload (str "Export public pages and publish assets to " root-dir " successfully 🎉")})))) | ||
(fse/copy (node-path/join static-dir part) (node-path/join output-static-dir part))) | ||
static-dirs)))]))) | ||
|
||
(defn export | ||
"Given a graph's directory, the generated html and the directory containing | ||
html/static assets, creates an index.html with supporting assets at the | ||
specified output directory" | ||
[html static-dir repo-path output-dir {:keys [notification-fn] | ||
:or {notification-fn default-notification} | ||
:as options}] | ||
(let [custom-css-path (node-path/join repo-path "logseq" "custom.css") | ||
export-css-path (node-path/join repo-path "logseq" "export.css") | ||
output-static-dir (node-path/join output-dir "static") | ||
index-html-path (node-path/join output-dir "index.html")] | ||
(-> (p/let [_ (fs/mkdirSync output-static-dir #js {:recursive true}) | ||
_ (fs/writeFileSync index-html-path html) | ||
_ (copy-static-files-and-assets static-dir repo-path output-dir options) | ||
export-css (if (fs/existsSync export-css-path) (str (fs/readFileSync export-css-path)) "") | ||
_ (fs/writeFileSync (node-path/join output-static-dir "css" "export.css") export-css) | ||
custom-css (if (fs/existsSync custom-css-path) (str (fs/readFileSync custom-css-path)) "") | ||
_ (fs/writeFileSync (node-path/join output-static-dir "css" "custom.css") custom-css) | ||
_ (cleanup-js-dir output-static-dir)] | ||
(notification-fn {:type "success" | ||
:payload (str "Export public pages and publish assets to " output-dir " successfully 🎉")})) | ||
(p/catch (fn [error] | ||
(notification-fn {:type "error" | ||
:payload (str "Export public pages unexpectedly failed with: " error)})))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.