0

I am using rails v6.0.3.2 and ruby 2.7.1. I am putting text in ActionText rich textboxes. My mathjax is not rendered when the page first loads. It is rendered if I refresh the page. I need to either (1) make sure the mathjax renders when the page first loads, or (2) force a page reload. Obviously the former is better.

I read in the mathjax docs here that I can accomplish the former with the following javascript (that I put in my application.js)(but it did not work):

(function () {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.src  = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-MML-AM_CHTML";
  document.getElementsByTagName("head")[0].appendChild(script);
})();

In my application.html.haml view I load the mathjax CDN along with some code to enable single dollar sign notation:

:plain
      <!--               MathJax                   -->
      <!-- This implements single $ as a delimiter -->
      <!-- This MUST come before the CDN reference below -->
      <script type="text/x-mathjax-config">
        MathJax.Hub.Config({
        tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']],
        processEscapes: true}
        });
      </script>

      <!-- Mathjax: latest CDN version -->
      <script type="text/javascript" async
        src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-MML-AM_CHTML">
      </script>

How can I enable "dynamic" rendering of mathjax, or, alternatively, force a page refresh when the page first loads?

Randall Blake
  • 684
  • 2
  • 6
  • 15

1 Answers1

0

My New Answer

I think I found a better answer to my problem. Following this SO suggestion, I invalidate the rails cache after an update (using an after_action callback). It now seems to be reprocessing the mathjax without destroying what is already there.

My Previous Answer

Reading the turbolinks documentation here, I learned that I could insert the following meta tag in my application.html.haml file to force a page reload:

<head>
  ...
  <meta name="turbolinks-visit-control" content="reload">
</head>

Actually this is the haml version: %meta{:content => "reload", :name => "turbolinks-visit-control"}/

The problem is that if I edit a page that has pre-existing mathjax on it, the delimiters are stripped and I have to reenter them as part of my edit (which makes it a royal pain). This did not happen before I added the meta tag.

I can tolerate this, but if anyone can suggest a better way, I am all ears.

Randall Blake
  • 684
  • 2
  • 6
  • 15