Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compute doc URL on the JVM #284

Merged
merged 5 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion notebooks/viewers/html.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
;; # HTML & Hiccup 🧙‍♀️
(ns html (:require [nextjournal.clerk :as clerk]))
(ns viewers.html (:require [nextjournal.clerk :as clerk]))

(clerk/html "<h3>Ohai, HTML! 👋</h3>")

(clerk/html [:h3 "We "
[:i "strongly"]
" prefer hiccup, don't we? ✨"])

;; Linking to notebooks

(clerk/html
[:div
"Go to "
[:a.text-lg {:href (clerk/doc-url "notebooks/viewers/image.clj")} "images"]
" notebook."])

(clerk/with-viewer
'(fn [_ _] [:div
"Go to "
[:a.text-lg {:href (v/doc-url "notebooks/viewers/image.clj")} "images"]
" notebook."]) nil)
2 changes: 1 addition & 1 deletion resources/viewer-js-hash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
38F71uHxJsdbeuJsMnSfkow4eTQ2
462ZVi7ZTvDpYGQ3GJkyBDP76cJ9
3 changes: 1 addition & 2 deletions src/nextjournal/clerk.clj
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,7 @@
"Experimental notebook viewer. You probably should not use this."
(partial with-viewer (:name v/notebook-viewer)))

(defn doc-url [path]
(v/->viewer-eval (list 'v/doc-url path)))
(defn doc-url [path] (v/doc-url path))

(defmacro example
"Evaluates the expressions in `body` showing code next to results in Clerk.
Expand Down
29 changes: 22 additions & 7 deletions src/nextjournal/clerk/builder.clj
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,21 @@
index (assoc :index (str index)))))

#_(process-build-opts {:index 'book.clj})
(defn build-path->url [{:as opts :keys [bundle?]} docs]
(into {}
(map (comp (juxt identity #(cond-> (->> % (view/map-index opts) strip-index) (not bundle?) ->html-extension))
:file))
docs))
#_(build-path->url {:bundle? false} [{:file "notebooks/foo.clj"} {:file "index.clj"}])
#_(build-path->url {:bundle? true} [{:file "notebooks/foo.clj"} {:file "index.clj"}])

(defn build-static-app-opts [{:as opts :keys [bundle? out-path browse? index]} docs]
(let [paths (mapv :file docs)
path->doc (into {} (map (juxt :file :viewer)) docs)
path->url (into {} (map (juxt identity #(cond-> (->> % (view/map-index opts) strip-index) (not bundle?) ->html-extension))) paths)]
(assoc opts :bundle? bundle? :path->doc path->doc :paths (vec (keys path->doc)) :path->url path->url)))
(let [path->doc (into {} (map (juxt :file :viewer)) docs)]
(assoc opts
:bundle? bundle?
:path->doc path->doc
:paths (vec (keys path->doc))
:path->url (build-path->url opts docs))))

(defn ssr!
"Shells out to node to generate server-side-rendered html."
Expand Down Expand Up @@ -239,7 +248,7 @@
"/css/viewer.css" (viewer/store+get-cas-url! (assoc opts :ext "css") (fs/read-all-bytes tw-output)))
(fs/delete-tree tw-folder)))

(defn build-static-app! [opts]
(defn build-static-app! [{:as opts :keys [bundle?]}]
(let [{:as opts :keys [download-cache-fn upload-cache-fn report-fn compile-css?]}
(process-build-opts opts)
{:keys [expanded-paths error]} (try {:expanded-paths (expand-paths opts)}
Expand Down Expand Up @@ -267,11 +276,17 @@
(report-fn {:stage :downloading-cache})
(let [{duration :time-ms} (eval/time-ms (download-cache-fn state))]
(report-fn {:stage :done :duration duration})))
state (mapv (fn [doc idx]
state (mapv (fn [{:as doc :keys [file]} idx]
(report-fn {:stage :building :doc doc :idx idx})
(let [{result :result duration :time-ms} (eval/time-ms
(try
(let [doc (eval/eval-analyzed-doc doc)]
(let [doc (binding [viewer/doc-url
(fn [path]
(let [url (get (build-path->url opts state) path)]
(if bundle?
(str "#/" url)
(str (viewer/relative-root-prefix-from file) url))))]
(eval/eval-analyzed-doc doc))]
(assoc doc :viewer (view/doc->viewer (assoc opts :inline-results? true) doc)))
(catch Exception e
{:error e})))]
Expand Down
4 changes: 2 additions & 2 deletions src/nextjournal/clerk/viewer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1365,8 +1365,8 @@
(def tex (partial with-viewer katex-viewer))
(def notebook (partial with-viewer (:name notebook-viewer)))
(def code (partial with-viewer code-viewer))
(defn doc-url [path]
(->viewer-eval (list 'nextjournal.clerk.render/doc-url path)))

(defn ^:dynamic doc-url [path] (str "#/" path))

(defn hide-result
"Deprecated, please put ^{:nextjournal.clerk/visibility {:result :hide}} metadata on the form instead."
Expand Down