Skip to content

Commit 8a01658

Browse files
authored
fix: allow strikethrough inside strong and em to follow gfm (#3569)
1 parent 33eb5c6 commit 8a01658

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

src/rules.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -249,27 +249,40 @@ const _notPunctuationOrSpace = /[^\s\p{P}\p{S}]/u;
249249
const punctuation = edit(/^((?![*_])punctSpace)/, 'u')
250250
.replace(/punctSpace/g, _punctuationOrSpace).getRegex();
251251

252+
// GFM allows ~ inside strong and em for strikethrough
253+
const _punctuationGfmStrongEm = /(?!~)[\p{P}\p{S}]/u;
254+
const _punctuationOrSpaceGfmStrongEm = /(?!~)[\s\p{P}\p{S}]/u;
255+
const _notPunctuationOrSpaceGfmStrongEm = /(?:[^\s\p{P}\p{S}]|~)/u;
256+
252257
// sequences em should skip over [title](link), `code`, <html>
253258
const blockSkip = /\[[^[\]]*?\]\((?:\\.|[^\\\(\)]|\((?:\\.|[^\\\(\)])*\))*\)|`[^`]*?`|<[^<>]*?>/g;
254259

255260
const emStrongLDelim = edit(/^(?:\*+(?:((?!\*)punct)|[^\s*]))|^_+(?:((?!_)punct)|([^\s_]))/, 'u')
256261
.replace(/punct/g, _punctuation)
257262
.getRegex();
258263

259-
const emStrongRDelimAst = edit(
264+
const emStrongRDelimAstCore =
260265
'^[^_*]*?__[^_*]*?\\*[^_*]*?(?=__)' // Skip orphan inside strong
261266
+ '|[^*]+(?=[^*])' // Consume to delim
262267
+ '|(?!\\*)punct(\\*+)(?=[\\s]|$)' // (1) #*** can only be a Right Delimiter
263268
+ '|notPunctSpace(\\*+)(?!\\*)(?=punctSpace|$)' // (2) a***#, a*** can only be a Right Delimiter
264269
+ '|(?!\\*)punctSpace(\\*+)(?=notPunctSpace)' // (3) #***a, ***a can only be Left Delimiter
265270
+ '|[\\s](\\*+)(?!\\*)(?=punct)' // (4) ***# can only be Left Delimiter
266271
+ '|(?!\\*)punct(\\*+)(?!\\*)(?=punct)' // (5) #***# can be either Left or Right Delimiter
267-
+ '|notPunctSpace(\\*+)(?=notPunctSpace)', 'gu') // (6) a***a can be either Left or Right Delimiter
272+
+ '|notPunctSpace(\\*+)(?=notPunctSpace)'; // (6) a***a can be either Left or Right Delimiter
273+
274+
const emStrongRDelimAst = edit(emStrongRDelimAstCore, 'gu')
268275
.replace(/notPunctSpace/g, _notPunctuationOrSpace)
269276
.replace(/punctSpace/g, _punctuationOrSpace)
270277
.replace(/punct/g, _punctuation)
271278
.getRegex();
272279

280+
const emStrongRDelimAstGfm = edit(emStrongRDelimAstCore, 'gu')
281+
.replace(/notPunctSpace/g, _notPunctuationOrSpaceGfmStrongEm)
282+
.replace(/punctSpace/g, _punctuationOrSpaceGfmStrongEm)
283+
.replace(/punct/g, _punctuationGfmStrongEm)
284+
.getRegex();
285+
273286
// (6) Not allowed for _
274287
const emStrongRDelimUnd = edit(
275288
'^[^_*]*?\\*\\*[^_*]*?_[^_*]*?(?=\\*\\*)' // Skip orphan inside strong
@@ -375,7 +388,7 @@ const inlinePedantic: Record<InlineKeys, RegExp> = {
375388

376389
const inlineGfm: Record<InlineKeys, RegExp> = {
377390
...inlineNormal,
378-
escape: edit(escape).replace('])', '~|])').getRegex(),
391+
emStrongRDelimAst: emStrongRDelimAstGfm,
379392
url: edit(/^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/, 'i')
380393
.replace('email', /[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/)
381394
.getRegex(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<p><em><del>a</del></em>b</p>
2+
3+
<p><em><del>a</del></em>b</p>
4+
5+
<p><strong><del>a</del></strong>b</p>
6+
7+
<p><strong><del>a</del></strong>b</p>
8+
9+
<p>_<del>a</del>_b</p>
10+
11+
<p>_<del>a</del>_b</p>
12+
13+
<p>__<del>a</del>__b</p>
14+
15+
<p>__<del>a</del>__b</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*~a~*b
2+
3+
*~~a~~*b
4+
5+
**~a~**b
6+
7+
**~~a~~**b
8+
9+
_~a~_b
10+
11+
_~~a~~_b
12+
13+
__~a~__b
14+
15+
__~~a~~__b

0 commit comments

Comments
 (0)