fix: pedantic **foo:** and **"word"** emphasis parsing (#3999)

This commit is contained in:
Waiter
2026-08-03 22:29:40 -06:00
committed by GitHub
parent 12a84a8d3c
commit 823093f3d4
3 changed files with 270 additions and 1 deletions
+52 -1
View File
@@ -292,6 +292,12 @@ const _notPunctuationOrSpace = /[^\s\p{P}\p{S}]/u;
const punctuation = edit(/^((?![*_])punctSpace)/, 'u')
.replace(/punctSpace/g, _punctuationOrSpace).getRegex();
/**
* Opening punct/quotes excluded from pedantic LDelim nextChar so
* `aa**"A"**bb` / `aa**(A)**bb` can start strong (Pi + Ps + ASCII straight quotes).
*/
const _openQuote = /[\p{Pi}\p{Ps}"']/u;
// GFM allows ~ inside strong and em for strikethrough
const _punctuationGfmStrongEm = /(?!~)[\p{P}\p{S}]/u;
const _punctuationOrSpaceGfmStrongEm = /(?!~)[\s\p{P}\p{S}]/u;
@@ -315,6 +321,14 @@ const emStrongLDelimGfm = edit(emStrongLDelimCore, 'u')
.replace(/punct/g, _punctuationGfmStrongEm)
.getRegex();
// Pedantic: do not treat opening punct/quotes as left-delim punct (e.g. **( / **").
const emStrongLDelimPedanticCore = /^(?:\*+(?:((?!\*)(?!openQuote)punct)|([^\s*]))?)|^_+(?:((?!_)(?!openQuote)punct)|([^\s_]))?/;
const emStrongLDelimPedantic = edit(emStrongLDelimPedanticCore, 'u')
.replace(/openQuote/g, _openQuote)
.replace(/punct/g, _punctuation)
.getRegex();
const emStrongRDelimAstCore =
'^[^_*]*?__[^_*]*?\\*[^_*]*?(?=__)' // Skip orphan inside strong
+ '|[^*]+(?=[^*])' // Consume to delim
@@ -337,7 +351,24 @@ const emStrongRDelimAstGfm = edit(emStrongRDelimAstCore, 'gu')
.replace(/punct/g, _punctuationGfmStrongEm)
.getRegex();
// (6) Not allowed for _
// Pedantic: rule 3 requires whitespace before left delim; rule 6 allows punct before close (e.g. `:**bar`).
const emStrongRDelimAstPedanticCore =
'^[^_*]*?__[^_*]*?\\*[^_*]*?(?=__)' // Skip orphan inside strong
+ '|[^*]+(?=[^*])' // Consume to delim
+ '|(?!\\*)punct(\\*+)(?=[\\s]|$)' // (1) #*** can only be a Right Delimiter
+ '|notPunctSpace(\\*+)(?!\\*)(?=punctSpace|$)' // (2) a***#, a*** can only be a Right Delimiter
+ '|(?!\\*)[\\s](\\*+)(?=notPunctSpace)' // (3) ***a can only be Left Delimiter (whitespace required; not #***a)
+ '|[\\s](\\*+)(?!\\*)(?=punct)' // (4) ***# can only be Left Delimiter
+ '|(?!\\*)punct(\\*+)(?!\\*)(?=punct)' // (5) #***# can be either Left or Right Delimiter
+ '|(?:(?!\\*)punct|notPunctSpace)(\\*+)(?!\\*)(?=notPunctSpace)'; // (6) a***a, #***a, and :** (e.g. **foo:**bar)
const emStrongRDelimAstPedantic = edit(emStrongRDelimAstPedanticCore, 'gu')
.replace(/notPunctSpace/g, _notPunctuationOrSpace)
.replace(/punctSpace/g, _punctuationOrSpace)
.replace(/punct/g, _punctuation)
.getRegex();
// Normal: no rule 6 for _ (a___a cannot close as emphasis).
const emStrongRDelimUnd = edit(
'^[^_*]*?\\*\\*[^_*]*?_[^_*]*?(?=\\*\\*)' // Skip orphan inside strong
+ '|[^_]+(?=[^_])' // Consume to delim
@@ -351,6 +382,23 @@ const emStrongRDelimUnd = edit(
.replace(/punct/g, _punctuation)
.getRegex();
// Pedantic: same rule 3/6 adjustments as emStrongRDelimAstPedantic (e.g. `_foo:_bar`).
const emStrongRDelimUndPedanticCore =
'^[^_*]*?\\*\\*[^_*]*?_[^_*]*?(?=\\*\\*)' // Skip orphan inside strong
+ '|[^_]+(?=[^_])' // Consume to delim
+ '|(?!_)punct(_+)(?=[\\s]|$)' // (1) #___ can only be a Right Delimiter
+ '|notPunctSpace(_+)(?!_)(?=punctSpace|$)' // (2) a___#, a___ can only be a Right Delimiter
+ '|(?!_)[\\s](_+)(?=notPunctSpace)' // (3) ___a can only be Left Delimiter (whitespace required; not #___a)
+ '|[\\s](_+)(?!_)(?=punct)' // (4) ___# can only be Left Delimiter
+ '|(?!_)punct(_+)(?!_)(?=punct)' // (5) #___# can be either Left or Right Delimiter
+ '|(?:(?!_)punct|notPunctSpace)(_+)(?!_)(?=notPunctSpace)'; // (6) a___a, #___a, and :__ (e.g. _foo:_bar)
const emStrongRDelimUndPedantic = edit(emStrongRDelimUndPedanticCore, 'gu')
.replace(/notPunctSpace/g, _notPunctuationOrSpace)
.replace(/punctSpace/g, _punctuationOrSpace)
.replace(/punct/g, _punctuation)
.getRegex();
// Tilde left delimiter for strikethrough (similar to emStrongLDelim for asterisk)
const delLDelim = edit(/^~~?(?:((?!~)punct)|[^\s~])/, 'u')
.replace(/punct/g, _punctuation)
@@ -453,6 +501,9 @@ type InlineKeys = keyof typeof inlineNormal;
const inlinePedantic: Record<InlineKeys, RegExp> = {
...inlineNormal,
emStrongLDelim: emStrongLDelimPedantic,
emStrongRDelimAst: emStrongRDelimAstPedantic,
emStrongRDelimUnd: emStrongRDelimUndPedantic,
link: edit(/^!?\[(label)\]\((.*?)\)/)
.replace('label', _inlineLabel)
.getRegex(),
+107
View File
@@ -0,0 +1,107 @@
<p><strong>foo:</strong>bar</p>
<p><strong>foo</strong>bar</p>
<p><em>foo:</em>bar</p>
<p><em>foo:</em>bar</p>
<p><strong>foo:</strong>bar</p>
<p>a<strong>b:</strong>c</p>
<p><strong>foo.</strong>bar</p>
<p><strong>foo,</strong>bar</p>
<p>aa<strong>bb</strong>cc</p>
<p>aa<strong>&quot;A&quot;</strong>and<strong>&quot;B&quot;</strong>aa</p>
<p>aa<strong>&#39;A&#39;</strong>and<strong>&#39;B&#39;</strong>aa</p>
<p>We can compare <strong>&quot;direction&quot;</strong> and <strong>&quot;distance&quot;</strong> in two ways</p>
<p>aa<strong>“A”</strong>and<strong>“B”</strong>aa</p>
<p>aa<strong>‘A’</strong>and<strong>‘B’</strong>aa</p>
<p>aa<strong>«A»</strong>bb</p>
<p>aa<strong>‹A›</strong>bb</p>
<p><strong>(A)</strong></p>
<p>aa<strong>(A)</strong>bb</p>
<p>aa<strong>[A]</strong>bb</p>
<p>aa<strong>{A}</strong>bb</p>
<p>aa<strong>「A」</strong>bb</p>
<p>aa<strong>(A)</strong>bb</p>
<p>aa<strong>【A】</strong>bb</p>
<p>aa**”A”**bb</p>
<p>aa**’A’**bb</p>
<p><strong>&quot;a</strong>b</p>
<p><strong>&quot;:</strong>x</p>
<p>a<strong>&quot;b</strong>c</p>
<p><strong>&quot;A&quot;</strong></p>
<p><strong>&quot;&quot;</strong></p>
<p>x<strong>&quot;y:&quot;</strong>z</p>
<p><strong>foo:</strong></p>
<p><strong>:</strong></p>
<hr>
<p>aa<strong>(A)</strong>and<strong>&quot;B&quot;</strong>cc</p>
<p><strong>foo</strong>bar</p>
<p><strong>foo.</strong>bar</p>
<p><strong>foo,</strong>bar</p>
<p><strong>(A)</strong></p>
<p><strong>«A»</strong></p>
<p><strong>「A」</strong></p>
<p><strong>&quot;A&quot;</strong></p>
<p><strong>&quot;&quot;</strong></p>
<p><strong>&quot;a</strong>b</p>
<p><strong>&quot;:</strong>x</p>
<p><em>&quot;a</em>b</p>
<p><strong>foo:</strong></p>
<p><strong>:</strong></p>
<p>aa__bb__cc</p>
<p>a__b:__c</p>
<p>aa__(A)__bb</p>
<p>aa__&quot;A&quot;__bb</p>
<p>aa_bb_cc</p>
<p>a_&quot;b_c</p>
+111
View File
@@ -0,0 +1,111 @@
---
pedantic: true
---
**foo:**bar
**foo**bar
*foo:*bar
_foo:_bar
__foo:__bar
a**b:**c
**foo.**bar
**foo,**bar
aa**bb**cc
aa**"A"**and**"B"**aa
aa**'A'**and**'B'**aa
We can compare **"direction"** and **"distance"** in two ways
aa**“A”**and**“B”**aa
aa**‘A’**and**‘B’**aa
aa**«A»**bb
aa**‹A›**bb
**(A)**
aa**(A)**bb
aa**[A]**bb
aa**{A}**bb
aa**「A」**bb
aa**(A)**bb
aa**【A】**bb
aa**”A”**bb
aa**’A’**bb
**"a**b
**":**x
a**"b**c
**"A"**
**""**
x**"y:"**z
**foo:**
**:**
****
aa**(A)**and**"B"**cc
__foo__bar
__foo.__bar
__foo,__bar
__(A)__
__«A»__
__「A」__
__"A"__
__""__
__"a__b
__":__x
_"a_b
__foo:__
__:__
aa__bb__cc
a__b:__c
aa__(A)__bb
aa__"A"__bb
aa_bb_cc
a_"b_c