When adding a cloze ending in inline mathjax, the mathjax is deleted and the cloze isn’t created correctly.
Example steps to reproduce:
Start creating a new cloze card (Example HTML: abc <anki-mathjax>def</anki-mathjax> ghi)
Use mouse or shift+arrows to mark abc def (but nothing more) in the example (e.g. by pressing crtl+shift+right arrow once from the very left)
Press crtl+shift+c to create a cloze
The result is }}{{c1::abc ghi
The entire mathjax has been deleted and the cloze is inverted
(Side note: This error has been partially reported before, but was not addressed at the time, see 2022 and this long write-up by me from ten months ago.)
There are a number of similar issues in the issue tracker already. The person who wrote this code has moved on, and nobody has had the time or inclination to dig into the issues. PRs would be very welcome!
I wish I could help, but to be it seems like the issue is in the JS side of Anki and I’m completely unfamiliar with JS, sorry .
This specific issue seems to be related to that small space after the mathjax field, which might be related to the closing tag? In any case, even with correct creation (by including the space) reverting with CRTL+Z does strange things.
Some side notes about implementation, that are possibly wrong because I’ve lot looked at much JS (well, TS/Svelte here) or HTML before, but that might be helpful:
In the case of creating clozes inside HTML tags there are in my opinion multiple cases to consider, which is true for not just mathjax, but also underlines etc… These improve the usability of clozes in my opinion and also apply to the inverse conditions where the direction and tag is inverted and might serve as a source of ideas for implementation:
Opening tag right before the first marked character (= closing tag right after): Place {{ in front of the opening tag.
Opening tag inside the cloze area, closing tag outside: Create a closing tag before }} and an opening tag after }}.
Both tags outside: Currently creates a closing tag before {{ and an opening tag after }}, but seems to work fine for mathjax. Curiously also changes spaces to non-breaking ones on both sides if the marked text is surrounded by them, but only on the closing side if the spaces are supposed to be part of the cloze (in that case the cloze also does not include the spaces, but the HTML tags are placed outside of the spaces, odd). In this case the {{ should probably be created at the edge of the marked area, but then surrounded by a new closing and opening tag.