Skip to content

Commit

Permalink
Support pagination for values nested inside clerk/html (nextjournal…
Browse files Browse the repository at this point in the history
…#428)


Co-authored-by: Martin Kavalar <martin@nextjournal.com>
  • Loading branch information
zampino and mk authored Mar 3, 2023
1 parent b149f2f commit 2997b26
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Changes can be:

## Unreleased

...
* 💫 Support pagination for values nested inside `clerk/html`

## 0.13.838 (2023-03-02)

Expand Down
8 changes: 8 additions & 0 deletions notebooks/pagination.clj
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,11 @@
(clerk/html [:div
[:h3 "Nesting Images inside " [:span.font-mono "clerk/html"]]
(clerk/image "trees.png")])

(clerk/html [:div
[:h3 "Nesting paginated collections inside " [:span.font-mono "clerk/html"]]
[:ul
[:li "A strong range: "
[:p [:strong {:nextjournal/value (range 30)}]]]
[:li "A slanted range:"
[:p [:em {:nextjournal/value (range 100)}]]]]])
22 changes: 7 additions & 15 deletions src/nextjournal/clerk/viewer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,7 @@
(merge (select-keys (->opts wrapped-value) [:!budget :store!-wrapped-value :nextjournal/budget :path]))
(update :path (fnil conj []) path-segment)))


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

(defn path-to-value [path]
(conj (interleave path (repeat :nextjournal/value)) :nextjournal/value))

(defn merge-presentations [root more elision]
(update-in root
(path-to-value (:path elision))
(fn [value]
(let [{:keys [offset path]} (-> value peek :nextjournal/value)
path-from-value (conj path offset)
path-from-more (or (:replace-path elision) ;; string case, TODO find a better way to unify
(-> more :nextjournal/value first :path))]
(when (not= path-from-value path-from-more)
(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})))
(into (pop value) (:nextjournal/value more))))))


(clojure.walk/postwalk (fn [x] (if (some #(= elision (:nextjournal/value %)) (when (coll? x) x))
(into (pop x) (:nextjournal/value more))
x))
root))

(defn assign-closing-parens
([node] (assign-closing-parens '() node))
Expand Down
17 changes: 12 additions & 5 deletions test/nextjournal/clerk/viewer_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@

(testing "resolving multiple elisions"
(let [value (reduce (fn [acc i] (vector i acc)) :fin (range 15 0 -1))]
(is (= value (v/desc->values (-> (v/present {:nextjournal/budget 11 :nextjournal/value value}) resolve-elision resolve-elision)))))))
(is (= value (v/desc->values (-> (v/present {:nextjournal/budget 11 :nextjournal/value value}) resolve-elision resolve-elision))))))

(testing "elision inside html"
(let [value (v/html [:div [:ul [:li {:nextjournal/value (range 30)}]]])]
(is (= (v/->value value) (v/->value (v/desc->values (resolve-elision (v/present value)))))))))

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

(defn path-to-value [path]
(conj (interleave path (repeat :nextjournal/value)) :nextjournal/value))

(deftest assign-closing-parens
(testing "closing parenthesis are moved to right-most children in the tree"
(let [before (#'v/present* (assoc (v/ensure-wrapped-with-viewers {:a [1 '(2 3 #{4})]
Expand All @@ -196,26 +203,26 @@

(is (= "}"
(-> before
(get-in (v/path-to-value [0 1 1]))
(get-in (path-to-value [0 1 1]))
(get 2)
v/->viewer
:closing-paren)))
(is (= ")"
(-> before
(get-in (v/path-to-value [1]))
(get-in (path-to-value [1]))
(get 1)
v/->viewer
:closing-paren)))

(is (= '( "}" ")" "]")
(-> after
(get-in (v/path-to-value [0 1 1]))
(get-in (path-to-value [0 1 1]))
(get 2)
v/->viewer
:closing-paren)))
(is (= '(")" "}")
(-> after
(get-in (v/path-to-value [1]))
(get-in (path-to-value [1]))
(get 1)
v/->viewer
:closing-paren))))))
Expand Down

0 comments on commit 2997b26

Please sign in to comment.