Skip to content

Commit

Permalink
修改hook,完善了语法解析,增加通过测试用例
Browse files Browse the repository at this point in the history
对于多处hook进行了修改,并且给出了注释
  • Loading branch information
Cross1111 committed Oct 11, 2024
1 parent 5c5d59c commit 5aa9988
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/core/hooks/Emphasis.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ export default class Emphasis extends SyntaxBase {
return allowWhitespace ? ALLOW_WHITESPACE_MULTILINE : `(${char}|${char}(.*?(\n${char}.*)*)${char})`;
};
const asterisk = {
begin: '(^|[^\\\\])([*]+)', // ?<leading>, ?<symbol>
begin: '(^|[^\\\\])(\\*+)(?!\\*)', // ?<leading>, ?<symbol>
//防止连续星号:改后的正则表达式不允许捕获的星号序列后紧跟另一个星号

Check failure on line 98 in src/core/hooks/Emphasis.js

View workflow job for this annotation

GitHub Actions / build

Expected space or tab after '//' in comment
content: `(${emRegexp(allowWhitespace, '*')})`, // ?<text>
end: '\\2',
};
Expand Down
9 changes: 8 additions & 1 deletion src/core/hooks/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,14 @@ export default class Header extends ParagraphBase {

// atx header
const atx = {
begin: '(?:^|\\n)(\\n*)(?:\\h*(#{1,6}))', // (?<lines>\\n*), (?<level>#{1,6})
begin: '(?:^|\\n)(\\n*)(?:\\h{0,}(#{1,6})(?:\\s+|$))', // (?<lines>\\n*), (?<level>#{1,6})
// begin: 匹配标题的开始部分
// (?:^|\\n) : 非捕获组,匹配字符串的开始位置(^)或换行符(\\n)
// (\\n*) : 捕获组,匹配任意数量的换行符,可能用于后续处理(例如,计算缩进级别)
// (?:\\h{0,}(#{1,6})(?:\\s+|$)) : 非捕获组,用于匹配标题的级别和可选的后续空格或行尾
// \\h{0,} : 匹配任意数量的水平空白字符(空格、制表符等),但此处由于后面紧跟#{1,6},所以实际上这个部分可以省略,因为#前的空格在Markdown中通常被忽略
// (#{1,6}) : 捕获组,匹配1到6个#字符,表示标题的级别(1级到6级)
// (?:\\s+|$) : 非捕获组,要求要么有一个或多个空格
content: '(.+?)', // '(?<text>.+?)'
end: '(?=$|\\n)',
};
Expand Down
9 changes: 8 additions & 1 deletion src/core/hooks/InlineCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ export default class InlineCode extends ParagraphBase {
}

rule() {
const ret = { begin: '(`+)[ ]*', end: '[ ]*\\1', content: '(.+?(?:\\n.+?)*?)' };
const ret = { begin: '(`+)[ ]*', end: '[ ]?\\1', content: '([^`\\n]*(?:\\n[^`\\n]*)*)(?=\\1|$)' };
// 匹配零个或一个空格,后面紧跟与begin中捕获的反引号数量相同的反引号
// 简化content部分,使用非贪婪匹配来捕获任意数量的非反引号字符(包括换行符),
// 直到遇到与begin中捕获的反引号数量相同的反引号为止。
// 上面的content正则表达式解释:
// - `[^`\\n]*`:匹配任意数量的非反引号和非换行符字符。
// - `(?:\\n[^`\\n]*)*`:非捕获组,匹配任意数量的换行符后跟任意数量的非反引号和非换行符字符的重复。
// - `(?=\\1|$)`:正向前瞻断言,确保接下来是与begin中捕获的反引号数量相同的反引号或字符串结束。
ret.reg = compileRegExp(ret, 'g');
return ret;
}
Expand Down
1 change: 1 addition & 0 deletions src/core/hooks/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export default class Link extends SyntaxBase {
begin: isLookbehindSupported() ? '((?<!\\\\))' : '(^|[^\\\\])',
content: [
'\\[([^\\n]*?)\\]', // ?<text>
//同样匹配方括号内的非换行字符,但使用了*?进行非贪婪匹配
'[ \\t]*', // any spaces
`${
'(?:' +
Expand Down

0 comments on commit 5aa9988

Please sign in to comment.