Skip to content

Commit 9022f3a

Browse files
authored
Merge pull request #588 from Tencent/bugfix/emphasis-performance-issue
fix(emphasis): fix poor performance when matching across lines
2 parents f75e15e + 54b6972 commit 9022f3a

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/core/hooks/Emphasis.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@
1414
* limitations under the License.
1515
*/
1616
import SyntaxBase from '@/core/SyntaxBase';
17-
import {
18-
compileRegExp,
19-
DO_NOT_STARTS_AND_END_WITH_SPACES_MULTILINE_ALLOW_EMPTY,
20-
ALLOW_WHITESPACE_MULTILINE,
21-
UNDERSCORE_EMPHASIS_BOUNDARY,
22-
} from '@/utils/regexp';
17+
import { compileRegExp, ALLOW_WHITESPACE_MULTILINE, UNDERSCORE_EMPHASIS_BOUNDARY } from '@/utils/regexp';
2318

2419
export default class Emphasis extends SyntaxBase {
2520
static HOOK_NAME = 'fontEmphasis';
@@ -94,19 +89,20 @@ export default class Emphasis extends SyntaxBase {
9489
*/
9590
rule({ config } = { config: undefined }) {
9691
const allowWhitespace = config ? !!config.allowWhitespace : false;
97-
const REGEX = allowWhitespace
98-
? ALLOW_WHITESPACE_MULTILINE
99-
: DO_NOT_STARTS_AND_END_WITH_SPACES_MULTILINE_ALLOW_EMPTY;
92+
const emRegexp = (allowWhitespace, symbol) => {
93+
const char = `[^${symbol}\\s]`;
94+
return allowWhitespace ? ALLOW_WHITESPACE_MULTILINE : `(${char}|${char}(.*?(\n${char}.*)*)${char})`;
95+
};
10096
const asterisk = {
101-
begin: '(^|[^\\\\])(\\*+)', // ?<leading>, ?<symbol>
102-
content: `(${REGEX})`, // ?<text>
97+
begin: '(^|[^\\\\])([*]+)', // ?<leading>, ?<symbol>
98+
content: `(${emRegexp(allowWhitespace, '*')})`, // ?<text>
10399
end: '\\2',
104100
};
105101

106102
// UNDERSCORE_EMPHASIS_BORDER:允许除下划线以外的「标点符号」和空格出现,使用[^\w\S \t]或[\W\s]会有性能问题
107103
const underscore = {
108104
begin: `(^|${UNDERSCORE_EMPHASIS_BOUNDARY})(_+)`, // ?<leading>, ?<symbol>
109-
content: `(${REGEX})`, // ?<text>
105+
content: `(${emRegexp(allowWhitespace, '_')})`, // ?<text>
110106
end: `\\2(?=${UNDERSCORE_EMPHASIS_BOUNDARY}|$)`,
111107
};
112108

src/utils/regexp.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ export const ALLOW_WHITESPACE_MULTILINE = '(?:.*?)(?:(?:\\n.*?)*?)';
3737
export const DO_NOT_STARTS_AND_END_WITH_SPACES = '(?:\\S|(?:\\S.*?\\S))';
3838
export const DO_NOT_STARTS_AND_END_WITH_SPACES_MULTILINE =
3939
'(?:(?:\\S|(?:\\S[^\\n]*?\\S))(?:\\n(?:\\S|(?:\\S[^\\n]*?\\S)))*?)';
40+
41+
/**
42+
* @deprecated
43+
*
44+
* 存在严重性能问题,应弃用
45+
*/
4046
export const DO_NOT_STARTS_AND_END_WITH_SPACES_MULTILINE_ALLOW_EMPTY = '(?:(?:\\S|(?:\\S.*?\\S))(?:[ \\t]*\\n.*?)*?)';
4147

4248
export const NOT_ALL_WHITE_SPACES_INLINE = '(?:[^\\n]*?\\S[^\\n]*?)';

0 commit comments

Comments
 (0)