The Problem of Subscript in Hexo with Mathjax

Problem

When writing equations with hexo, there are a lot of problems with its math engine. The specific problem that I came across is that the inline formula of

1
$\{P_sa\}$

will not render correctly.

Test

LaTeX

The formula was rendered correctly on $\LaTeX$. So I did not make it wrong in the formula.

Mathjax

Then I thought the problem might be with mathjax. However, when I tested the same formula directly written in html, it worked perfectly.

Hexo

When I checked the html page generated by hexo, I found out that the formula was rendered as:

1
<p>$\{P<em>sa\}$......<\em>

The problem is, markdown will use not only *asterisks* for emphasis, but also _underscores_. What an stupid interesting design!

Solution

In the file of node_modules/marked/lib/marked.js:

1
2
3
4
5
6
7
8
9
link: /^!?\[(inside)\]\(href\)/,
reflink: /^!?\[(inside)\]\s*\[([^\]]*)\]/,
nolink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]/,
strong: /^__([\s\S]+?)__(?!_)|^\*\*([\s\S]+?)\*\*(?!\*)/,
em: /^\b_((?:[^_]|__)+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,
code: /^(`+)([\s\S]*?[^`])\1(?!`)/,
br: /^ {2,}\n(?!\s*$)/,
del: noop,
text: /^[\s\S]+?(?=[\\<!\[_*`]| {2,}\n|$)/

Change the line

1
em: /^\b_((?:[^_]|__)+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,

to

1
em:  /^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,

This will disable the effort of trying to match _ for the regular expression engine.

Now it works.