Skip to content

Commit 8dd37db

Browse files
mkphilippamarkovics
authored andcommitted
Rename :nextjournal.clerk/opts to :nextjournal.clerk/render-opts (#517)
To clarify this options map is available as the second arg to parametrize the `:render-fn`. Still support the old name for now.
1 parent eaf7bf0 commit 8dd37db

File tree

8 files changed

+57
-46
lines changed

8 files changed

+57
-46
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Changes can be:
1616

1717
* Support first-class `:add-viewers` attribute on viewer map which will do `clerk/add-viewers` before passing viewers down the tree. Use it in `table-viewer` and `markdown-viewer`. Both these viewers can now be customized more easily. For example, you can customize the `table-viewer` to show missing values differently, see [Book of Clerk](https://book.clerk.vision/#tables).
1818

19+
* 🚨 Rename `:nextjournal.clerk/opts` to `:nextjournal.clerk/render-opts` to clarify this options map is available as the second arg to parametrize the `:render-fn`. Still support the `:nextjournal.clerk/opts` for now.
20+
1921
* 💫 Assign `:name` to every viewer in `default-viewers`
2022

2123
* 🐞 Show correct non-var return value for deflike form, fixes [#499](https://github.com/nextjournal/clerk/issues/499)

notebooks/onwards.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,27 @@ Notes about what currently breaks 💥 and what could be better tomorrow.
8888
- [x] Set expanded state depending on shape (width) of the data
8989
- [x] Allow to control viewer expansion state programmatically
9090
- [x] Support cherry as alternative js evaluator
91-
- [ ] Make `doc-url` work in live app using `v/clerk-eval` with homepage, 404 and symbol links
91+
- [x] Make `doc-url` work in live app using `v/clerk-eval` with homepage, 404 and symbol links
92+
- [x] Build static build on CI
93+
- [x] Hook up new table viewer
94+
- [x] Status log
95+
- [x] Rename `:nextjournal.clerk/opts` to `:nextjournal.clerk/render-opts`
96+
- [ ] Improve page load performance of large documents like book (probably by evaluating each `:render-fn` only once)
97+
- [ ] Introduce window/session concept to support opening concurrent documents
98+
- [ ] Introduce per-session/connection state for sync
99+
- [ ] Make clerk sync recomputations more granular (only recompute deps of changed atom)
92100
- [ ] Global config with consistent inheritance to ns, expression
93-
- [ ] Add sync atoms to book
94-
- [ ] Add tap inspector to book
95-
- [ ] Add customizing clerk: markdown backtick eval example to book
96101
- [ ] Tap & eval viewer window
97102
- [ ] Add multiviewer to default viewers
98103
- [ ] Make stacktraces clickable
99104
- [ ] Mark uncacheable results in browser window including explanation
100-
- [x] Build static build on CI
101-
- [x] Hook up new table viewer
102105
- [ ] Add CAS storage option for static builds
103-
- [x] Status log
106+
- [ ] Improve performance of analysis & presentation
107+
## 📖 Book Updates
108+
- [ ] Explain `render-opts` and `viewer-opts` in book
109+
- [ ] Add sync atoms to book
110+
- [ ] Add tap inspector to book
111+
- [ ] Add customizing clerk: markdown backtick eval example to book
112+
- [ ] Add example for cross-document table of contents
104113
## 💡 Ideas
105114
- [ ] Hook up distributed caching using CAS + cloud bucket

src/nextjournal/clerk.clj

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
130130
* `:nextjournal.clerk/width`: set the width to `:full`, `:wide`, `:prose`
131131
* `:nextjournal.clerk/viewers`: a seq of viewers to use for presentation of this value and its children
132-
* `:nextjournal.clerk/opts`: a map argument that will be passed to the viewers `:render-fn`"
132+
* `:nextjournal.clerk/render-opts`: a map argument that will be passed as a secong arg to the viewers `:render-fn`"
133133
([viewer x] (with-viewer viewer {} x))
134134
([viewer viewer-opts x] (v/with-viewer viewer viewer-opts x)))
135135

@@ -221,7 +221,7 @@
221221
222222
* `:nextjournal.clerk/width`: set the width to `:full`, `:wide`, `:prose`
223223
* `:nextjournal.clerk/viewers`: a seq of viewers to use for presentation of this value and its children
224-
* `:nextjournal.clerk/opts`: a map argument that will be passed to the viewers `:render-fn`"
224+
* `:nextjournal.clerk/render-opts`: a map argument that will be passed as a secong arg to the viewers `:render-fn`"
225225
([x] (v/html x))
226226
([viewer-opts x] (v/html viewer-opts x)))
227227

@@ -232,7 +232,7 @@
232232
233233
* `:nextjournal.clerk/width`: set the width to `:full`, `:wide`, `:prose`
234234
* `:nextjournal.clerk/viewers`: a seq of viewers to use for presentation of this value and its children
235-
* `:nextjournal.clerk/opts`: a map argument that will be passed to the viewers `:render-fn`"
235+
* `:nextjournal.clerk/render-opts`: a map argument that will be passed as a secong arg to the viewers `:render-fn`"
236236
([x] (v/md x))
237237
([viewer-opts x] (v/md viewer-opts x)))
238238

