|
182 | 182 | ;; In addition to these defaults, you can also attach a custom viewer
|
183 | 183 | ;; to any form. Here we make our own little viewer to greet James
|
184 | 184 | ;; Clerk Maxwell:
|
| 185 | + |
185 | 186 | (clerk/with-viewer '#(v/html [:div "Greetings to " [:strong %] "!"])
|
186 | 187 | "James Clerk Maxwell")
|
187 | 188 |
|
188 | 189 | ;; But we can do more interesting things, like using a predicate
|
189 | 190 | ;; function to match numbers and turn them into headings, or
|
190 | 191 | ;; 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 %])}]) |
195 | 196 | [1 "To begin at the beginning:"
|
196 | 197 | 2 "It is Spring, moonless night in the small town, starless and bible-black,"
|
197 | 198 | 3 "the cobblestreets silent and the hunched,"
|
|
200 | 201 |
|
201 | 202 | ;; Or you could use black and white squares to render numbers:
|
202 | 203 | ^::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")}])}]) |
206 | 207 | (take 10 (repeatedly #(rand-int 2))))
|
207 | 208 |
|
208 | 209 | ;; 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 %}}])}]) |
221 | 222 | ["#571845"
|
222 | 223 | "rgb(144,12,62)"
|
223 | 224 | "rgba(199,0,57,1.0)"
|
|
230 | 231 | ;; what it sends to the browser, you can specify a `:transform-fn`
|
231 | 232 | ;; that will be called before the data is sent over the wire.
|
232 | 233 |
|
233 |
| -#_ "TODO example of using a :transform-fn" |
234 |
| - |
235 | 234 | ;; #### 🏞 Customizing Data Fetching
|
236 | 235 |
|
237 | 236 | ;; Sometimes you might want to create a custom viewer that overrides
|
238 | 237 | ;; 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 |
240 | 239 | ;; arbitrary byte arrays as PNG images.
|
241 | 240 |
|
242 | 241 | ;; Notice that the image is conveyed out-of-band using the `url-for`
|
243 | 242 | ;; function to get a URL from which to fetch the blob.
|
244 | 243 |
|
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}) |
248 | 249 | :render-fn '(fn [blob] (v/html [:img {:src (v/url-for blob)}]))}])
|
249 | 250 |
|
| 251 | + |
250 | 252 | (.. (HttpClient/newHttpClient)
|
251 | 253 | (send (.build (HttpRequest/newBuilder (URI. "https://upload.wikimedia.org/wikipedia/commons/5/57/James_Clerk_Maxwell.png")))
|
252 | 254 | (HttpResponse$BodyHandlers/ofByteArray)) body)
|
|
0 commit comments