-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use viewer api to to enable customization of markdown nodes #122
Merged
Merged
Changes from 36 commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
6a435a9
Preparatory work
zampino 70c6305
clearer separation of concerns between fetch-fn and transform-fn
zampino 2e34b9b
Make `into-markup` a fn of node
zampino ba985e4
Sketch of inline eval
zampino 55ac94f
Wire things up: markdown notebook case
zampino 4fa8d39
Wire things up for clojure notebooks as well
zampino 73bea15
Implement all markdown nodes
zampino 53bd5ea
Show inline eval
zampino 1d593c2
Test simplification of viewer spec
zampino 44699a9
Compact markdown viewer spec
zampino cdc5022
Fix hashing tests
zampino e5de829
Fix code viewer
zampino 1eb958c
Fix rendering inline katex formulas
zampino 9b3816d
Make custom eval a bit more interesting
zampino d0f4a23
Inline eval in bold
zampino 987005b
Merge markdown viewers in existing var
zampino 33e67f8
Fix markdown image viewer
zampino 8d22dc1
Remove debug class
zampino 8ffd8ba
Adjust scope of notebook to in-text eval
zampino afaa3e1
Make custom markdown viewers survive page reload
zampino b8b56d4
Fix demo notebooks paths and nss
zampino fe1e9a6
Fix reagent structure for in-text evals not to trigger re-rendering
zampino 2bade89
Tweak in text eval
mk cc24fa2
Remove useless class from text spans
zampino e613885
Make clerk/md function consistent with markdown viewers
zampino bd6a1a6
Fix describe tests
zampino 109862f
Merge remote-tracking branch 'origin/main' into described-markdown-II
zampino f1ad495
Merge remote-tracking branch 'origin/main' into described-markdown-II
zampino 8f8dabd
Fix hashing tests
zampino 1ee6b2c
Bump markdown lib with internal links
zampino 4173f2c
Fix showing initial help doc
zampino 528c190
Fix TOC text for heading nodes with marks
zampino f911e66
How Clerk Works ToC
zampino 7ad9564
Ignore [[TOC]] placeholder instead of throwing
zampino 359761c
Fix rendering softbreaks
zampino f4cff55
Merge branch 'main' into described-markdown-II
mk a359fe0
remove viewers exclusions
zampino 284c36e
Remove redundant notebook
zampino 5bcec41
Move markdown viewers to a separate var
zampino 4abc4ad
Merge remote-tracking branch 'origin/main' into described-markdown-II
zampino 50962d1
Fix scrolling to section when clicking on ToC items
zampino e31fe9e
Make assertions more precise in describe test
zampino b256c23
Improve notebook wording
zampino 55d9290
Merge branch 'main' into described-markdown-II
mk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
;; # ✍️ Described Markdown | ||
^{:nextjournal.clerk/visibility #{:hide-ns}} | ||
(ns ^:nextjournal.clerk/no-cache described-markdown | ||
(:require [nextjournal.clerk :as clerk] | ||
[nextjournal.clerk.viewer :as v] | ||
[nextjournal.markdown :as md] | ||
[nextjournal.markdown.parser :as md.parser] | ||
[nextjournal.markdown.transform :as md.transform])) | ||
|
||
^{::clerk/viewer :hide-result} | ||
(defn parse [text] | ||
(md.parser/parse (update md.parser/empty-doc :text-tokenizers conj | ||
{:regex #"\{\{([^{]+)\}\}" | ||
:handler (fn [m] {:type :inline :text (m 1)})}) | ||
(md/tokenize text))) | ||
|
||
;; This notebook contains preparatory work to make markdown node customizable in clerk. | ||
|
||
;; Due to the recursive nature of [[v/describe]] it is sufficien to wrap a node at a time: | ||
|
||
(defn with-md-viewer [{:as node :keys [type]}] | ||
(v/wrap-value node (keyword "nextjournal.markdown" (name type)))) | ||
|
||
;; We define a generic `transform-fn` with 2 purposes: | ||
;; * produces the desired hiccup representation at each node and | ||
;; * delegates to describe the representation of the child nodes: | ||
|
||
(defn into-markup [mkup] | ||
(let [mkup-fn (if (fn? mkup) mkup (constantly mkup))] | ||
(fn [{:as node :keys [text content]}] | ||
(v/html (into (mkup-fn node) (cond text [text] content (map with-md-viewer content))))))) | ||
|
||
^{::clerk/viewer :hide-result} | ||
(def md-viewers | ||
[{:name :nextjournal.markdown/doc | ||
:transform-fn (into-markup [:div.viewer-markdown])} | ||
|
||
{:name :nextjournal.markdown/heading | ||
:transform-fn (into-markup #(vector (str "h" (:heading-level %))))} | ||
|
||
{:name :nextjournal.markdown/paragraph | ||
:transform-fn (into-markup [:p])} | ||
|
||
{:name :nextjournal.markdown/em | ||
:transform-fn (into-markup [:em])} | ||
|
||
{:name :nextjournal.markdown/strong | ||
:transform-fn (into-markup [:strong])} | ||
|
||
{:name :nextjournal.markdown/monospace | ||
:transform-fn (into-markup [:code])} | ||
|
||
{:name :nextjournal.markdown/internal-link | ||
:transform-fn (into-markup #(vector :a {:href (str "#" (:text %))}))} | ||
|
||
{:name :nextjournal.markdown/text | ||
;; TODO: find a way to drop wrapping [:span] | ||
:transform-fn (into-markup [:span.text])} | ||
|
||
{:name :nextjournal.markdown/inline | ||
;; TODO: use clerk/read+eval-cached | ||
:transform-fn (comp eval read-string :text)} | ||
|
||
{:name :nextjournal.markdown/formula | ||
:transform-fn :text | ||
:render-fn 'v/mathjax-viewer}]) | ||
|
||
(defn marine [text] (clerk/html [:span {:style {:font-weight 800 :color "#0284c7"}} (str "⚓️" text)])) | ||
|
||
(def text "# Hello | ||
|
||
This is not _really_ a **strong** formula $\\alpha$. | ||
|
||
## Section 3 | ||
|
||
with inline wiki [[link]] and inline eval {{ (marine \"Ahoi\") }}. | ||
") | ||
|
||
^{::clerk/viewers md-viewers} | ||
(with-md-viewer (parse text)) | ||
|
||
^{::clerk/visibility :hide} | ||
(comment | ||
(v/describe | ||
(v/with-viewers md-viewers | ||
(with-md-viewer (parse text)))) | ||
|
||
(reset! nextjournal.clerk.webserver/!doc nextjournal.clerk.webserver/help-doc) | ||
(reset! v/!viewers (v/get-all-viewers))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's drop this. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Customizable Markdown | ||
|
||
Playground for overriding markdown nodes | ||
|
||
```clojure | ||
^{:nextjournal.clerk/visibility #{:hide-ns}} | ||
(ns ^:nextjournal.clerk/no-cache viewers.custom-markdown | ||
(:require [nextjournal.clerk :as clerk] | ||
[nextjournal.clerk.viewer :as v])) | ||
``` | ||
|
||
```clojure | ||
(clerk/set-viewers! [{:name :nextjournal.markdown/text | ||
:transform-fn (v/into-markup [:span {:style {:color "#64748b"}}])} | ||
{:name :nextjournal.markdown/ruler | ||
:transform-fn (constantly | ||
(v/html [:div {:style {:width "100%" :height "80px" :background-position "center" :background-size "cover" | ||
:background-image "url(https://www.maxpixel.net/static/photo/1x/Ornamental-Separator-Decorative-Line-Art-Divider-4715969.png)"}}]))}]) | ||
``` | ||
|
||
## Sections | ||
|
||
with some _more_ text and a ruler. | ||
|
||
--- | ||
|
||
Clerk parses all _consecutive_ `;;`-led _clojure comment lines_ as [Markdown](https://daringfireball.net/projects/markdown). All of markdown syntax should be supported: | ||
|
||
### Lists | ||
|
||
- one **strong** hashtag like #nomarkdownforoldcountry | ||
- two ~~strikethrough~~ | ||
|
||
and bullet lists | ||
|
||
- [x] what | ||
- [ ] the | ||
|
||
### Code | ||
|
||
(assoc {:this "should get"} :clojure "syntax highlighting") | ||
|
||
--- | ||
|
||
### Tables | ||
|
||
Tables with specific alignment. | ||
|
||
| feature | | | ||
|:-------:|:---| | ||
| One |✅ | | ||
| Two |✅ | | ||
| Three |❌ | | ||
|
||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
;; # 📝 _In-Text_ Evaluation | ||
^{:nextjournal.clerk/visibility #{:hide-ns :hide} :nextjournal.clerk/toc true} | ||
(ns ^{:nextjournal.clerk/no-cache true} viewers.in-text-eval | ||
(:require [nextjournal.clerk :as clerk] | ||
[nextjournal.clerk.viewer :as v] | ||
[nextjournal.markdown.transform :as markdown.transform])) | ||
|
||
;; Being able to override markdown viewers allows we get in-text evaluation for free: | ||
|
||
^{::clerk/viewer clerk/hide-result} | ||
(defonce num★ (atom 3)) | ||
#_(reset! num★ 3) | ||
|
||
^{::clerk/visibility :show} | ||
(clerk/set-viewers! [{:name :nextjournal.markdown/monospace | ||
:transform-fn (comp eval read-string markdown.transform/->text)} | ||
{:name :nextjournal.markdown/ruler | ||
:transform-fn (constantly | ||
(v/with-viewer :html [:div.text-center (repeat @num★ "★")]))}]) | ||
;; --- | ||
^{::clerk/viewer clerk/hide-result} | ||
(defn slider [var {:keys [min max]}] | ||
(clerk/with-viewer | ||
{:fetch-fn (fn [_ x] x) | ||
:transform-fn (fn [var] {:var-name (symbol var) :value @@var}) | ||
:render-fn `(fn [{:as x :keys [var-name value]}] | ||
(v/html [:input {:type :range | ||
:min ~min :max ~max | ||
:value value | ||
:on-change #(v/clerk-eval `(reset! ~var-name (Integer/parseInt ~(.. % -target -value))))}]))} | ||
var)) | ||
|
||
;; Drag the following slider `(slider #'num★ {:min 1 :max 40})` to control the number of stars (currently **`(deref num★)`**) in our custom horizontal rules. | ||
|
||
;; --- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
;; # Markdown ✍️ | ||
(ns markdown (:require [nextjournal.clerk :as clerk])) | ||
|
||
(clerk/md "### Text can be\n * **bold**\n * *italic\n * ~~Strikethrough~~\n | ||
(clerk/md "### Text can be\n * **bold**\n * *italic*\n * ~~Strikethrough~~\n | ||
It's [Markdown](https://daringfireball.net/projects/markdown/), like you know it.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
vN7eLXAmyRrUvHLvw2ZzvSqPyTH | ||
3oPQaaq3te999AVUnqPnMeNwg2jV |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👆 these redundant deps confuse my intellij, can we exclude them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove those from the viewers so it doesn't bring both of those in.