mirror of https://github.com/vector-im/riot-web
use html parser rather than regexes
parent
cc713aff72
commit
10b732131a
|
@ -30,6 +30,7 @@ import url from 'url';
|
||||||
import katex from 'katex';
|
import katex from 'katex';
|
||||||
import { AllHtmlEntities } from 'html-entities';
|
import { AllHtmlEntities } from 'html-entities';
|
||||||
import SettingsStore from './settings/SettingsStore';
|
import SettingsStore from './settings/SettingsStore';
|
||||||
|
import cheerio from 'cheerio';
|
||||||
|
|
||||||
import {MatrixClientPeg} from './MatrixClientPeg';
|
import {MatrixClientPeg} from './MatrixClientPeg';
|
||||||
import {tryTransformPermalinkToLocalHref} from "./utils/permalinks/Permalinks";
|
import {tryTransformPermalinkToLocalHref} from "./utils/permalinks/Permalinks";
|
||||||
|
@ -414,23 +415,20 @@ export function bodyToHtml(content: IContent, highlights: string[], opts: IOpts
|
||||||
if (isHtmlMessage) {
|
if (isHtmlMessage) {
|
||||||
isDisplayedWithHtml = true;
|
isDisplayedWithHtml = true;
|
||||||
safeBody = sanitizeHtml(formattedBody, sanitizeParams);
|
safeBody = sanitizeHtml(formattedBody, sanitizeParams);
|
||||||
if (SettingsStore.getValue("feature_latex_maths")) {
|
const phtml = cheerio.load(safeBody,
|
||||||
const mathDelimiters = [
|
{ _useHtmlParser2: true, decodeEntities: false })
|
||||||
{ pattern: "<div data-mx-maths=\"([^\"]*)\">(.|\\s)*?</div>", display: true },
|
|
||||||
{ pattern: "<span data-mx-maths=\"([^\"]*)\">(.|\\s)*?</span>", display: false },
|
|
||||||
];
|
|
||||||
|
|
||||||
mathDelimiters.forEach(function(d) {
|
if (SettingsStore.getValue("feature_latex_maths")) {
|
||||||
safeBody = safeBody.replace(RegExp(d.pattern, "gm"), function(m, p1) {
|
phtml('div, span[data-mx-maths!=""]').replaceWith(function(i, e) {
|
||||||
return katex.renderToString(
|
return katex.renderToString(
|
||||||
AllHtmlEntities.decode(p1),
|
AllHtmlEntities.decode(phtml(e).attr('data-mx-maths')),
|
||||||
{
|
{
|
||||||
throwOnError: false,
|
throwOnError: false,
|
||||||
displayMode: d.display,
|
displayMode: e.name == 'div',
|
||||||
output: "htmlAndMathml",
|
output: "htmlAndMathml",
|
||||||
})
|
});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
safeBody = phtml.html();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
|
Loading…
Reference in New Issue