Skip to content

Commit

Permalink
Fix page-size option throwing when set on strings (#585)
Browse files Browse the repository at this point in the history
Fix `:nextjournal.clerk/page-size` option throwing when set on string values.

Fixes #584.

---------

Co-authored-by: Martin Kavalar <[email protected]>
  • Loading branch information
zampino and mk authored Dec 7, 2023
1 parent 9547880 commit babbba5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Changes can be:

* 🐜 Make edn transmission resilient to symbols and keywords containing multiple slashes like `foo/bar/baz`. Those can be read by `read-string` but not in ClojureScript which is based on `tools.reader`.

* 🐞 Fix `:nextjournal.clerk/page-size` option throwing when set on string values, fixes [#584][https://github.com/nextjournal/clerk/issues/584]

* 🐞 Fix caching behaviour of `clerk/image` and support overriding image-viewer by name

* 🐞 Fix `unquote` in experimental cljs Clerk editor, fixes [#576](https://github.com/nextjournal/clerk/issues/576) @sritchie
Expand Down
13 changes: 5 additions & 8 deletions src/nextjournal/clerk/viewer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1511,15 +1511,13 @@

(defn get-elision [wrapped-value]
(let [{:as fetch-opts :keys [n]} (->fetch-opts wrapped-value)]
(merge fetch-opts (bounded-count-opts n (->value wrapped-value)))))
(when (number? n)
(merge fetch-opts (bounded-count-opts n (->value wrapped-value))))))

#_(get-elision (present (range)))
#_(get-elision (present "abc"))
#_(get-elision (present (str/join (repeat 1000 "abc"))))

(defn get-fetch-opts-n [wrapped-value]
(-> wrapped-value ->fetch-opts :n))

(defn present+paginate-children [{:as wrapped-value :nextjournal/keys [budget viewers preserve-keys?] :keys [!budget]}]
(let [{:as fetch-opts :keys [offset n]} (->fetch-opts wrapped-value)
xs (->value wrapped-value)
Expand All @@ -1541,10 +1539,9 @@
(conj (let [fetch-opts (assoc elision :offset new-offset)]
(make-elision viewers fetch-opts))))))

(defn present+paginate-string [{:as wrapped-value :nextjournal/keys [viewers viewer value]}]
(let [{:as elision :keys [n total path offset]} (and (:page-size viewer)
(get-elision wrapped-value))]
(if (and n (< n total))
(defn present+paginate-string [{:as wrapped-value :nextjournal/keys [viewers value]}]
(let [{:as elision :keys [n total path offset]} (get-elision wrapped-value)]
(if (and elision n (< n total))
(let [new-offset (min (+ (or offset 0) n) total)]
(cond-> [(subs value (or offset 0) new-offset)]
(pos? (- total new-offset)) (conj (let [fetch-opts (-> elision
Expand Down
5 changes: 5 additions & 0 deletions test/nextjournal/clerk/eval_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@
:nextjournal/hash string?}}]
(eval+extract-doc-blocks "(range)"))))

(testing "Skipping pagination for strings"
(is (= "012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849"
(-> (eval+extract-doc-blocks "^{:nextjournal.clerk/page-size nil} (apply str (range 50))")
second :nextjournal/value :nextjournal/presented :nextjournal/value))))

(testing "assigns folded visibility"
(is (match? [{:nextjournal/viewer {:name `viewer/folded-code-block-viewer}
:nextjournal/value "{:some :map}"}
Expand Down

0 comments on commit babbba5

Please sign in to comment.