BookStackApp / BookStack

A platform to create documentation/wiki content built with PHP & Laravel
https://www.bookstackapp.com/
MIT License
15.31k stars 1.91k forks source link

[Bug Report]: curly braces with recurring underscores conflict with MathJax #3048

Closed onionyst closed 3 years ago

onionyst commented 3 years ago

Describe the Bug

When using Markdown with MathJax, the following pattern cannot display normally:

$$
{v}_{a_{i,j}}
$$

image

Steps to Reproduce

  1. Set Page Editor as Markdown.
  2. Set Custom HTML Head Content with the following code:
<script type="text/javascript" id="MathJax-script" async
  src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js">
</script>
  1. Create a new note with the following content and save.
$$
{v}_{a_{i,j}}
$$
  1. Note equation not renderring correctly.

Expected Behaviour

Should render the equation.

A minimal demo works properly (you can check it here or locally):

<!DOCTYPE html>
<html>
<head>
  <script type="text/javascript" id="MathJax-script" async
    src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
</head>
<body>
  <p>
  $${v}_{a_{i,j}}$$
  </p>
</body>
</html>

image

So I suppose BookStack has something with this pattern (maybe a higher priority on italics) and conflicts with MathJax.

Screenshots or Additional Context

No response

Exact BookStack Version

v21.10.3

PHP Version

No response

Hosting Environment

Latest Docker image provided by linuxserver.

ssddanbrown commented 3 years ago

Hi @onionyst,

I wouldn't consider this to be a bug in BookStack, as reported, since this is related to a MathJax rendering/library which is not built in or officially supported.

This is occurring since the underscores in your MathJax content are assumed to be markdown italic syntax, so the actually resulting HTML is actually:

<p>$$
{v}<em>{a</em>{i,j}}
$$</p>

This therefore breaks the MathJax library parsing. You can get around this by wrapping the MathJax content in HTML tags to get the content to be parsed as HTML instead of MathJax. For example, pasting the below in the Markdown editor should get the result you desire:

<p>$$
{v}_{a_{i,j}}
$$<p>

Note, You may come across other oddities but having MathJax run globally across all page content like it looks to do by default.

onionyst commented 3 years ago

Thanks, @ssddanbrown.

I understand that it is caused by the rendering sequence. Would it be possible to have pre-markdown-rendering hooks to have custom modifications?

Another question would be, is there any method to have custom header (e.g. MathJax) available also on the Markdown preview pane on the right?

ssddanbrown commented 3 years ago

I understand that it is caused by the rendering sequence. Would it be possible to have pre-markdown-rendering hooks to have custom modifications?

We do provide editor hooks (BookStack Editor Events) although they might be rudundant soon as we change editor and I don't think this would help here. Markdown is rendered to HTML server-side so you won't be able to get in-front of the markdown parsing for page views. You could maybe tweak the back-end render using the logical theme system.

We current don't inject custom header scripts into the preview pane, as to avoid any issues, we only inject styles. You could use the linked editor hooks above to inject scripts in yourself if desired.