<%
var enable = theme.post.math.enable;

if (enable) {
  if (is_post()) {
    enable = !theme.post.math.specific || (theme.post.math.specific && page.math);
  } else if (is_page()) {
    enable = page.math;
  } else {
    enable = false;
  }
}
%>

<% if (enable) { %>
  <% var engine = theme.post.math.engine; %>

  <% if(engine === 'mathjax') { %>
    <!-- MathJax -->
    <script>
      MathJax = {
        tex: {
          inlineMath: [['$', '$'], ['\\(', '\\)']]
        },
        options: {
          renderActions: {
            findScript: [10, doc => {
              document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
                const display = !!node.type.match(/; *mode=display/);
                const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
                const text = document.createTextNode('');
                node.parentNode.replaceChild(text, node);
                math.start = { node: text, delim: '', n: 0 };
                math.end = { node: text, delim: '', n: 0 };
                doc.math.push(math);
              });
            }, '', false],
            insertedScript: [200, () => {
              document.querySelectorAll('mjx-container').forEach(node => {
                let target = node.parentNode;
                if (target.nodeName.toLowerCase() === 'li') {
                  target.parentNode.classList.add('has-jax');
                }
              });
            }, '', false]
          }
        }
      };
    </script>

    <%- js_ex(theme.static_prefix.mathjax, "tex-svg.js", 'async') %>

  <% } else if (engine === 'katex') { %>
    <!-- KaTeX -->
    <%- css_ex(theme.static_prefix.katex, "katex.min.css") %>
  <% } %>
<% } %>
