Skip to content

Commit

Permalink
Ensure ex-data is always inspectable (#587)
Browse files Browse the repository at this point in the history
Fix blank screen caused by react unmounting when an exception occurs during `clerk/show!`. Also make parsed (but not evaluated) documents representable.

Fixes #586.
  • Loading branch information
zampino authored Dec 7, 2023
1 parent 6dab22c commit 9547880
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/nextjournal/clerk/render.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 7 additions & 10 deletions src/nextjournal/clerk/viewer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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-> []
Expand All @@ -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 [_] [:<>])))))))))
Expand Down Expand Up @@ -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 %)
Expand Down Expand Up @@ -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]}]
Expand Down
1 change: 0 additions & 1 deletion test/nextjournal/clerk/eval_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,3 @@
(catch Exception _ nil))
(clerk/show! (java.io.StringReader. code))
(is (= result-first-run (get-result)))))))

13 changes: 12 additions & 1 deletion test/nextjournal/clerk/parser_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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)))))

0 comments on commit 9547880

Please sign in to comment.