Skip to content

Commit 8b13cb6

Browse files
mkborkdude
authored andcommitted
Add inspect-wrapped-values helper and use it in book
Co-Authored-By: Michiel Borkent <[email protected]>
1 parent 211b912 commit 8b13cb6

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

book.clj

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -473,20 +473,13 @@ v/default-viewers
473473
;; transforms it such that Clerk can send it to the browser where it
474474
;; will be rendered.
475475

476-
477-
^{::clerk/visibility {:code :fold :result :hide}}
478-
(defn show-raw-value [x]
479-
(binding [*print-namespace-maps* false]
480-
(clerk/code (with-out-str (clojure.pprint/pprint x)))))
481-
482476
;; Let's start with one of the simplest examples. You can see that
483477
;; `present` takes our value `1` and transforms it into a map, with
484478
;; `1` under a `:nextjournal/value` key and the number viewer assigned
485479
;; under the `:nextjournal/viewer` key. We call this map a
486480
;; `wrapped-value`.
487481

488-
489-
^{::clerk/viewer show-raw-value}
482+
^{::clerk/viewer v/inspect-wrapped-values ::clerk/auto-expand-results? true}
490483
(v/present 1)
491484

492485
;; This data structure is sent over Clerk's websocket to the
@@ -495,7 +488,7 @@ v/default-viewers
495488

496489
;; Now onto something slightly more complex, `#{1 2 3}`.
497490

498-
^{::clerk/viewer show-raw-value}
491+
^{::clerk/viewer v/inspect-wrapped-values ::clerk/auto-expand-results? true}
499492
(v/present #{1 2 3})
500493

501494

@@ -524,13 +517,14 @@ v/default-viewers
524517
;; When writing your own viewer, the first extension point you should reach for is `:tranform-fn`.
525518

526519
#_ "exercise: wrap this in `v/present` and call it at the REPL"
527-
(v/with-viewer {:transform-fn #(clerk/html [:pre (pr-str %)])}
520+
(v/with-viewer {:transform-fn v/inspect-wrapped-values}
528521
"Exploring the viewer api")
529522

530523
;; As you can see the argument to the `:transform-fn` isn't just the
531-
;; string we're passing it, but a `wrapped-value`. We will look at
532-
;; what this enables in a bit. But let's look at one of the simplest
533-
;; examples first.
524+
;; string we're passing it, but a map with the original value under a
525+
;; `:nextjournal/value` key. We call this map a `wrapped-value`. We
526+
;; will look at what this enables in a bit. But let's look at one of
527+
;; the simplest examples first.
534528

535529
;; **A first simple example**
536530

@@ -543,7 +537,7 @@ v/default-viewers
543537
"James Clerk Maxwell")
544538

545539
;; The `:transform-fn` runs on the JVM, which means you can explore what it does at your REPL by calling `v/present` on such a value.
546-
^{::clerk/viewer show-raw-value}
540+
^{::clerk/viewer v/inspect-wrapped-values}
547541
(v/present (v/with-viewer greet-viewer
548542
"James Clerk Maxwell"))
549543

@@ -580,7 +574,7 @@ v/table-viewer
580574
;; `clerk/mark-presented` as a `:transform-fn`. Compare the result
581575
;; below in which `[1 2 3]` appears unaltered with what you see above.
582576

583-
^{::clerk/viewer show-raw-value}
577+
^{::clerk/viewer v/inspect-wrapped-values}
584578
(v/present (clerk/with-viewer {:transform-fn clerk/mark-presented
585579
:render-fn '(fn [x] [:pre (pr-str x)])}
586580
[1 2 3]))
@@ -591,7 +585,7 @@ v/table-viewer
591585
;; `clerk/mark-preserve-keys`. This will still transform (and
592586
;; paginate) the values of the map, but leave the keys unaltered.
593587

594-
^{::clerk/viewer show-raw-value}
588+
^{::clerk/viewer v/inspect-wrapped-values ::clerk/auto-expand-results? true}
595589
(v/present (clerk/with-viewer {:transform-fn clerk/mark-preserve-keys}
596590
{:hello 42}))
597591

src/nextjournal/clerk/viewer.cljc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,17 @@
305305
(defn mark-preserve-keys [wrapped-value]
306306
(assoc wrapped-value :nextjournal/preserve-keys? true))
307307

308+
(defn inspect-wrapped-values
309+
"Takes `x` and modifies it such that Clerk will show raw
310+
wrapped-values. Useful for inspecting the inner workings of the
311+
viewer api. Also useable as a `:transform-fn`.
312+
313+
Will eagerly walk the whole data structure so unsuited for infinite
314+
sequences."
315+
[x]
316+
;; exploits the fact that that our keyword renderer doesn't show spaces
317+
(w/postwalk-replace {:nextjournal/value (keyword "nextjournal/value ")} x))
318+
308319
(defn fetch-all [_opts _xs]
309320
(throw (ex-info "`fetch-all` is deprecated, please use a `:transform-fn` with `mark-presented` instead." {})))
310321

0 commit comments

Comments
 (0)