From 5aa998846e4810679808f03e81ee964747e50e6f Mon Sep 17 00:00:00 2001 From: MasterYHY <95060647+Cross1111@users.noreply.github.com> Date: Fri, 11 Oct 2024 08:39:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9hook=EF=BC=8C=E5=AE=8C?= =?UTF-8?q?=E5=96=84=E4=BA=86=E8=AF=AD=E6=B3=95=E8=A7=A3=E6=9E=90=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=80=9A=E8=BF=87=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 对于多处hook进行了修改,并且给出了注释 --- src/core/hooks/Emphasis.js | 3 ++- src/core/hooks/Header.js | 9 ++++++++- src/core/hooks/InlineCode.js | 9 ++++++++- src/core/hooks/Link.js | 1 + 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/core/hooks/Emphasis.js b/src/core/hooks/Emphasis.js index f6bc784d6..c15863796 100644 --- a/src/core/hooks/Emphasis.js +++ b/src/core/hooks/Emphasis.js @@ -94,7 +94,8 @@ export default class Emphasis extends SyntaxBase { return allowWhitespace ? ALLOW_WHITESPACE_MULTILINE : `(${char}|${char}(.*?(\n${char}.*)*)${char})`; }; const asterisk = { - begin: '(^|[^\\\\])([*]+)', // ?, ? + begin: '(^|[^\\\\])(\\*+)(?!\\*)', // ?, ? + //防止连续星号:改后的正则表达式不允许捕获的星号序列后紧跟另一个星号 content: `(${emRegexp(allowWhitespace, '*')})`, // ? end: '\\2', }; diff --git a/src/core/hooks/Header.js b/src/core/hooks/Header.js index 408d8fd10..f48e35997 100644 --- a/src/core/hooks/Header.js +++ b/src/core/hooks/Header.js @@ -222,7 +222,14 @@ export default class Header extends ParagraphBase { // atx header const atx = { - begin: '(?:^|\\n)(\\n*)(?:\\h*(#{1,6}))', // (?\\n*), (?#{1,6}) + begin: '(?:^|\\n)(\\n*)(?:\\h{0,}(#{1,6})(?:\\s+|$))', // (?\\n*), (?#{1,6}) + // begin: 匹配标题的开始部分 + // (?:^|\\n) : 非捕获组,匹配字符串的开始位置(^)或换行符(\\n) + // (\\n*) : 捕获组,匹配任意数量的换行符,可能用于后续处理(例如,计算缩进级别) + // (?:\\h{0,}(#{1,6})(?:\\s+|$)) : 非捕获组,用于匹配标题的级别和可选的后续空格或行尾 + // \\h{0,} : 匹配任意数量的水平空白字符(空格、制表符等),但此处由于后面紧跟#{1,6},所以实际上这个部分可以省略,因为#前的空格在Markdown中通常被忽略 + // (#{1,6}) : 捕获组,匹配1到6个#字符,表示标题的级别(1级到6级) + // (?:\\s+|$) : 非捕获组,要求要么有一个或多个空格 content: '(.+?)', // '(?.+?)' end: '(?=$|\\n)', }; diff --git a/src/core/hooks/InlineCode.js b/src/core/hooks/InlineCode.js index b35d7b6f6..5a88bb20b 100644 --- a/src/core/hooks/InlineCode.js +++ b/src/core/hooks/InlineCode.js @@ -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; } diff --git a/src/core/hooks/Link.js b/src/core/hooks/Link.js index afbaf8511..2d002c360 100644 --- a/src/core/hooks/Link.js +++ b/src/core/hooks/Link.js @@ -129,6 +129,7 @@ export default class Link extends SyntaxBase { begin: isLookbehindSupported() ? '((? + //同样匹配方括号内的非换行字符,但使用了*?进行非贪婪匹配 '[ \\t]*', // any spaces `${ '(?:' +