From 87166f94c368e8d6ce5cc741c8a7645c85877abf Mon Sep 17 00:00:00 2001 From: Andrea Amantini Date: Wed, 16 Nov 2022 11:30:37 +0100 Subject: [PATCH 1/5] Compute doc URL on the JVM --- resources/viewer-js-hash | 2 +- src/nextjournal/clerk.clj | 8 +++++++- src/nextjournal/clerk/builder.clj | 26 ++++++++++++++++++++------ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/resources/viewer-js-hash b/resources/viewer-js-hash index 3c4edb603..8a97977ef 100644 --- a/resources/viewer-js-hash +++ b/resources/viewer-js-hash @@ -1 +1 @@ -38F71uHxJsdbeuJsMnSfkow4eTQ2 \ No newline at end of file +Krwn1rGspBF32oETr17MR8aLCqd \ No newline at end of file diff --git a/src/nextjournal/clerk.clj b/src/nextjournal/clerk.clj index f84b99f98..d3eab6320 100644 --- a/src/nextjournal/clerk.clj +++ b/src/nextjournal/clerk.clj @@ -327,7 +327,13 @@ (partial with-viewer (:name v/notebook-viewer))) (defn doc-url [path] - (v/->viewer-eval (list 'v/doc-url path))) + (if-not builder/*opts* + (str "#/" path) + (let [{:keys [bundle? path->url current-path]} builder/*opts*] + (let [url (get path->url path)] + (if bundle? + (str "#/" url) + (str (v/relative-root-prefix-from current-path) url)))))) (defmacro example "Evaluates the expressions in `body` showing code next to results in Clerk. diff --git a/src/nextjournal/clerk/builder.clj b/src/nextjournal/clerk/builder.clj index 1c71d2d17..20107e70a 100644 --- a/src/nextjournal/clerk/builder.clj +++ b/src/nextjournal/clerk/builder.clj @@ -15,6 +15,8 @@ [nextjournal.clerk.webserver :as webserver] [nextjournal.clerk.config :as config])) +(def ^:dynamic *opts* nil) + (def clerk-docs (into ["CHANGELOG.md" "notebooks/markdown.md" @@ -115,12 +117,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." @@ -267,11 +278,14 @@ (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 [*opts* (assoc opts + :current-path file + :path->url (build-path->url opts state))] + (eval/eval-analyzed-doc doc))] (assoc doc :viewer (view/doc->viewer (assoc opts :inline-results? true) doc))) (catch Exception e {:error e})))] From b51fd2a2fcc62ad120211196db513513a8844acf Mon Sep 17 00:00:00 2001 From: Andrea Amantini Date: Wed, 16 Nov 2022 18:47:10 +0100 Subject: [PATCH 2/5] Get rid of doc-url in viewer --- resources/viewer-js-hash | 2 +- src/nextjournal/clerk/viewer.cljc | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/resources/viewer-js-hash b/resources/viewer-js-hash index 8a97977ef..20b6a0a60 100644 --- a/resources/viewer-js-hash +++ b/resources/viewer-js-hash @@ -1 +1 @@ -Krwn1rGspBF32oETr17MR8aLCqd \ No newline at end of file +2KfLaChGn54Gzj1Gj7Da4VbjFniH \ No newline at end of file diff --git a/src/nextjournal/clerk/viewer.cljc b/src/nextjournal/clerk/viewer.cljc index f2f408398..f64b7de51 100644 --- a/src/nextjournal/clerk/viewer.cljc +++ b/src/nextjournal/clerk/viewer.cljc @@ -1365,8 +1365,6 @@ (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 hide-result "Deprecated, please put ^{:nextjournal.clerk/visibility {:result :hide}} metadata on the form instead." From eb3a8fa24f8f5d7bf6e95a10fe5553ad6594fa01 Mon Sep 17 00:00:00 2001 From: Andrea Amantini Date: Wed, 16 Nov 2022 19:02:52 +0100 Subject: [PATCH 3/5] Simplify --- resources/viewer-js-hash | 2 +- src/nextjournal/clerk.clj | 9 +-------- src/nextjournal/clerk/builder.clj | 11 +++++++---- src/nextjournal/clerk/viewer.cljc | 2 ++ 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/resources/viewer-js-hash b/resources/viewer-js-hash index 20b6a0a60..2afffcd19 100644 --- a/resources/viewer-js-hash +++ b/resources/viewer-js-hash @@ -1 +1 @@ -2KfLaChGn54Gzj1Gj7Da4VbjFniH \ No newline at end of file +462ZVi7ZTvDpYGQ3GJkyBDP76cJ9 \ No newline at end of file diff --git a/src/nextjournal/clerk.clj b/src/nextjournal/clerk.clj index d3eab6320..6bb98e15b 100644 --- a/src/nextjournal/clerk.clj +++ b/src/nextjournal/clerk.clj @@ -326,14 +326,7 @@ "Experimental notebook viewer. You probably should not use this." (partial with-viewer (:name v/notebook-viewer))) -(defn doc-url [path] - (if-not builder/*opts* - (str "#/" path) - (let [{:keys [bundle? path->url current-path]} builder/*opts*] - (let [url (get path->url path)] - (if bundle? - (str "#/" url) - (str (v/relative-root-prefix-from current-path) url)))))) +(defn doc-url [path] (v/doc-url path)) (defmacro example "Evaluates the expressions in `body` showing code next to results in Clerk. diff --git a/src/nextjournal/clerk/builder.clj b/src/nextjournal/clerk/builder.clj index 20107e70a..b286c1f1e 100644 --- a/src/nextjournal/clerk/builder.clj +++ b/src/nextjournal/clerk/builder.clj @@ -250,7 +250,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)} @@ -282,9 +282,12 @@ (report-fn {:stage :building :doc doc :idx idx}) (let [{result :result duration :time-ms} (eval/time-ms (try - (let [doc (binding [*opts* (assoc opts - :current-path file - :path->url (build-path->url opts state))] + (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 diff --git a/src/nextjournal/clerk/viewer.cljc b/src/nextjournal/clerk/viewer.cljc index f64b7de51..d36afab7d 100644 --- a/src/nextjournal/clerk/viewer.cljc +++ b/src/nextjournal/clerk/viewer.cljc @@ -1366,6 +1366,8 @@ (def notebook (partial with-viewer (:name notebook-viewer))) (def code (partial with-viewer code-viewer)) +(defn ^:dynamic doc-url [path] (str "#/" path)) + (defn hide-result "Deprecated, please put ^{:nextjournal.clerk/visibility {:result :hide}} metadata on the form instead." {:deprecated "0.10"} From 731b78748c5bb56f48992135c5b3cca3e6db1203 Mon Sep 17 00:00:00 2001 From: Andrea Amantini Date: Thu, 17 Nov 2022 09:49:39 +0100 Subject: [PATCH 4/5] Test doc-url in static build --- notebooks/viewers/html.clj | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/notebooks/viewers/html.clj b/notebooks/viewers/html.clj index 18af22d9c..fcef041c4 100644 --- a/notebooks/viewers/html.clj +++ b/notebooks/viewers/html.clj @@ -1,8 +1,22 @@ ;; # HTML & Hiccup 🧙‍♀️ -(ns html (:require [nextjournal.clerk :as clerk])) +(ns viewers.html (:require [nextjournal.clerk :as clerk])) (clerk/html "

Ohai, HTML! 👋

") (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) From 40dcf945fec3fda0e29123b6475bf0cb08c1e766 Mon Sep 17 00:00:00 2001 From: Andrea Amantini Date: Thu, 17 Nov 2022 09:54:40 +0100 Subject: [PATCH 5/5] Remove traces of nuts --- src/nextjournal/clerk/builder.clj | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/nextjournal/clerk/builder.clj b/src/nextjournal/clerk/builder.clj index b286c1f1e..dfed0a1f0 100644 --- a/src/nextjournal/clerk/builder.clj +++ b/src/nextjournal/clerk/builder.clj @@ -15,8 +15,6 @@ [nextjournal.clerk.webserver :as webserver] [nextjournal.clerk.config :as config])) -(def ^:dynamic *opts* nil) - (def clerk-docs (into ["CHANGELOG.md" "notebooks/markdown.md"