Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: wrong parsing for math #929

Closed
dreamerblue opened this issue Aug 27, 2017 · 4 comments
Closed

Bug: wrong parsing for math #929

dreamerblue opened this issue Aug 27, 2017 · 4 comments
Labels
L2 - annoying Similar to L1 - broken but there is a known workaround available for the issue question

Comments

@dreamerblue
Copy link

dreamerblue commented Aug 27, 2017

marked version: 0.3.6

My markdown code:
$B_{p_{1}}+B_{p_{2}}$

But when I try to parse into HTML using
$('#content').html(marked(mdString));

It returns wrong HTML code. Unexpected <em></em> occurred:
<p>$B<em>{p</em>{1}}+B<em>{p</em>{2}}$</p>

@joshbruce joshbruce added L2 - annoying Similar to L1 - broken but there is a known workaround available for the issue question labels Dec 1, 2017
@joshbruce
Copy link
Member

See also #936

@Feder1co5oave
Copy link
Contributor

Feder1co5oave commented Dec 27, 2017

You shouldn't put LaTeX math formula in markdown, since they are not supported and are instead interpreted as markdown.

If you want to keep the latex code as is, wrap it inside a `code` element:

`$B_{p_{1}}+B_{p_{2}}$`

This is not a bug

@joshbruce joshbruce added this to the 0.5.0 - Architecture and extensibility milestone Dec 27, 2017
@joshbruce
Copy link
Member

Adding this to the 0.5.0 miestone as math is something that comes up quite a bit, but does not seem to specifically addressed in the specifications we are focusin on (see #956). Closing.

@Hzzone
Copy link

Hzzone commented Oct 21, 2018

It's rude but solved my problem in a way...

function replace(content, regx) {
    content = content.replace(regx,
        function(expression) {
            var max_formula_length = 200;
            if (expression.length > max_formula_length || has_chinese(expression)) return result;
            expression = expression.replace(/\\\\/g, '\\\\\\\\');
            expression = expression.replace(/\\ /g, '\\\\ ');
            expression = expression.replace(/\\%/g, '\\\\%');
            expression = expression.replace(/\\{/g, '\\\\{');
            expression = expression.replace(/\\}/g, '\\\\}');
            expression = expression.replace(/\\#/g, '\\\\#');
            expression = expression.replace(/\\~/g, '\\\\~');
            expression = expression.replace(/\\_/g, '\\\\_');
            expression = expression.replace(/\\&/g, '\\\\&');
            expression = expression.replace(/\\\$/g, '\\\\$');
            expression = expression.replace(/\\\^/g, '\\\\^');
            expression = expression.replace(/\\\|/g, '\\\\|');
            expression = expression.replace(/\_/g, '\\_');
            return expression;
        });
    return content;
}

var data = replace(raw_data, /\$\$([\s\S]*?)\$\$/gm);
data = replace(data, /\$([\s\S]*?)\$/gm);
data = marked(data);

For example:

var raw_data = '$$\\mathcal{L}_C=\\frac{1}{2} \\sum_{i=1}^{m} \\| x_i-c_{y_i} \\|_2^2$$';
var data = replace(raw_data, /\$\$([\s\S]*?)\$\$/gm);
data = replace(data, /\$([\s\S]*?)\$/gm);
data = marked(data);
console.log(data);

output:

<p>$$\mathcal{L}_C=\frac{1}{2} \sum_{i=1}^{m} \| x_i-c_{y_i} \|_2^2$$</p>

It will be effective even for multi-line math formula.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
L2 - annoying Similar to L1 - broken but there is a known workaround available for the issue question
Projects
None yet
Development

No branches or pull requests

4 participants