Skip to content

Commit 2997b26

Browse files
zampinomk
andauthored
Support pagination for values nested inside clerk/html (#428)
Co-authored-by: Martin Kavalar <martin@nextjournal.com>
1 parent b149f2f commit 2997b26

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Changes can be:
77

88
## Unreleased
99

10-
...
10+
* 💫 Support pagination for values nested inside `clerk/html`
1111

1212
## 0.13.838 (2023-03-02)
1313

notebooks/pagination.clj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,11 @@
7373
(clerk/html [:div
7474
[:h3 "Nesting Images inside " [:span.font-mono "clerk/html"]]
7575
(clerk/image "trees.png")])
76+
77+
(clerk/html [:div
78+
[:h3 "Nesting paginated collections inside " [:span.font-mono "clerk/html"]]
79+
[:ul
80+
[:li "A strong range: "
81+
[:p [:strong {:nextjournal/value (range 30)}]]]
82+
[:li "A slanted range:"
83+
[:p [:em {:nextjournal/value (range 100)}]]]]])

src/nextjournal/clerk/viewer.cljc

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,7 @@
846846
(merge (select-keys (->opts wrapped-value) [:!budget :store!-wrapped-value :nextjournal/budget :path]))
847847
(update :path (fnil conj []) path-segment)))
848848

849+
849850
(defn transform-html [{:as wrapped-value :keys [path]}]
850851
(let [!path-idx (atom -1)]
851852
(update wrapped-value
@@ -1509,6 +1510,8 @@
15091510
(let [x (->value desc)
15101511
viewer-name (-> desc ->viewer :name)]
15111512
(cond (= viewer-name `elision-viewer) (with-meta '... x)
1513+
(= viewer-name `html-viewer) (update desc :nextjournal/value desc->values)
1514+
(and (vector? x) (= (first x) (inspect-fn))) {:nextjournal/value (desc->values (second x))}
15121515
(coll? x) (into (case viewer-name
15131516
(nextjournal.clerk.viewer/map-viewer
15141517
nextjournal.clerk.viewer/table-viewer) {}
@@ -1521,22 +1524,11 @@
15211524
#_(desc->values (present (table (mapv vector (range 30)))))
15221525
#_(desc->values (present (with-viewer `table-viewer (normalize-table-data (repeat 60 ["Adelie" "Biscoe" 50 30 200 5000 :female])))))
15231526

1524-
(defn path-to-value [path]
1525-
(conj (interleave path (repeat :nextjournal/value)) :nextjournal/value))
1526-
15271527
(defn merge-presentations [root more elision]
1528-
(update-in root
1529-
(path-to-value (:path elision))
1530-
(fn [value]
1531-
(let [{:keys [offset path]} (-> value peek :nextjournal/value)
1532-
path-from-value (conj path offset)
1533-
path-from-more (or (:replace-path elision) ;; string case, TODO find a better way to unify
1534-
(-> more :nextjournal/value first :path))]
1535-
(when (not= path-from-value path-from-more)
1536-
(throw (ex-info "paths mismatch" {:path-from-value path-from-value :path-from-more path-from-more :root root :more more :path-to-value (path-to-value (:path more)) :value value})))
1537-
(into (pop value) (:nextjournal/value more))))))
1538-
1539-
1528+
(clojure.walk/postwalk (fn [x] (if (some #(= elision (:nextjournal/value %)) (when (coll? x) x))
1529+
(into (pop x) (:nextjournal/value more))
1530+
x))
1531+
root))
15401532

15411533
(defn assign-closing-parens
15421534
([node] (assign-closing-parens '() node))

test/nextjournal/clerk/viewer_test.clj

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@
7777

7878
(testing "resolving multiple elisions"
7979
(let [value (reduce (fn [acc i] (vector i acc)) :fin (range 15 0 -1))]
80-
(is (= value (v/desc->values (-> (v/present {:nextjournal/budget 11 :nextjournal/value value}) resolve-elision resolve-elision)))))))
80+
(is (= value (v/desc->values (-> (v/present {:nextjournal/budget 11 :nextjournal/value value}) resolve-elision resolve-elision))))))
81+
82+
(testing "elision inside html"
83+
(let [value (v/html [:div [:ul [:li {:nextjournal/value (range 30)}]]])]
84+
(is (= (v/->value value) (v/->value (v/desc->values (resolve-elision (v/present value)))))))))
8185

8286
(deftest apply-viewers
8387
(testing "selects number viewer"
@@ -188,6 +192,9 @@
188192
(v/desc->values (v/present {:nextjournal/budget 3, :nextjournal/value (range 10)}))
189193
(v/desc->values (v/present {:nextjournal/budget 3, :nextjournal/value (range 10)}))))))
190194

195+
(defn path-to-value [path]
196+
(conj (interleave path (repeat :nextjournal/value)) :nextjournal/value))
197+
191198
(deftest assign-closing-parens
192199
(testing "closing parenthesis are moved to right-most children in the tree"
193200
(let [before (#'v/present* (assoc (v/ensure-wrapped-with-viewers {:a [1 '(2 3 #{4})]
@@ -196,26 +203,26 @@
196203

197204
(is (= "}"
198205
(-> before
199-
(get-in (v/path-to-value [0 1 1]))
206+
(get-in (path-to-value [0 1 1]))
200207
(get 2)
201208
v/->viewer
202209
:closing-paren)))
203210
(is (= ")"
204211
(-> before
205-
(get-in (v/path-to-value [1]))
212+
(get-in (path-to-value [1]))
206213
(get 1)
207214
v/->viewer
208215
:closing-paren)))
209216

210217
(is (= '( "}" ")" "]")
211218
(-> after
212-
(get-in (v/path-to-value [0 1 1]))
219+
(get-in (path-to-value [0 1 1]))
213220
(get 2)
214221
v/->viewer
215222
:closing-paren)))
216223
(is (= '(")" "}")
217224
(-> after
218-
(get-in (v/path-to-value [1]))
225+
(get-in (path-to-value [1]))
219226
(get 1)
220227
v/->viewer
221228
:closing-paren))))))

0 commit comments

Comments
 (0)