6
$\begingroup$

Hardmath's earlier bug report made me take a closer look at the way MathJax works when editing a post from preview. Or, rather, the way it currently doesn't work.

While investigating, I also noticed a related bug: when editing a deleted question inline, MathJax also doesn't get rendered in the editor preview pane.

Both of these bugs seem to have the same cause, which I've described in my self-answer below.


Steps to reproduce:

In review:

  1. Visit any review queue that gives you the option to edit the post under review. Close votes review works fine.
  2. Click the "Edit" button. An inline Markdown editor should open up.
  3. If the post does not have any MathJax markup in it, add some. Something trivial like $1+1=2$ should work.
  4. Observe that the math is not typeset in the editor preview pane.

On a deleted question (10k+ rep only):

  1. Visit any deleted question. You can find some e.g. via the 10k tools page.
  2. Click the "edit" link below the question. Again, an inline editor should open.
  3. Proceed as in steps 3 and 4 above.

Note: Curiously, if there are multiple deleted posts (e.g. a deleted question and its answers) on the same page, and you try to edit several of them, it seems that MathJax works in all but the first editor you open. It would appear that something in the Markdown editor code does try to automatically load the MathJax editing module when needed, but does it too late.

It's possible that this bug may also appear in other situations, but these are the two that I've confirmed so far.

$\endgroup$
0

1 Answer 1

3
$\begingroup$

The problem seems to be that the MathJax editing code is not loaded in review or on deleted question pages. This, in turn, seems to happen because those pages are missing the following inline JavaScript code that is included on normal question pages:

StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
        StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\\\(","\\\\)"]]);
        });
    });
}, "mathjax-editing");

Indeed, manually running that code in the browser JS console before opening the editor will cause MathJax preview to start working even on the pages where it otherwise fails.

I suspect this absence of this code is due to some mistaken attempt at optimization in the SE codebase: some developer probably decided to only include that code on pages that have a Markdown editor present by default, assuming that it wasn't otherwise necessary. Unfortunately, that assumption turns out to be wrong: editors can be dynamically created even on pages where they don't initially appear, and the code quoted above is exactly what's supposed to trigger the loading of the MathJax editing code in that situation.

Anyway, simply including the code above on all pages should fix the bug — on pages that don't have an editor present, it simply does nothing until and unless an editor is actually loaded.

(Other ways to fix this would be possible, too; for example, the editor code itself could be made to detect the presence of MathJax and load the necessary extra code in that case, which would save a handful of bytes per page load if the editor isn't needed.)

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .