Skip to content

Commit

Permalink
Make edn transmission resilient to symbols and keywords with more tha…
Browse files Browse the repository at this point in the history
…n one slash (#588)

Symbols and keywords with more than one slash like `foo/bar/baz` can be read by `read-string` but not in ClojureScript which is based on `tools.reader`. This changes the `roundtrippable?` check to read using tools.reader to ensure a symbol will be tagged as a string in case it's not readable, fixing a read exception that could occur when reading in the browser.

---------

Co-authored-by: Andrea Amantini <[email protected]>
  • Loading branch information
mk and zampino authored Dec 7, 2023
1 parent ff2bf8b commit 6dab22c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 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`

* 🐜 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

* 🐞 Fix `unquote` in experimental cljs Clerk editor, fixes [#576](https://github.com/nextjournal/clerk/issues/576) @sritchie
Expand Down
7 changes: 4 additions & 3 deletions src/nextjournal/clerk/viewer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
[flatland.ordered.map :refer [ordered-map]]
#?@(:clj [[babashka.fs :as fs]
[clojure.repl :refer [demunge]]
[clojure.tools.reader :as tools.reader]
[editscript.edit]
[nextjournal.clerk.config :as config]
[nextjournal.clerk.analyzer :as analyzer]]
Expand Down Expand Up @@ -361,7 +362,7 @@

#?(:clj (defn roundtrippable? [x]
(try
(= x (-> x str read-string))
(= x (-> x str tools.reader/read-string))
(catch Exception _e false))))

#?(:clj
Expand All @@ -375,7 +376,7 @@
#?(:clj
(defmethod print-method clojure.lang.Symbol [o w]
(if (or (roundtrippable? o)
(= (name o) "?@")) ;; splicing reader conditional, see issue #338
(= (name o) "?@")) ;; splicing reader conditional, see issue #338
(print-simple o w)
(.write w (pr-str (->viewer-eval (if-let [ns (namespace o)]
(list 'symbol ns (name o))
Expand Down Expand Up @@ -1105,7 +1106,7 @@

#?(:clj
(defn edn-roundtrippable? [x]
(= x (-> x ->edn read-string))))
(= x (-> x ->edn tools.reader/read-string))))

#?(:clj
(defn throw-if-sync-var-is-invalid [var]
Expand Down
6 changes: 6 additions & 0 deletions test/nextjournal/clerk/viewer_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,12 @@
(is (= "#viewer-eval (symbol \"~\")"
(pr-str (symbol "~")))))

(testing "symbols and keywords with two slashes readable by `read-string` but not `tools.reader/read-string` print as viewer-eval"
(is (= "#viewer-eval (symbol \"foo\" \"bar/baz\")"
(pr-str (read-string "foo/bar/baz"))))
(is (= "#viewer-eval (keyword \"foo\" \"bar/baz\")"
(pr-str (read-string ":foo/bar/baz")))))

(testing "splicing reader conditional prints normally (issue #338)"
(is (= "?@" (pr-str (symbol "?@")))))

Expand Down

0 comments on commit 6dab22c

Please sign in to comment.