Skip to content

Commit

Permalink
fix: fix strikethrough inside strong and em to follow gfm (#3577)
Browse files Browse the repository at this point in the history
* fix: fix strikethrough inside strong and em to follow gfm

* add both sides tests
  • Loading branch information
UziTech authored Jan 6, 2025
1 parent 42ac48b commit 7712a53
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,16 @@ const _notPunctuationOrSpaceGfmStrongEm = /(?:[^\s\p{P}\p{S}]|~)/u;
// sequences em should skip over [title](link), `code`, <html>
const blockSkip = /\[[^[\]]*?\]\((?:\\.|[^\\\(\)]|\((?:\\.|[^\\\(\)])*\))*\)|`[^`]*?`|<[^<>]*?>/g;

const emStrongLDelim = edit(/^(?:\*+(?:((?!\*)punct)|[^\s*]))|^_+(?:((?!_)punct)|([^\s_]))/, 'u')
const emStrongLDelimCore = /^(?:\*+(?:((?!\*)punct)|[^\s*]))|^_+(?:((?!_)punct)|([^\s_]))/;

const emStrongLDelim = edit(emStrongLDelimCore, 'u')
.replace(/punct/g, _punctuation)
.getRegex();

const emStrongLDelimGfm = edit(emStrongLDelimCore, 'u')
.replace(/punct/g, _punctuationGfmStrongEm)
.getRegex();

const emStrongRDelimAstCore =
'^[^_*]*?__[^_*]*?\\*[^_*]*?(?=__)' // Skip orphan inside strong
+ '|[^*]+(?=[^*])' // Consume to delim
Expand Down Expand Up @@ -389,6 +395,7 @@ const inlinePedantic: Record<InlineKeys, RegExp> = {
const inlineGfm: Record<InlineKeys, RegExp> = {
...inlineNormal,
emStrongRDelimAst: emStrongRDelimAstGfm,
emStrongLDelim: emStrongLDelimGfm,
url: edit(/^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/, 'i')
.replace('email', /[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/)
.getRegex(),
Expand Down
32 changes: 32 additions & 0 deletions test/specs/new/strikethrough_in_em_strong.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,35 @@
<p>__<del>a</del>__b</p>

<p>__<del>a</del>__b</p>

<p>b<em><del>a</del></em></p>

<p>b<em><del>a</del></em></p>

<p>b<strong><del>a</del></strong></p>

<p>b<strong><del>a</del></strong></p>

<p>b_<del>a</del>_</p>

<p>b_<del>a</del>_</p>

<p>b__<del>a</del>__</p>

<p>b__<del>a</del>__</p>

<p>b<em><del>a</del></em>b</p>

<p>b<em><del>a</del></em>b</p>

<p>b<strong><del>a</del></strong>b</p>

<p>b<strong><del>a</del></strong>b</p>

<p>b_<del>a</del>_b</p>

<p>b_<del>a</del>_b</p>

<p>b__<del>a</del>__b</p>

<p>b__<del>a</del>__b</p>
32 changes: 32 additions & 0 deletions test/specs/new/strikethrough_in_em_strong.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,35 @@ _~~a~~_b
__~a~__b

__~~a~~__b

b*~a~*

b*~~a~~*

b**~a~**

b**~~a~~**

b_~a~_

b_~~a~~_

b__~a~__

b__~~a~~__

b*~a~*b

b*~~a~~*b

b**~a~**b

b**~~a~~**b

b_~a~_b

b_~~a~~_b

b__~a~__b

b__~~a~~__b

0 comments on commit 7712a53

Please sign in to comment.