nextjournal / clerk

⚡️ Moldable Live Programming for Clojure

Home Page:https://clerk.vision

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Katex renderer should not throw on error

titonbarua opened this issue · comments

Right now, even a slight error on tex source will make the cell unreadable after Katex throws ParseError. Sometimes the whole page becomes blank and auto-reload with watch fails even after doing a correction. Current Katex renderer function does not handle the parser errors gracefully.

(defn render-katex [tex-string {:keys [inline?]}]
  (let [katex (hooks/use-d3-require "katex@0.16.4")]
    (if katex
      [:span {:dangerouslySetInnerHTML {:__html (.renderToString katex tex-string (j/obj :displayMode (not inline?)))}}]
      default-loading-view)))

Katex documentation ( https://katex.org/docs/options ) says that the behavior can be controlled with throwOnError option. Documentation for the option is:

throwOnError: boolean. If true (the default), KaTeX will throw a ParseError when it encounters an unsupported command or invalid LaTeX. If false, KaTeX will render unsupported commands as text, and render invalid LaTeX as its source code with hover text giving the error, in the color given by errorColor.

Having information about source of error is much better from usability perspective. As such, render-katex should be modified like this:

(defn render-katex [tex-string {:keys [inline?]}]
  (let [katex (hooks/use-d3-require "katex@0.16.4")]
    (if katex
      [:span {:dangerouslySetInnerHTML {:__html (.renderToString katex tex-string (j/obj :displayMode (not inline?) :throwOnError false))}}]
      default-loading-view)))