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) diff --git a/resources/viewer-js-hash b/resources/viewer-js-hash index 3c4edb603..2afffcd19 100644 --- a/resources/viewer-js-hash +++ b/resources/viewer-js-hash @@ -1 +1 @@ -38F71uHxJsdbeuJsMnSfkow4eTQ2 \ 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 f84b99f98..6bb98e15b 100644 --- a/src/nextjournal/clerk.clj +++ b/src/nextjournal/clerk.clj @@ -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. diff --git a/src/nextjournal/clerk/builder.clj b/src/nextjournal/clerk/builder.clj index 1c71d2d17..dfed0a1f0 100644 --- a/src/nextjournal/clerk/builder.clj +++ b/src/nextjournal/clerk/builder.clj @@ -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." @@ -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)} @@ -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})))] diff --git a/src/nextjournal/clerk/viewer.cljc b/src/nextjournal/clerk/viewer.cljc index f2f408398..d36afab7d 100644 --- a/src/nextjournal/clerk/viewer.cljc +++ b/src/nextjournal/clerk/viewer.cljc @@ -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."