Skip to content

Commit 34962e3

Browse files
committed
Clerk's new viewer API centered around :transform-fn
1 parent 7eafeb7 commit 34962e3

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

deps.edn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{:paths ["dev" "notebooks" "resources"]
2-
:deps {io.github.nextjournal/clerk {:mvn/version "0.7.418"}
2+
:deps {io.github.nextjournal/clerk {:git/sha "88b1df4607d9612678012cde4fea98cfbc0871fe"}
33

44
;; input various external data formats
55
com.github.seancorfield/next.jdbc {:mvn/version "1.2.659"}

notebooks/introduction.clj

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,17 @@
182182
;; In addition to these defaults, you can also attach a custom viewer
183183
;; to any form. Here we make our own little viewer to greet James
184184
;; Clerk Maxwell:
185+
185186
(clerk/with-viewer '#(v/html [:div "Greetings to " [:strong %] "!"])
186187
"James Clerk Maxwell")
187188

188189
;; But we can do more interesting things, like using a predicate
189190
;; function to match numbers and turn them into headings, or
190191
;; converting string into paragraphs.
191-
(clerk/with-viewers [{:pred number?
192-
:render-fn '#(v/html [(keyword (str "h" %)) (str "Heading " %)])}
193-
{:pred string?
194-
:render-fn '#(v/html [:p %])}]
192+
(clerk/with-viewers (clerk/add-viewers [{:pred number?
193+
:render-fn '#(v/html [(keyword (str "h" %)) (str "Heading " %)])}
194+
{:pred string?
195+
:render-fn '#(v/html [:p %])}])
195196
[1 "To begin at the beginning:"
196197
2 "It is Spring, moonless night in the small town, starless and bible-black,"
197198
3 "the cobblestreets silent and the hunched,"
@@ -200,24 +201,24 @@
200201

201202
;; Or you could use black and white squares to render numbers:
202203
^::clerk/no-cache
203-
(clerk/with-viewers [{:pred number?
204-
:render-fn '#(v/html [:div.inline-block {:style {:width 16 :height 16}
205-
:class (if (pos? %) "bg-black" "bg-white border-solid border-2 border-black")}])}]
204+
(clerk/with-viewers (clerk/add-viewers [{:pred number?
205+
:render-fn '#(v/html [:div.inline-block {:style {:width 16 :height 16}
206+
:class (if (pos? %) "bg-black" "bg-white border-solid border-2 border-black")}])}])
206207
(take 10 (repeatedly #(rand-int 2))))
207208

208209
;; Or build your own colour parser and then use it to generate swatches:
209-
(clerk/with-viewers
210-
[{:pred #(and (string? %)
211-
(re-matches
212-
(re-pattern
213-
(str "(?i)"
214-
"(#(?:[0-9a-f]{2}){2,4}|(#[0-9a-f]{3})|"
215-
"(rgb|hsl)a?\\((-?\\d+%?[,\\s]+){2,3}\\s*[\\d\\.]+%?\\))")) %))
216-
:render-fn '#(v/html [:div.inline-block.rounded-sm.shadow
217-
{:style {:width 16
218-
:height 16
219-
:border "1px solid rgba(0,0,0,.2)"
220-
:background-color %}}])}]
210+
(clerk/with-viewers (clerk/add-viewers
211+
[{:pred #(and (string? %)
212+
(re-matches
213+
(re-pattern
214+
(str "(?i)"
215+
"(#(?:[0-9a-f]{2}){2,4}|(#[0-9a-f]{3})|"
216+
"(rgb|hsl)a?\\((-?\\d+%?[,\\s]+){2,3}\\s*[\\d\\.]+%?\\))")) %))
217+
:render-fn '#(v/html [:div.inline-block.rounded-sm.shadow
218+
{:style {:width 16
219+
:height 16
220+
:border "1px solid rgba(0,0,0,.2)"
221+
:background-color %}}])}])
221222
["#571845"
222223
"rgb(144,12,62)"
223224
"rgba(199,0,57,1.0)"
@@ -230,23 +231,24 @@
230231
;; what it sends to the browser, you can specify a `:transform-fn`
231232
;; that will be called before the data is sent over the wire.
232233

233-
#_ "TODO example of using a :transform-fn"
234-
235234
;; #### 🏞 Customizing Data Fetching
236235

237236
;; Sometimes you might want to create a custom viewer that overrides
238237
;; Clerk's automatic paging behavior. In this example, we use a custom
239-
;; `fetch-fn` that specifies a `content-type` to tell Clerk to serve
238+
;; `transform-fn` that specifies a `content-type` to tell Clerk to serve
240239
;; arbitrary byte arrays as PNG images.
241240

242241
;; Notice that the image is conveyed out-of-band using the `url-for`
243242
;; function to get a URL from which to fetch the blob.
244243

245-
(clerk/set-viewers! [{:pred bytes?
246-
:fetch-fn (fn [_ bytes] {:nextjournal/content-type "image/png"
247-
:nextjournal/value bytes})
244+
(clerk/add-viewers! [{:pred bytes?
245+
:transform-fn (fn [{bytes :nextjournal/value}]
246+
{:nextjournal/presented? true
247+
:nextjournal/content-type "image/png"
248+
:nextjournal/value bytes})
248249
:render-fn '(fn [blob] (v/html [:img {:src (v/url-for blob)}]))}])
249250

251+
250252
(.. (HttpClient/newHttpClient)
251253
(send (.build (HttpRequest/newBuilder (URI. "https://upload.wikimedia.org/wikipedia/commons/5/57/James_Clerk_Maxwell.png")))
252254
(HttpResponse$BodyHandlers/ofByteArray)) body)

notebooks/rule_30.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
;; `html` viewer, which allows us to emit arbitrary hiccup to
1919
;; represent a value.
2020

21-
(clerk/set-viewers!
21+
(clerk/add-viewers!
2222
[{:pred number? :render-fn '#(v/html [:div.inline-block {:style {:width 16 :height 16}
2323
:class (if (pos? %) "bg-black" "bg-white border-solid border-2 border-black")}])}
2424
{:pred list? :render-fn '#(v/html (into [:div.flex.flex-col] (v/inspect-children %2) %1))}

0 commit comments

Comments
 (0)