diff --git a/deps.edn b/deps.edn index c0423aac4..087ab5007 100644 --- a/deps.edn +++ b/deps.edn @@ -3,7 +3,7 @@ org.clojure/java.classpath {:mvn/version "1.0.0"} org.clojure/tools.analyzer.jvm {:mvn/version "1.1.0"} org.clojure/tools.deps.alpha {:mvn/version "0.11.905"} - babashka/fs {:mvn/version "0.0.5"} + babashka/fs {:mvn/version "0.1.2"} borkdude/edamame {:mvn/version "0.0.11"} weavejester/dependency {:mvn/version "0.2.1"} @@ -49,5 +49,5 @@ io.github.nextjournal/cas {:git/url "git@github.com:nextjournal/cas" :git/sha "5e8079b720e347b9466db9c2282ce79a125a011c"} rewrite-clj/rewrite-clj {:mvn/version "1.0.699-alpha"} - babashka/fs {:mvn/version "0.0.5"}} + babashka/fs {:mvn/version "0.1.2"}} :ns-default build}}} diff --git a/src/nextjournal/clerk.clj b/src/nextjournal/clerk.clj index a489f6b61..5fff379be 100644 --- a/src/nextjournal/clerk.clj +++ b/src/nextjournal/clerk.clj @@ -309,8 +309,16 @@ #_(->html-extension "hello.clj") + +(defn- path-to-url-canonicalize + "Canonicalizes the system specific path separators in `PATH` (e.g. `\\` + on MS-Windows) to URL-compatible forward slashes." + [path] + (str/replace path fs/file-separator "/")) + (defn build-static-app! - "Builds a static html app of the notebooks. Takes an options map with keys: + "Builds a static html app of the notebooks and opens the app in the + default browser. Takes an options map with keys: - `:paths` a vector of relative paths to notebooks to include in the build - `:bundle?` builds a single page app versus a folder with an html page for each notebook (defaults to `true`) @@ -321,7 +329,7 @@ " [{:as opts :keys [paths out-path live-js? bundle? browse?] :or {paths clerk-docs - out-path "public/build" + out-path (str "public" fs/file-separator "build") live-js? view/live-js? bundle? true browse? true}}] @@ -342,7 +350,7 @@ (when browse? (if (and live-js? (str/starts-with? out-path "public/")) (browse/browse-url (str "http://localhost:7778/" (str/replace out-path "public/" ""))) - (browse/browse-url index-html))))) + (browse/browse-url (-> index-html fs/absolutize .toString path-to-url-canonicalize)))))) #_(build-static-app! {:paths ["index.clj" "notebooks/rule_30.clj" "notebooks/markdown.md"] :bundle? true}) #_(build-static-app! {:paths ["index.clj" "notebooks/rule_30.clj" "notebooks/markdown.md"] :bundle? false :path-prefix "build/"}) diff --git a/test/nextjournal/clerk_test.clj b/test/nextjournal/clerk_test.clj new file mode 100644 index 000000000..efb2afc1d --- /dev/null +++ b/test/nextjournal/clerk_test.clj @@ -0,0 +1,24 @@ +(ns nextjournal.clerk-test + (:require [babashka.fs :as fs] + [clojure.java.browse :as browse] + [clojure.string :as str] + [clojure.test :refer :all] + [nextjournal.clerk :as clerk]) + (:import (java.io File))) + +(deftest url-canonicalize + (testing "canonicalization of file components into url components" + (let [dice (str/join (File/separator) ["notebooks" "dice.clj"])] + (is (= (#'clerk/path-to-url-canonicalize dice) (str/replace dice (File/separator) "/")))))) + +(deftest static-app + (let [url* (volatile! nil)] + (with-redefs [clojure.java.browse/browse-url (fn [path] + (vreset! url* path))] + (testing "browser receives canonical url in this system arch" + (fs/with-temp-dir [temp {}] + (let [expected (-> (str/join (java.io.File/separator) [(.toString temp) "index.html"]) + (str/replace (java.io.File/separator) "/"))] + (clerk/build-static-app! {:paths ["notebooks/hello.clj"] + :out-path temp}) + (is (= expected @url*))))))))