Skip to content

Commit

Permalink
feat(emphasis): add chinese punctuations as word's boundary
Browse files Browse the repository at this point in the history
  • Loading branch information
lyngai authored and sunsonliu committed Jun 8, 2022
1 parent 5a74b0c commit 6d8a769
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/core/hooks/Emphasis.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
compileRegExp,
DO_NOT_STARTS_AND_END_WITH_SPACES_MULTILINE_ALLOW_EMPTY,
ALLOW_WHITESPACE_MULTILINE,
UNDERSCORE_EMPHASIS_BORDER,
UNDERSCORE_EMPHASIS_BOUNDARY,
} from '@/utils/regexp';

export default class Emphasis extends SyntaxBase {
Expand Down Expand Up @@ -97,9 +97,9 @@ export default class Emphasis extends SyntaxBase {

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

asterisk.reg = compileRegExp(asterisk, 'g');
Expand Down
13 changes: 11 additions & 2 deletions src/utils/regexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,25 @@ export const NOT_ALL_WHITE_SPACES_INLINE = '(?:[^\\n]*?\\S[^\\n]*?)';

export const NORMAL_INDENT = '[ ]{0, 3}|\\t';
export const NO_BACKSLASH_BEFORE_CAPTURE = '[^\\\\]';

// https://spec.commonmark.org/0.29/#ascii-punctuation-character
// !, ", #, $, %, &, ', (, ), *, +, ,, -, ., / (U+0021–2F),
// :, ;, <, =, >, ?, @ (U+003A–0040),
// [, \, ], ^, _, ` (U+005B–0060),
// {, |, }, or ~ (U+007B–007E).
export const PUNCTUATION = '[\\u0021-\\u002F\\u003a-\\u0040\\u005b-\\u0060\\u007b-\\u007e]';

// extra punctuations
export const CHINESE_PUNCTUATION = '[!“”¥‘’(),。—:;《》?【】「」·~|]';

// 下划线强调语法允许的边界符号
export const UNDERSCORE_EMPHASIS_BORDER =
'[\\u0021-\\u002F\\u003a-\\u0040\\u005b\\u005d\\u005e\\u0060\\u007b-\\u007e \\t\\n]';
export const UNDERSCORE_EMPHASIS_BOUNDARY =
'[' +
'\\u0021-\\u002F\\u003a-\\u0040\\u005b\\u005d\\u005e\\u0060\\u007b-\\u007e' + // punctuations defined in commonmark
' ' +
'\\t\\n' +
'!“”¥‘’(),。—:;《》?【】「」·~|' + // chinese punctuations
']';

// https://html.spec.whatwg.org/multipage/input.html#e-mail-state-(type%3Demail)
export const EMAIL_INLINE = new RegExp(
Expand Down

0 comments on commit 6d8a769

Please sign in to comment.