Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into static-xhr-routing
Browse files Browse the repository at this point in the history
  • Loading branch information
zampino committed Dec 11, 2023
2 parents 870e34c + babbba5 commit 115f8f6
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 33 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@ Changes can be:

* 💫 Support using Markdown syntax in `clerk/caption` text

* 💫 Redesign examples viewer to be more readable and in a way that doesn't force `display: flex` onto contents.

* 🛠 Bump depdendencies

* `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 `:nextjournal.clerk/page-size` option throwing when set on string values, fixes [#584][https://github.com/nextjournal/clerk/issues/584]

* 🐞 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
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
54 changes: 24 additions & 30 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 @@ -609,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 @@ -622,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 @@ -631,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 @@ -866,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 @@ -1105,7 +1110,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 Expand Up @@ -1136,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 Expand Up @@ -1513,15 +1511,13 @@

(defn get-elision [wrapped-value]
(let [{:as fetch-opts :keys [n]} (->fetch-opts wrapped-value)]
(merge fetch-opts (bounded-count-opts n (->value wrapped-value)))))
(when (number? n)
(merge fetch-opts (bounded-count-opts n (->value wrapped-value))))))

#_(get-elision (present (range)))
#_(get-elision (present "abc"))
#_(get-elision (present (str/join (repeat 1000 "abc"))))

(defn get-fetch-opts-n [wrapped-value]
(-> wrapped-value ->fetch-opts :n))

(defn present+paginate-children [{:as wrapped-value :nextjournal/keys [budget viewers preserve-keys?] :keys [!budget]}]
(let [{:as fetch-opts :keys [offset n]} (->fetch-opts wrapped-value)
xs (->value wrapped-value)
Expand All @@ -1543,10 +1539,9 @@
(conj (let [fetch-opts (assoc elision :offset new-offset)]
(make-elision viewers fetch-opts))))))

(defn present+paginate-string [{:as wrapped-value :nextjournal/keys [viewers viewer value]}]
(let [{:as elision :keys [n total path offset]} (and (:page-size viewer)
(get-elision wrapped-value))]
(if (and n (< n total))
(defn present+paginate-string [{:as wrapped-value :nextjournal/keys [viewers value]}]
(let [{:as elision :keys [n total path offset]} (get-elision wrapped-value)]
(if (and elision n (< n total))
(let [new-offset (min (+ (or offset 0) n) total)]
(cond-> [(subs value (or offset 0) new-offset)]
(pos? (- total new-offset)) (conj (let [fetch-opts (-> elision
Expand Down Expand Up @@ -1860,19 +1855,18 @@
(-> wrapped-value
mark-preserve-keys
(assoc :nextjournal/viewer {:render-fn '(fn [{:keys [form val]} opts]
[:div.flex.flex-wrap
{:class "py-[7px]"}
[:div [:div.bg-slate-100.px-2.rounded
(nextjournal.clerk.render/inspect-presented opts form)]]
[:div.flex.mt-1
[:div.mx-2.font-sans.text-xs.text-slate-500 {:class "mt-[2px]"} ""]
[:div.mb-3.last:mb-0
[:div.bg-slate-100.dark:bg-slate-800.px-4.py-2.border-l-2.border-slate-200.dark:border-slate-700
(nextjournal.clerk.render/inspect-presented opts form)]
[:div.pt-2.px-4.border-l-2.border-transparent
(nextjournal.clerk.render/inspect-presented opts val)]])})
(update-in [:nextjournal/value :form] code)))})

(def examples-viewer
{:transform-fn (update-val (fn [examples]
(mapv (partial with-viewer example-viewer) examples)))
:render-fn '(fn [examples opts]
(into [:div.border-l-2.border-slate-300.pl-4
[:div.uppercase.tracking-wider.text-xs.font-sans.text-slate-500.mt-4.mb-2 "Examples"]]
(nextjournal.clerk.render/inspect-children opts) examples))})
[:div
[:div.uppercase.tracking-wider.text-xs.font-sans.font-bold.text-slate-500.dark:text-white.mb-2.mt-3 "Examples"]
(into [:div]
(nextjournal.clerk.render/inspect-children opts) examples)])})
6 changes: 5 additions & 1 deletion test/nextjournal/clerk/eval_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@
:nextjournal/hash string?}}]
(eval+extract-doc-blocks "(range)"))))

(testing "Skipping pagination for strings"
(is (= "012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849"
(-> (eval+extract-doc-blocks "^{:nextjournal.clerk/page-size nil} (apply str (range 50))")
second :nextjournal/value :nextjournal/presented :nextjournal/value))))

(testing "assigns folded visibility"
(is (match? [{:nextjournal/viewer {:name `viewer/folded-code-block-viewer}
:nextjournal/value "{:some :map}"}
Expand Down Expand Up @@ -240,4 +245,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)))))
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 115f8f6

Please sign in to comment.