@@ -243,7 +243,7 @@
243243
244244
* `:nextjournal.clerk/width`: set the width to `:full`, `:wide`, `:prose`
245245
* `:nextjournal.clerk/viewers`: a seq of viewers to use for presentation of this value and its children
246-
* `:nextjournal.clerk/opts`: a map argument that will be passed to the viewers `:render-fn`"
246+
* `:nextjournal.clerk/render-opts`: a map argument that will be passed as a secong arg to the viewers `:render-fn`"
247247
([x] (v/plotly x))
248248
([viewer-opts x] (v/plotly viewer-opts x)))
249249

@@ -258,7 +258,7 @@
258258
259259
* `:nextjournal.clerk/width`: set the width to `:full`, `:wide`, `:prose`
260260
* `:nextjournal.clerk/viewers`: a seq of viewers to use for presentation of this value and its children
261-
* `:nextjournal.clerk/opts`: a map argument that will be passed to the viewers `:render-fn`"
261+
* `:nextjournal.clerk/render-opts`: a map argument that will be passed as a secong arg to the viewers `:render-fn`"
262262
([x] (v/vl x))
263263
([viewer-opts x] (v/vl viewer-opts x)))
264264

@@ -283,7 +283,7 @@
283283
284284
* `:nextjournal.clerk/width`: set the width to `:full`, `:wide`, `:prose`
285285
* `:nextjournal.clerk/viewers`: a seq of viewers to use for presentation of this value and its children
286-
* `:nextjournal.clerk/opts`: a map argument that will be passed to the viewers `:render-fn`"
286+
* `:nextjournal.clerk/render-opts`: a map argument that will be passed as a secong arg to the viewers `:render-fn`"
287287
([xs] (v/table xs))
288288
([viewer-opts xs] (v/table viewer-opts xs)))
289289

@@ -296,7 +296,7 @@
296296
297297
* `:nextjournal.clerk/width`: set the width to `:full`, `:wide`, `:prose`
298298
* `:nextjournal.clerk/viewers`: a seq of viewers to use for presentation of this value and its children
299-
* `:nextjournal.clerk/opts`: a map argument that will be passed to the viewers `:render-fn`"
299+
* `:nextjournal.clerk/render-opts`: a map argument that will be passed as a secong arg to the viewers `:render-fn`"
300300
[& xs] (apply v/row xs))
301301

302302
(defn col
@@ -308,7 +308,7 @@
308308
309309
* `:nextjournal.clerk/width`: set the width to `:full`, `:wide`, `:prose`
310310
* `:nextjournal.clerk/viewers`: a seq of viewers to use for presentation of this value and its children
311-
* `:nextjournal.clerk/opts`: a map argument that will be passed to the viewers `:render-fn`"
311+
* `:nextjournal.clerk/render-opts`: a map argument that will be passed as a secong arg to the viewers `:render-fn`"
312312
[& xs] (apply v/col xs))
313313

