diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bc09dfad..c63f62d1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,8 @@ Changes can be: * `com.taoensso/nippy` to `3.4.0-beta1` * `io.github.nextjournal/markdown` to `0.5.146` +* 🐜 Fix blank screen caused by react unmounting when an exception occurs during `clerk/show!`, fixes [#586](https://github.com/nextjournal/clerk/issues/586) @elken + * 🐜 Make edn transmission resilient to symbols and keywords containing multiple slashes like `foo/bar/baz`. Those can be read by `read-string` but not in ClojureScript which is based on `tools.reader`. * 🐞 Fix caching behaviour of `clerk/image` and support overriding image-viewer by name diff --git a/src/nextjournal/clerk/render.cljs b/src/nextjournal/clerk/render.cljs index 1c65bb235..0f83bd991 100644 --- a/src/nextjournal/clerk/render.cljs +++ b/src/nextjournal/clerk/render.cljs @@ -484,7 +484,7 @@ [:div.font-bold "Unhandled " type]) [:div.font-bold.mt-1 message] (when data - [:div.mt-1 [inspect data]])]) + [:div.mt-1 [inspect (viewer/inspect-wrapped-values data)]])]) via)) [:div.py-6.overflow-x-auto [:table.w-full diff --git a/src/nextjournal/clerk/viewer.cljc b/src/nextjournal/clerk/viewer.cljc index 61980f23b..62f018c9f 100644 --- a/src/nextjournal/clerk/viewer.cljc +++ b/src/nextjournal/clerk/viewer.cljc @@ -610,6 +610,9 @@ #_(present @nextjournal.clerk.webserver/!doc) +(defn update-if [m k f] (if (k m) (update m k f) m)) +#_(update-if {:n "42"} :n #(Integer/parseInt %)) + (defn with-block-viewer [doc {:as cell :keys [type id]}] (case type :markdown (let [{:keys [content]} (:doc cell) @@ -623,7 +626,7 @@ ::doc doc} doc))])) (partition-by (comp #{:image} :type) content))) - :code (let [cell (update cell :result apply-viewer-unwrapping-var-from-def) + :code (let [cell (update-if cell :result apply-viewer-unwrapping-var-from-def) {:keys [code? result? fold?]} (->display cell) eval? (-> cell :result :nextjournal/value (get-safe :nextjournal/value) viewer-eval?)] (cond-> [] @@ -632,7 +635,7 @@ {:nextjournal/render-opts (assoc (select-keys cell [:loc]) :id (processed-block-id (str id "-code")))} (dissoc cell :result))) - (or result? eval?) + (and (:result cell) (or result? eval?)) (conj (cond-> (ensure-wrapped (-> cell (assoc ::doc doc) (set/rename-keys {:result ::result}))) (and eval? (not result?)) (assoc :nextjournal/viewer (assoc result-viewer :render-fn '(fn [_] [:<>]))))))))) @@ -867,7 +870,8 @@ {:name `throwable-viewer :render-fn 'nextjournal.clerk.render/render-throwable :pred (fn [e] (instance? #?(:clj Throwable :cljs js/Error) e)) - :transform-fn (comp mark-presented (update-val (comp demunge-ex-data datafy/datafy)))}) + :transform-fn (comp mark-presented (update-val (comp demunge-ex-data + datafy/datafy)))}) (def image-viewer {#?@(:clj [:pred #(instance? BufferedImage %) @@ -1137,13 +1141,6 @@ (map (juxt #(list 'quote (symbol %)) #(->> % deref deref (list 'quote)))) (extract-sync-atom-vars doc))))) -(defn update-if [m k f] - (if (k m) - (update m k f) - m)) - -#_(update-if {:n "42"} :n #(Integer/parseInt %)) - (declare html doc-url) (defn home? [{:keys [nav-path]}] diff --git a/test/nextjournal/clerk/eval_test.clj b/test/nextjournal/clerk/eval_test.clj index 8d157db21..bd1c668c4 100644 --- a/test/nextjournal/clerk/eval_test.clj +++ b/test/nextjournal/clerk/eval_test.clj @@ -240,4 +240,3 @@ (catch Exception _ nil)) (clerk/show! (java.io.StringReader. code)) (is (= result-first-run (get-result))))))) - diff --git a/test/nextjournal/clerk/parser_test.clj b/test/nextjournal/clerk/parser_test.clj index 95dcf3b6c..54a9f5fb9 100644 --- a/test/nextjournal/clerk/parser_test.clj +++ b/test/nextjournal/clerk/parser_test.clj @@ -3,7 +3,8 @@ [matcher-combinators.matchers :as m] [matcher-combinators.test :refer [match?]] [nextjournal.clerk.analyzer-test :refer [analyze-string]] - [nextjournal.clerk.parser :as parser])) + [nextjournal.clerk.parser :as parser] + [nextjournal.clerk.view :as view])) (defmacro with-ns-binding [ns-sym & body] `(binding [*ns* (find-ns ~ns-sym)] @@ -156,3 +157,13 @@ par two")))) (testing "unreadable forms" (is (= (parser/text-with-clerk-metadata-removed "^{:un :balanced :map} (do nothing)" clerk-ns-alias) "^{:un :balanced :map} (do nothing)")))) + +(deftest presenting-a-parsed-document + (testing "presenting a parsed document doesn't produce garbage" + (is (match? [{:nextjournal/viewer {:name 'nextjournal.clerk.viewer/code-block-viewer}} + {:nextjournal/viewer {:name 'nextjournal.clerk.viewer/code-block-viewer}} + {:nextjournal/viewer {:name 'nextjournal.clerk.viewer/code-block-viewer}}] + (-> (parser/parse-clojure-string {:doc? true} "(ns testing-presented-parsed) 123 :ahoi") + view/doc->viewer + :nextjournal/value + :blocks)))))