Skip to content
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

Add meta-viewer for metadata-based viewer assignment #502

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sritchie
Copy link
Collaborator

@sritchie sritchie commented May 31, 2023

@mk and I came up with this approach working on emmy-viewers together.

Another viewer I'd like to suggest for inclusion is a "reagent-viewer"... or maybe a render-fn-viewer. It acts on quoted reagent forms.

The existing reagent-viewer errors for reagent forms like

(clerk/with-viewer nextjournal.clerk.viewer/reagent-viewer
  '(reagent.core/with-let [!click (reagent.core/atom 0)]
     [:div
      "The atom " [:code "!click"] " has value: "
      @!click ". "
      [:input
       {:type :button
        :value "Click me!"
        :on-click
        (fn [e]
          (js/console.log e)
          (.stopPropagation e)
          (swap! !click inc))}]]))
image

Here is the viewer:

(defn- strip-meta [form]
  (postwalk (fn [x]
              (if (meta x)
                (vary-meta x dissoc ::clerk/viewer)
                x))
            form))

(def reagent-viewer
  {:name `reagent-viewer
   :transform-fn
   (clerk/update-val
    (fn [form]
      (clerk/with-viewer
        {:render-fn
         (list 'fn [] (strip-meta form))}
        nil)))})

And here is it working with that reagent form above:

2023-05-31 16 32 37

@@ -868,6 +868,17 @@
(def mathjax-viewer
{:name `mathjax-viewer :render-fn 'nextjournal.clerk.render/render-mathjax :transform-fn mark-presented})

(def meta-viewer
Copy link
Collaborator Author

@sritchie sritchie May 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am happy to add examples to the book, @mk .

Here's one:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant