Skip to content

Commit

Permalink
Introduce a toc-viewer to customize the table of contents (#609)
Browse files Browse the repository at this point in the history
  • Loading branch information
zampino authored Jan 11, 2024
1 parent cc5cf40 commit 464d8fb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Changes can be:

* 💫 Introduce client-side routing for static builds to make page transitions smoother by default. In addition, the option `:bundle?` for `clerk/build!` is now deprecated in favour of setting a new option `:package` to `:single-file` (the default for it being `:directory`).

* 💫 Introduce a toc viewer for allowing customization of the table of contents

* 🛠 Bump depdendencies

* `com.taoensso/nippy` to `3.4.0-beta1`
Expand Down
22 changes: 11 additions & 11 deletions notebooks/meta_toc.clj
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@
(partial parser/parse-file {:doc? true})))
paths))

(def book-viewer
(update v/notebook-viewer
:transform-fn (fn [original-transform]
(fn [wrapped-value]
(-> wrapped-value
original-transform
(assoc :nextjournal/render-opts {:expandable-toc? true})
(assoc-in [:nextjournal/value :toc]
(meta-toc (:file (v/->value wrapped-value)) notebooks)))))))
(def toc-viewer
(update v/toc-viewer
:transform-fn
(fn [original-transform]
(fn transform-fn [{:as wrapped-value doc :nextjournal/value}]
(-> wrapped-value
original-transform
(assoc :nextjournal/render-opts {:expandable-toc? true})
(assoc :nextjournal/value (meta-toc (:file doc) notebooks)))))))

(clerk/add-viewers! [book-viewer])
(clerk/add-viewers! [toc-viewer])

(comment
;; Test actual cross-doc toc
(viewer/reset-viewers! :default (viewer/add-viewers [book-viewer]))
(viewer/reset-viewers! :default (viewer/add-viewers [toc-viewer]))
(viewer/reset-viewers! :default (viewer/get-default-viewers))
(clerk/build! {:render-router :fetch-edn :paths notebooks}))
6 changes: 3 additions & 3 deletions src/nextjournal/clerk/render.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@
(catch js/Error _
(js/console.warn (str "Clerk render-notebook, invalid hash: "
(.-hash js/location))))))]
(js/requestAnimationFrame #(.scrollIntoViewIfNeeded heading)))))
_ (swap! !expanded-at merge (navbar/->toc-expanded-at toc toc-visibility))]
(js/requestAnimationFrame #(.scrollIntoViewIfNeeded heading)))))]

[:div.flex
{:ref root-ref-fn}
[:div.fixed.top-2.left-2.md:left-auto.md:right-2.z-10
[dark-mode-toggle]]
(when (and toc toc-visibility)
[navbar/view toc (assoc render-opts :set-hash? (not= :single-file package) :toc-visibility toc-visibility)])
[inspect-presented render-opts toc])
[:div.flex-auto.w-screen.scroll-container
(into
[:> (.-div motion)
Expand Down
16 changes: 15 additions & 1 deletion src/nextjournal/clerk/viewer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1235,8 +1235,21 @@
:path (str "#" (:id attrs))
:items (md-toc->navbar-items node)}) children))

(defn transform-toc [{:as wrapped-value doc :nextjournal/value}]
(let [{:keys [toc-visibility package]} doc]
(-> wrapped-value
mark-presented
(update :nextjournal/value (comp md-toc->navbar-items :toc))
(update :nextjournal/render-opts assoc
:toc-visibility toc-visibility :set-hash? (not= :single-file package)))))

(comment #?(:clj (nextjournal.clerk/recompute!)))

(def toc-viewer
{:name `toc-viewer
:transform-fn transform-toc
:render-fn 'nextjournal.clerk.render.navbar/view})

(defn process-blocks [viewers {:as doc :keys [ns]}]
(-> doc
(assoc :atom-var-name->state (atom-var-name->state doc))
Expand All @@ -1246,7 +1259,7 @@
(assoc :header (present (with-viewers viewers (with-viewer `header-viewer doc))))
#_(assoc :footer (present (footer doc)))

(update :toc md-toc->navbar-items)
(assoc :toc (present (with-viewers viewers (with-viewer `toc-viewer doc))))
(update :file str)

(select-keys [:atom-var-name->state
Expand Down Expand Up @@ -1296,6 +1309,7 @@
(def default-viewers
;; maybe make this a sorted-map
[header-viewer
toc-viewer
char-viewer
string-viewer
number-viewer
Expand Down

0 comments on commit 464d8fb

Please sign in to comment.