314314
(defn tex
@@ -318,7 +318,7 @@
318318
319319
* `:nextjournal.clerk/width`: set the width to `:full`, `:wide`, `:prose`
320320
* `:nextjournal.clerk/viewers`: a seq of viewers to use for presentation of this value and its children
321-
* `:nextjournal.clerk/opts`: a map argument that will be passed to the viewers `:render-fn`"
321+
* `:nextjournal.clerk/render-opts`: a map argument that will be passed as a secong arg to the viewers `:render-fn`"
322322
([x] (v/tex x))
323323
([viewer-opts x] (v/tex viewer-opts x)))
324324

@@ -355,7 +355,7 @@
355355
356356
* `:nextjournal.clerk/width`: set the width to `:full`, `:wide`, `:prose`
357357
* `:nextjournal.clerk/viewers`: a seq of viewers to use for presentation of this value and its children
358-
* `:nextjournal.clerk/opts`: a map argument that will be passed to the viewers `:render-fn`"
358+
* `:nextjournal.clerk/render-opts`: a map argument that will be passed as a secong arg to the viewers `:render-fn`"
359359
([code-string-or-form] (v/code code-string-or-form))
360360
([viewer-opts code-string-or-form] (v/code viewer-opts code-string-or-form)))
361361

src/nextjournal/clerk/parser.cljc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
#{:nextjournal.clerk/auto-expand-results?
5353
:nextjournal.clerk/budget
5454
:nextjournal.clerk/css-class
55-
:nextjournal.clerk/opts
55+
:nextjournal.clerk/render-opts
5656
:nextjournal.clerk/page-size
5757
:nextjournal.clerk/render-evaluator
5858
:nextjournal.clerk/visibility

src/nextjournal/clerk/render.cljs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@
325325
(cond-> viewer-css-class
326326
(string? viewer-css-class) vector)
327327
["viewer"
328-
(when (get-in x [:nextjournal/opts :fragment-item?]) "fragment-item")
328+
(when (get-in x [:nextjournal/render-opts :fragment-item?]) "fragment-item")
329329
(when viewer-name (name viewer-name))
330330
(when inner-viewer-name (name inner-viewer-name))
331331
(case (or (viewer/width x)
@@ -392,8 +392,8 @@
392392

393393
(defn inspect-children [opts]
394394
(map (fn [x] (cond-> [inspect-presented opts x]
395-
(get-in x [:nextjournal/opts :id])
396-
(with-meta {:key (str (get-in x [:nextjournal/opts :id]) "@" @!eval-counter)})))))
395+
(get-in x [:nextjournal/render-opts :id])
396+
(with-meta {:key (str (get-in x [:nextjournal/render-opts :id]) "@" @!eval-counter)})))))
397397

398398
(def expand-style
399399
["cursor-pointer"
@@ -617,7 +617,7 @@
617617
;; each view function must be called in its own 'functional component' so that it gets its own hook state.
618618
^{:key (str (:hash viewer) "@" (peek (:path opts)))}
619619
[(:render-fn viewer) value (merge opts
620-
(:nextjournal/opts x)
620+
(:nextjournal/render-opts x)
621621
{:viewer viewer :path path})]))))
622622

623623
(defn inspect [value]

src/nextjournal/clerk/tap.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
(-> wrapped-value
5454
v/mark-preserve-keys
5555
(merge (v/->opts (v/ensure-wrapped (::val value)))) ;; preserve opts like ::clerk/width and ::clerk/css-class
56-
(assoc-in [:nextjournal/opts :id] (::key value)) ;; assign custom react key
56+
(assoc-in [:nextjournal/render-opts :id] (::key value)) ;; assign custom react key
5757
(update-in [:nextjournal/value ::tapped-at] inst->local-time-str)))})
5858

5959

src/nextjournal/clerk/viewer.cljc

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@
163163

164164
(def viewer-opts-normalization
165165
"Normalizes ns for viewer opts keywords `:nextjournal.clerk/x` => `:nextjournal/x`"
166-
(into {}
166+
(into {:nextjournal/opts :nextjournal/render-opts}
167167
(map (juxt #(keyword "nextjournal.clerk" (name %))
168168
#(keyword "nextjournal" (name %))))
169169
(conj parser/block-settings :viewer :viewers)))
@@ -179,8 +179,8 @@
179179
{:width width :css-class css-class}))))
180180
opts)
181181

