mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
fix: fix strikethrough flanking rules (#3882)
This commit is contained in:
+1
-1
@@ -408,7 +408,7 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
|
||||
}
|
||||
|
||||
// del (gfm)
|
||||
if (token = this.tokenizer.del(src)) {
|
||||
if (token = this.tokenizer.del(src, maskedSrc, prevChar)) {
|
||||
src = src.substring(token.raw.length);
|
||||
tokens.push(token);
|
||||
continue;
|
||||
|
||||
+50
-9
@@ -818,15 +818,56 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
|
||||
}
|
||||
}
|
||||
|
||||
del(src: string): Tokens.Del | undefined {
|
||||
const cap = this.rules.inline.del.exec(src);
|
||||
if (cap) {
|
||||
return {
|
||||
type: 'del',
|
||||
raw: cap[0],
|
||||
text: cap[2],
|
||||
tokens: this.lexer.inlineTokens(cap[2]),
|
||||
};
|
||||
del(src: string, maskedSrc: string, prevChar = ''): Tokens.Del | undefined {
|
||||
let match = this.rules.inline.delLDelim.exec(src);
|
||||
if (!match) return;
|
||||
|
||||
const nextChar = match[1] || '';
|
||||
|
||||
if (!nextChar || !prevChar || this.rules.inline.punctuation.exec(prevChar)) {
|
||||
// unicode Regex counts emoji as 1 char; spread into array for proper count
|
||||
const lLength = [...match[0]].length - 1;
|
||||
let rDelim, rLength, delimTotal = lLength;
|
||||
|
||||
const endReg = this.rules.inline.delRDelim;
|
||||
endReg.lastIndex = 0;
|
||||
|
||||
// Clip maskedSrc to same section of string as src
|
||||
maskedSrc = maskedSrc.slice(-1 * src.length + lLength);
|
||||
|
||||
while ((match = endReg.exec(maskedSrc)) != null) {
|
||||
rDelim = match[1] || match[2] || match[3] || match[4] || match[5] || match[6];
|
||||
|
||||
if (!rDelim) continue;
|
||||
|
||||
rLength = [...rDelim].length;
|
||||
|
||||
if (rLength !== lLength) continue;
|
||||
|
||||
if (match[3] || match[4]) { // found another Left Delim
|
||||
delimTotal += rLength;
|
||||
continue;
|
||||
}
|
||||
|
||||
delimTotal -= rLength;
|
||||
|
||||
if (delimTotal > 0) continue; // Haven't found enough closing delimiters
|
||||
|
||||
// Remove extra characters
|
||||
rLength = Math.min(rLength, rLength + delimTotal);
|
||||
// char length can be >1 for unicode characters
|
||||
const lastCharLength = [...match[0]][0].length;
|
||||
const raw = src.slice(0, lLength + match.index + lastCharLength + rLength);
|
||||
|
||||
// Create del token - only single ~ or double ~~ supported
|
||||
const text = raw.slice(lLength, -lLength);
|
||||
return {
|
||||
type: 'del',
|
||||
raw,
|
||||
text,
|
||||
tokens: this.lexer.inlineTokens(text),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -278,6 +278,11 @@ const _punctuationGfmStrongEm = /(?!~)[\p{P}\p{S}]/u;
|
||||
const _punctuationOrSpaceGfmStrongEm = /(?!~)[\s\p{P}\p{S}]/u;
|
||||
const _notPunctuationOrSpaceGfmStrongEm = /(?:[^\s\p{P}\p{S}]|~)/u;
|
||||
|
||||
// GFM allows * and _ inside strikethrough
|
||||
const _punctuationGfmDel = /(?![*_])[\p{P}\p{S}]/u;
|
||||
const _punctuationOrSpaceGfmDel = /(?![*_])[\s\p{P}\p{S}]/u;
|
||||
const _notPunctuationOrSpaceGfmDel = /(?:[^\s\p{P}\p{S}]|[*_])/u;
|
||||
|
||||
// sequences em should skip over [title](link), `code`, <html>
|
||||
const blockSkip = edit(/link|precode-code|html/, 'g')
|
||||
.replace('link', /\[(?:[^\[\]`]|(?<a>`+)[^`]+\k<a>(?!`))*?\]\((?:\\[\s\S]|[^\\\(\)]|\((?:\\[\s\S]|[^\\\(\)])*\))*\)/)
|
||||
@@ -332,6 +337,27 @@ const emStrongRDelimUnd = edit(
|
||||
.replace(/punct/g, _punctuation)
|
||||
.getRegex();
|
||||
|
||||
// Tilde left delimiter for strikethrough (similar to emStrongLDelim for asterisk)
|
||||
const delLDelim = edit(/^~~?(?:((?!~)punct)|[^\s~])/, 'u')
|
||||
.replace(/punct/g, _punctuationGfmDel)
|
||||
.getRegex();
|
||||
|
||||
// Tilde delimiter patterns for strikethrough (similar to asterisk)
|
||||
const delRDelimCore =
|
||||
'^[^~]+(?=[^~])' // Consume to delim
|
||||
+ '|(?!~)punct(~~?)(?=[\\s]|$)' // (1) #~~ can only be a Right Delimiter
|
||||
+ '|notPunctSpace(~~?)(?!~)(?=punctSpace|$)' // (2) a~~#, a~~ can only be a Right Delimiter
|
||||
+ '|(?!~)punctSpace(~~?)(?=notPunctSpace)' // (3) #~~a, ~~a can only be Left Delimiter
|
||||
+ '|[\\s](~~?)(?!~)(?=punct)' // (4) ~~# can only be Left Delimiter
|
||||
+ '|(?!~)punct(~~?)(?!~)(?=punct)' // (5) #~~# can be either Left or Right Delimiter
|
||||
+ '|notPunctSpace(~~?)(?=notPunctSpace)'; // (6) a~~a can be either Left or Right Delimiter
|
||||
|
||||
const delRDelim = edit(delRDelimCore, 'gu')
|
||||
.replace(/notPunctSpace/g, _notPunctuationOrSpaceGfmDel)
|
||||
.replace(/punctSpace/g, _punctuationOrSpaceGfmDel)
|
||||
.replace(/punct/g, _punctuationGfmDel)
|
||||
.getRegex();
|
||||
|
||||
const anyPunctuation = edit(/\\(punct)/, 'gu')
|
||||
.replace(/punct/g, _punctuation)
|
||||
.getRegex();
|
||||
@@ -389,6 +415,8 @@ const inlineNormal = {
|
||||
br,
|
||||
code: inlineCode,
|
||||
del: noopTest,
|
||||
delLDelim: noopTest,
|
||||
delRDelim: noopTest,
|
||||
emStrongLDelim,
|
||||
emStrongRDelimAst,
|
||||
emStrongRDelimUnd,
|
||||
@@ -427,6 +455,8 @@ const inlineGfm: Record<InlineKeys, RegExp> = {
|
||||
...inlineNormal,
|
||||
emStrongRDelimAst: emStrongRDelimAstGfm,
|
||||
emStrongLDelim: emStrongLDelimGfm,
|
||||
delLDelim,
|
||||
delRDelim,
|
||||
url: edit(/^((?:protocol):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/)
|
||||
.replace('protocol', _caseInsensitiveProtocol)
|
||||
.replace('email', /[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/)
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
<p>~1,~2</p>
|
||||
<p>Between ~1.34 and ~5.4</p>
|
||||
<p>Between (~1.34) and (~5.4)</p>
|
||||
<p>~ test~</p>
|
||||
<p>~test ~</p>
|
||||
@@ -0,0 +1,9 @@
|
||||
~1,~2
|
||||
|
||||
Between ~1.34 and ~5.4
|
||||
|
||||
Between (~1.34) and (~5.4)
|
||||
|
||||
~ test~
|
||||
|
||||
~test ~
|
||||
@@ -0,0 +1,16 @@
|
||||
<p><del>test 💁</del></p>
|
||||
<p><del>💁 test</del></p>
|
||||
<p><del>🤓 test</del></p>
|
||||
<p><del>🏖️ test</del></p>
|
||||
<p><del>🏖️🤓💁 test</del></p>
|
||||
<p><del>💁 test</del> test</p>
|
||||
<p>test <del>💁 test</del></p>
|
||||
<p>test <del>💁 test</del> test</p>
|
||||
<p>~~💁 ~~</p>
|
||||
<p>~~ 💁~~</p>
|
||||
<p><del>⚠️ test</del></p>
|
||||
<p><del>💁 test</del></p>
|
||||
<p><del>t💁t</del> test</p>
|
||||
<p><del>t💁t</del> test</p>
|
||||
<p>~1 😁~2</p>
|
||||
<p><del>🏴☠️</del> test</p>
|
||||
@@ -0,0 +1,31 @@
|
||||
~~test 💁~~
|
||||
|
||||
~~💁 test~~
|
||||
|
||||
~~🤓 test~~
|
||||
|
||||
~~🏖️ test~~
|
||||
|
||||
~~🏖️🤓💁 test~~
|
||||
|
||||
~~💁 test~~ test
|
||||
|
||||
test ~~💁 test~~
|
||||
|
||||
test ~~💁 test~~ test
|
||||
|
||||
~~💁 ~~
|
||||
|
||||
~~ 💁~~
|
||||
|
||||
~~⚠️ test~~
|
||||
|
||||
~💁 test~
|
||||
|
||||
~t💁t~ test
|
||||
|
||||
~~t💁t~~ test
|
||||
|
||||
~1 😁~2
|
||||
|
||||
~~🏴☠️~~ test
|
||||
Reference in New Issue
Block a user