Skip to content

Commit f08a9b0

Browse files
authored
Compute doc URL on the JVM (#284)
Fixes behaviour of `clerk/doc-url` in static app. A regression introduced when by dropping EDN encoding of results in #273.
1 parent d8c601f commit f08a9b0

File tree

5 files changed

+41
-13
lines changed

5 files changed

+41
-13
lines changed

notebooks/viewers/html.clj

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
;; # HTML & Hiccup 🧙‍♀️
2-
(ns html (:require [nextjournal.clerk :as clerk]))
2+
(ns viewers.html (:require [nextjournal.clerk :as clerk]))
33

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

66
(clerk/html [:h3 "We "
77
[:i "strongly"]
88
" prefer hiccup, don't we? ✨"])
9+
10+
;; Linking to notebooks
11+
12+
(clerk/html
13+
[:div
14+
"Go to "
15+
[:a.text-lg {:href (clerk/doc-url "notebooks/viewers/image.clj")} "images"]
16+
" notebook."])
17+
18+
(clerk/with-viewer
19+
'(fn [_ _] [:div
20+
"Go to "
21+
[:a.text-lg {:href (v/doc-url "notebooks/viewers/image.clj")} "images"]
22+
" notebook."]) nil)

resources/viewer-js-hash

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
38F71uHxJsdbeuJsMnSfkow4eTQ2
1+
462ZVi7ZTvDpYGQ3GJkyBDP76cJ9

src/nextjournal/clerk.clj

+1-2
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,7 @@
326326
"Experimental notebook viewer. You probably should not use this."
327327
(partial with-viewer (:name v/notebook-viewer)))
328328

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

332331
(defmacro example
333332
"Evaluates the expressions in `body` showing code next to results in Clerk.

src/nextjournal/clerk/builder.clj

+22-7
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,21 @@
115115
index (assoc :index (str index)))))
116116

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

119126
(defn build-static-app-opts [{:as opts :keys [bundle? out-path browse? index]} docs]
120-
(let [paths (mapv :file docs)
121-
path->doc (into {} (map (juxt :file :viewer)) docs)
122-
path->url (into {} (map (juxt identity #(cond-> (->> % (view/map-index opts) strip-index) (not bundle?) ->html-extension))) paths)]
123-
(assoc opts :bundle? bundle? :path->doc path->doc :paths (vec (keys path->doc)) :path->url path->url)))
127+
(let [path->doc (into {} (map (juxt :file :viewer)) docs)]
128+
(assoc opts
129+
:bundle? bundle?
130+
:path->doc path->doc
131+
:paths (vec (keys path->doc))
132+
:path->url (build-path->url opts docs))))
124133

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

242-
(defn build-static-app! [opts]
251+
(defn build-static-app! [{:as opts :keys [bundle?]}]
243252
(let [{:as opts :keys [download-cache-fn upload-cache-fn report-fn compile-css?]}
244253
(process-build-opts opts)
245254
{:keys [expanded-paths error]} (try {:expanded-paths (expand-paths opts)}
@@ -267,11 +276,17 @@
267276
(report-fn {:stage :downloading-cache})
268277
(let [{duration :time-ms} (eval/time-ms (download-cache-fn state))]
269278
(report-fn {:stage :done :duration duration})))
270-
state (mapv (fn [doc idx]
279+
state (mapv (fn [{:as doc :keys [file]} idx]
271280
(report-fn {:stage :building :doc doc :idx idx})
272281
(let [{result :result duration :time-ms} (eval/time-ms
273282
(try
274-
(let [doc (eval/eval-analyzed-doc doc)]
283+
(let [doc (binding [viewer/doc-url
284+
(fn [path]
285+
(let [url (get (build-path->url opts state) path)]
286+
(if bundle?
287+
(str "#/" url)
288+
(str (viewer/relative-root-prefix-from file) url))))]
289+
(eval/eval-analyzed-doc doc))]
275290
(assoc doc :viewer (view/doc->viewer (assoc opts :inline-results? true) doc)))
276291
(catch Exception e
277292
{:error e})))]

src/nextjournal/clerk/viewer.cljc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1365,8 +1365,8 @@
13651365
(def tex (partial with-viewer katex-viewer))
13661366
(def notebook (partial with-viewer (:name notebook-viewer)))
13671367
(def code (partial with-viewer code-viewer))
1368-
(defn doc-url [path]
1369-
(->viewer-eval (list 'nextjournal.clerk.render/doc-url path)))
1368+
1369+
(defn ^:dynamic doc-url [path] (str "#/" path))
13701370

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

0 commit comments

Comments
 (0)