rikhuijzer / PlutoStaticHTML.jl

Convert Pluto notebooks to HTML in automated workflows
https://PlutoStaticHTML.huijzer.xyz
MIT License
83 stars 7 forks source link

Escape dollar #192

Closed rikhuijzer closed 5 months ago

rikhuijzer commented 5 months ago

This makes PlutoStaticHTML escape dollar symbols when they were escaped in Pluto too. This avoids having multiple dollar symbols in one line to be interpreted as inline math. Doing these escapes are supported by at least MathJax (https://docs.mathjax.org/en/latest/input/tex/delimiters.html), but I'm quite sure KaTeX too.

rikhuijzer commented 5 months ago

Note that outside Documenter, this now requires the escaped dollar symbols to be converted back to normal dollar symbols inside HTML after running KaTeX or Mathjax. This is what I have in my blog now:

<script src="/libs/katex/katex.min.js"></script>
<script src="/libs/katex/auto-render.min.js"></script>
<script>
  const options = {
    delimiters: [
      {left: "$$", right: "$$", display: true},
      {left: "$", right: "$", display: false},
      {left: "``", right: "``", display: false},
      {left: "\\begin{equation}", right: "\\end{equation}", display: true},
      {left: "\\begin{align}", right: "\\end{align}", display: true},
      {left: "\\begin{alignat}", right: "\\end{alignat}", display: true},
      {left: "\\begin{gather}", right: "\\end{gather}", display: true},
      {left: "\\(", right: "\\)", display: false},
      {left: "\\[", right: "\\]", display: true}
    ]
  };
  renderMathInElement(document.body, options);

  function replaceEscapedDollars() {
    const allElements = document.body.getElementsByTagName('p');

    for (let i = 0; i < allElements.length; i++) {
      const element = allElements[i];
      // Check if element has text content
      if (element.textContent) {
        element.textContent = element.textContent.replace(/\\\$|\$/g, '$');
      }
    }
  }
  replaceEscapedDollars();
</script>

I guess that Documenter.jl does this automatically so that's why it's only required with output that is parsed by Franklin.

rikhuijzer commented 5 months ago

It would probably be more reliable to convert dollars to double backticks:

``x = 1``

and then let KaTeX process that. For now the current approach is good enough.