From 4dc2a7ee9bbed0da99d270e360c45614a35a543a Mon Sep 17 00:00:00 2001 From: jiawei686 <892001108@qq.com> Date: Thu, 17 Mar 2022 15:26:32 +0800 Subject: [PATCH] fix: replace lookbehind in math regex --- src/core/hooks/InlineMath.js | 22 ++++++++++++++++------ src/core/hooks/MathBlock.js | 23 +++++++++++++++-------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/core/hooks/InlineMath.js b/src/core/hooks/InlineMath.js index c9fd0bd0..1f422c6d 100644 --- a/src/core/hooks/InlineMath.js +++ b/src/core/hooks/InlineMath.js @@ -17,6 +17,9 @@ import ParagraphBase from '@/core/ParagraphBase'; import { escapeFormulaPunctuations, LoadMathModule } from '@/utils/mathjax'; import { getHTML } from '@/utils/dom'; import { isBrowser } from '@/utils/env'; +import { isLookbehindSupported } from '@/utils/regexp'; +import { replaceLookbehind } from '@/utils/lookbehind-replace'; + /** * 行内公式的语法 * 虽然叫做行内公式,Cherry依然将其视为“段落级语法”,因为其具备排他性并且需要优先渲染 @@ -34,7 +37,7 @@ export default class InlineMath extends ParagraphBase { this.engine = isBrowser() ? config.engine ?? 'MathJax' : 'node'; } - toHtml(wholeMatch, m1) { + toHtml(wholeMatch, leadingChar, m1) { if (!m1) { return wholeMatch; } @@ -47,18 +50,18 @@ export default class InlineMath extends ParagraphBase { const html = this.katex.renderToString(m1, { throwOnError: false, }); - const result = `${html}`; + const result = `${leadingChar}${html}`; return this.pushCache(result, ParagraphBase.IN_PARAGRAPH_CACHE_KEY_PREFIX + sign); } if (this.MathJax?.tex2svg) { // MathJax渲染 const svg = getHTML(this.MathJax.tex2svg(m1, { em: 12, ex: 6, display: false }), true); - const result = `${svg}`; + const result = `${leadingChar}${svg}`; return this.pushCache(result, ParagraphBase.IN_PARAGRAPH_CACHE_KEY_PREFIX + sign); } // 既无MathJax又无katex时,原样输出 - const result = `$${escapeFormulaPunctuations(m1)}$`; return this.pushCache(result, ParagraphBase.IN_PARAGRAPH_CACHE_KEY_PREFIX + sign); } @@ -67,7 +70,10 @@ export default class InlineMath extends ParagraphBase { if (!this.test(str)) { return str; } - return str.replace(this.RULE.reg, this.toHtml.bind(this)); + if (isLookbehindSupported()) { + return str.replace(this.RULE.reg, this.toHtml.bind(this)); + } + return replaceLookbehind(str, this.RULE.reg, this.toHtml.bind(this), true, 1); } makeHtml(str) { @@ -75,7 +81,11 @@ export default class InlineMath extends ParagraphBase { } rule() { - const ret = { begin: '(?${html}`; - return this.getCacheWithSpace(this.pushCache(result, sign, lines), wholeMatch); + return leadingChar + this.getCacheWithSpace(this.pushCache(result, sign, lines), wholeMatch); } if (this.MathJax?.tex2svg) { // MathJax渲染 const svg = getHTML(this.MathJax.tex2svg(content), true); const result = `