182-
(defn normalize-viewer-opts [opts]
183-
(throw-when-viewer-opts-invalid (set/rename-keys opts viewer-opts-normalization)))
182+
(defn normalize-viewer-opts [viewer-opts]
183+
(throw-when-viewer-opts-invalid (set/rename-keys viewer-opts viewer-opts-normalization)))
184184

185185
(defn normalize-viewer [viewer]
186186
(cond (keyword? viewer) viewer
@@ -330,12 +330,12 @@
330330
(with-viewer (keyword "nextjournal.markdown" (name type)) wrapped-value)))
331331

332332
(defn into-markup [markup]
333-
(fn [{:as wrapped-value :nextjournal/keys [viewers opts]}]
333+
(fn [{:as wrapped-value :nextjournal/keys [viewers render-opts]}]
334334
(-> (with-viewer {:name `markdown-node-viewer :render-fn 'identity} wrapped-value)
335335
mark-presented
336336
(update :nextjournal/value
337337
(fn [{:as node :keys [text content] ::keys [doc]}]
338-
(into (cond-> markup (fn? markup) (apply [(merge opts node)]))
338+
(into (cond-> markup (fn? markup) (apply [(merge render-opts node)]))
339339
(cond text [text]
340340
content (mapv #(-> (ensure-wrapped-with-viewers viewers (assoc % ::doc doc))
341341
(with-md-viewer)
@@ -507,7 +507,7 @@
507507
opts-from-block
508508
(ensure-wrapped-with-viewers (or viewers (get-viewers *ns*)) value))
509509
presented-result (-> (present to-present)
510-
(update :nextjournal/opts
510+
(update :nextjournal/render-opts
511511
(fn [{:as opts existing-id :id}]
512512
(cond-> opts
513513
auto-expand-results? (assoc :auto-expand-results? auto-expand-results?)
@@ -574,7 +574,7 @@
574574

575575
(defn md-image->viewer [doc block-id idx {:keys [attrs]}]
576576
(with-viewer `html-viewer
577-
#?(:clj {:nextjournal/opts {:id (processed-block-id block-id [idx])}
577+
#?(:clj {:nextjournal/render-opts {:id (processed-block-id block-id [idx])}
578578
:nextjournal/width (try (image-width (read-image (:src attrs)))
579579
(catch Throwable _ :prose))})
580580
[:div.flex.flex-col.items-center.not-prose.mb-4
@@ -598,7 +598,7 @@
598598
(mapcat (fn [fragment]
599599
(if (= :image (:type (first fragment)))
600600
(map #(md-image->viewer doc id (swap! !idx inc) %) fragment)
601-
[(with-viewer `markdown-viewer {:nextjournal/opts {:id (processed-block-id id [(swap! !idx inc)])}}
601+
[(with-viewer `markdown-viewer {:nextjournal/render-opts {:id (processed-block-id id [(swap! !idx inc)])}}
602602
(process-sidenotes {:type :doc
603603
:content (vec fragment)
604604
::doc doc} doc))]))
@@ -610,8 +610,8 @@
610610
(cond-> []
611611
code?
612612
(conj (with-viewer (if fold? `folded-code-block-viewer `code-block-viewer)
613-
{:nextjournal/opts (assoc (select-keys cell [:loc])
614-
:id (processed-block-id (str id "-code")))}
613+
{:nextjournal/render-opts (assoc (select-keys cell [:loc])
614+
:id (processed-block-id (str id "-code")))}
615615
(dissoc cell :result)))
616616
(or result? eval?)
617617
(conj (cond-> (ensure-wrapped (-> cell (assoc ::doc doc) (set/rename-keys {:result ::result})))
@@ -719,7 +719,7 @@
719719
:transform-fn (update-val #(with-viewer `html-viewer
720720
[:div.code-viewer.code-listing
721721
(with-viewer `code-viewer
722-
{:nextjournal/opts {:language (:language % "clojure")}}
722+
{:nextjournal/render-opts {:language (:language % "clojure")}}
723723
(str/trim-newline (md.transform/->text %)))]))}
724724

725725
;; marks
@@ -886,7 +886,7 @@
886886
{:name `mathjax-viewer :render-fn 'nextjournal.clerk.render/render-mathjax :transform-fn mark-presented})
887887

888888
(defn ->opts [wrapped-value]
889-
(select-keys wrapped-value [:nextjournal/budget :nextjournal/css-class :nextjournal/width :nextjournal/opts
889+
(select-keys wrapped-value [:nextjournal/budget :nextjournal/css-class :nextjournal/width :nextjournal/render-opts
890890
:nextjournal/render-evaluator
891891
:!budget :store!-wrapped-value :present-elision-fn :path :offset]))
892892

@@ -935,7 +935,7 @@
935935
{:name `code-viewer
936936
:render-fn 'nextjournal.clerk.render/render-code
937937
:transform-fn (comp mark-presented
938-
#(update-in % [:nextjournal/opts :language] (fn [lang] (or lang "clojure")))
938+
#(update-in % [:nextjournal/render-opts :language] (fn [lang] (or lang "clojure")))
939939
(update-val (fn [v] (if (string? v) v (str/trim (with-out-str (pprint/pprint v)))))))})
940940

941941
(def reagent-viewer
@@ -995,11 +995,11 @@
995995
(-> wrapped-value
996996
(assoc :nextjournal/viewer `table-markup-viewer)
997997
(update :nextjournal/width #(or % :wide))
998-
(update :nextjournal/opts merge {:num-cols (count (or head (first rows)))
999-
:number-col? (into #{}
1000-
(comp (map-indexed vector)
1001-
(keep #(when (number? (second %)) (first %))))
1002-
(not-empty (first rows)))})
998+
(update :nextjournal/render-opts merge {:num-cols (count (or head (first rows)))
999+
:number-col? (into #{}
1000+
(comp (map-indexed vector)
1001+
(keep #(when (number? (second %)) (first %))))
1002+
(not-empty (first rows)))})
10031003
(assoc :nextjournal/value (cond->> []
10041004
(seq rows) (cons (with-viewer `table-body-viewer (merge (-> applied-viewer
10051005
(select-keys [:page-size])

test/nextjournal/clerk/viewer_test.clj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,14 @@
182182
(testing "opts are not propagated to children during presentation"
183183
(let [count-opts (fn [o]
184184
(let [c (atom 0)]
185-
(w/postwalk (fn [f] (when (= :nextjournal/opts f) (swap! c inc)) f) o)
185+
(w/postwalk (fn [f] (when (= :nextjournal/render-opts f) (swap! c inc)) f) o)
186186
@c))]
187-
(let [presented (v/present (v/col {:nextjournal.clerk/opts {:width 150}} 1 2 3))]
188-
(is (= {:width 150} (:nextjournal/opts presented)))
187+
(let [presented (v/present (v/col {:nextjournal.clerk/render-opts {:width 150}} 1 2 3))]
188+
(is (= {:width 150} (:nextjournal/render-opts presented)))
189189
(is (= 1 (count-opts presented))))
190190

191191
(let [presented (v/present (v/table {:col1 [1 2] :col2 '[a b]}))]
192-
(is (= {:num-cols 2 :number-col? #{0}} (:nextjournal/opts presented)))
192+
(is (= {:num-cols 2 :number-col? #{0}} (:nextjournal/render-opts presented)))
193193
(is (= 1 (count-opts presented))))))
194194

195195
(testing "viewer opts are normalized"
@@ -329,7 +329,7 @@
329329
view/doc->viewer v/->value :blocks
330330
(tree-seq coll? seq)
331331
(filter (every-pred map? :nextjournal/presented))
332-
(map (comp :id :nextjournal/opts :nextjournal/presented)))]
332+
(map (comp :id :nextjournal/render-opts :nextjournal/presented)))]
333333
(is (= 5 (count ids)))
334334
(is (every? (every-pred not-empty string?) ids))
335335
(is (distinct? ids))))

0 commit comments

Comments
 (0)