diff --git a/CHANGELOG.md b/CHANGELOG.md index 1de52e3..3e256cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ ## [0.5.0] +- #29 converts `mentat.clerk-utils` to `cljc`. The CLJS side doesn't do + anything, but this allows the require and macro calls to live inside `cljc` + files. + - #28: - Removes the `babashka` dependency from `mentat.clerk-utils.build` diff --git a/src/mentat/clerk_utils.clj b/src/mentat/clerk_utils.cljc similarity index 60% rename from src/mentat/clerk_utils.clj rename to src/mentat/clerk_utils.cljc index ebc216e..dc1d18d 100644 --- a/src/mentat/clerk_utils.clj +++ b/src/mentat/clerk_utils.cljc @@ -4,28 +4,33 @@ Expect this namespace to fission off into more specific namespaces as the catalogue of functions grows." - (:import (java.io FileNotFoundException))) - -(defn- ns-present? - "Returns true if `sym` corresponds to a namespace present on the classpath and + #?(:clj + (:import (java.io FileNotFoundException))) + #?(:cljs + (:require-macros [mentat.clerk-utils]))) + +#?(:clj + (defn- ns-present? + "Returns true if `sym` corresponds to a namespace present on the classpath and available for loading, false otherwise. NOTE that calling [[ns-present?]] will `require` the namespace as a side-effect." - [sym] - (try (nil? (require sym)) - (catch FileNotFoundException _ false))) + [sym] + (try (nil? (require sym)) + (catch FileNotFoundException _ false)))) -(defn- try-resolve - "Takes a namespaced symbol `sym` and delegates to +#?(:clj + (defn- try-resolve + "Takes a namespaced symbol `sym` and delegates to `clojure.core/requiring-resolve` if its namespace is present on the classpath, returns `nil` otherwise. NOTE that calling [[try-resolve]] will `require` the `sym`'s namespace as a side-effect." - [sym] - (try (requiring-resolve sym) - (catch FileNotFoundException _ nil))) + [sym] + (try (requiring-resolve sym) + (catch FileNotFoundException _ nil)))) ;; ## Visibility Macros @@ -37,8 +42,10 @@ Like `comment`, body must still be written in well-formed Clojure that can be parsed by the reader." [& body] - (when (ns-present? 'nextjournal.clerk) - `(do ~@body))) + (when-not (:ns &env) + (when #?(:clj (ns-present? 'nextjournal.clerk) + :cljs false) + `(do ~@body)))) (defmacro ->clerk-only "If `nextjournal.clerk` is present on the classpath AND Clerk is evaluating @@ -48,7 +55,9 @@ Use this macro to supply examples and forms that should render in a Clerk environment but not affect normal Clojure evaluation in any way." ([& body] - (when (some-> (try-resolve - 'nextjournal.clerk.config/*in-clerk*) - deref) - `(do ~@body)))) + (when-not (:ns &env) + (when #?(:clj (some-> (try-resolve + 'nextjournal.clerk.config/*in-clerk*) + deref) + :cljs false) + `(do ~@body)))))