diff --git a/src/core/hooks/Emphasis.js b/src/core/hooks/Emphasis.js index f2a15436f..f6bc784d6 100644 --- a/src/core/hooks/Emphasis.js +++ b/src/core/hooks/Emphasis.js @@ -14,12 +14,7 @@ * limitations under the License. */ import SyntaxBase from '@/core/SyntaxBase'; -import { - compileRegExp, - DO_NOT_STARTS_AND_END_WITH_SPACES_MULTILINE_ALLOW_EMPTY, - ALLOW_WHITESPACE_MULTILINE, - UNDERSCORE_EMPHASIS_BOUNDARY, -} from '@/utils/regexp'; +import { compileRegExp, ALLOW_WHITESPACE_MULTILINE, UNDERSCORE_EMPHASIS_BOUNDARY } from '@/utils/regexp'; export default class Emphasis extends SyntaxBase { static HOOK_NAME = 'fontEmphasis'; @@ -94,19 +89,20 @@ export default class Emphasis extends SyntaxBase { */ rule({ config } = { config: undefined }) { const allowWhitespace = config ? !!config.allowWhitespace : false; - const REGEX = allowWhitespace - ? ALLOW_WHITESPACE_MULTILINE - : DO_NOT_STARTS_AND_END_WITH_SPACES_MULTILINE_ALLOW_EMPTY; + const emRegexp = (allowWhitespace, symbol) => { + const char = `[^${symbol}\\s]`; + return allowWhitespace ? ALLOW_WHITESPACE_MULTILINE : `(${char}|${char}(.*?(\n${char}.*)*)${char})`; + }; const asterisk = { - begin: '(^|[^\\\\])(\\*+)', // ?, ? - content: `(${REGEX})`, // ? + begin: '(^|[^\\\\])([*]+)', // ?, ? + content: `(${emRegexp(allowWhitespace, '*')})`, // ? end: '\\2', }; // UNDERSCORE_EMPHASIS_BORDER:允许除下划线以外的「标点符号」和空格出现,使用[^\w\S \t]或[\W\s]会有性能问题 const underscore = { begin: `(^|${UNDERSCORE_EMPHASIS_BOUNDARY})(_+)`, // ?, ? - content: `(${REGEX})`, // ? + content: `(${emRegexp(allowWhitespace, '_')})`, // ? end: `\\2(?=${UNDERSCORE_EMPHASIS_BOUNDARY}|$)`, }; diff --git a/src/utils/regexp.js b/src/utils/regexp.js index d6ebdc004..70585d9e1 100644 --- a/src/utils/regexp.js +++ b/src/utils/regexp.js @@ -37,6 +37,12 @@ export const ALLOW_WHITESPACE_MULTILINE = '(?:.*?)(?:(?:\\n.*?)*?)'; export const DO_NOT_STARTS_AND_END_WITH_SPACES = '(?:\\S|(?:\\S.*?\\S))'; export const DO_NOT_STARTS_AND_END_WITH_SPACES_MULTILINE = '(?:(?:\\S|(?:\\S[^\\n]*?\\S))(?:\\n(?:\\S|(?:\\S[^\\n]*?\\S)))*?)'; + +/** + * @deprecated + * + * 存在严重性能问题,应弃用 + */ export const DO_NOT_STARTS_AND_END_WITH_SPACES_MULTILINE_ALLOW_EMPTY = '(?:(?:\\S|(?:\\S.*?\\S))(?:[ \\t]*\\n.*?)*?)'; export const NOT_ALL_WHITE_SPACES_INLINE = '(?:[^\\n]*?\\S[^\\n]*?)';