mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
fix: pedantic **foo:** and **"word"** emphasis parsing (#3999)
This commit is contained in:
+52
-1
@@ -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(),
|
||||
|
||||
@@ -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>"A"</strong>and<strong>"B"</strong>aa</p>
|
||||
|
||||
<p>aa<strong>'A'</strong>and<strong>'B'</strong>aa</p>
|
||||
|
||||
<p>We can compare <strong>"direction"</strong> and <strong>"distance"</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>"a</strong>b</p>
|
||||
|
||||
<p><strong>":</strong>x</p>
|
||||
|
||||
<p>a<strong>"b</strong>c</p>
|
||||
|
||||
<p><strong>"A"</strong></p>
|
||||
|
||||
<p><strong>""</strong></p>
|
||||
|
||||
<p>x<strong>"y:"</strong>z</p>
|
||||
|
||||
<p><strong>foo:</strong></p>
|
||||
|
||||
<p><strong>:</strong></p>
|
||||
|
||||
<hr>
|
||||
|
||||
<p>aa<strong>(A)</strong>and<strong>"B"</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>"A"</strong></p>
|
||||
|
||||
<p><strong>""</strong></p>
|
||||
|
||||
<p><strong>"a</strong>b</p>
|
||||
|
||||
<p><strong>":</strong>x</p>
|
||||
|
||||
<p><em>"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__"A"__bb</p>
|
||||
|
||||
<p>aa_bb_cc</p>
|
||||
|
||||
<p>a_"b_c</p>
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user