Skip to content

Commit

Permalink
fix: em strong (#2075)
Browse files Browse the repository at this point in the history
  • Loading branch information
calculuschild authored Jun 1, 2021
1 parent 69871c0 commit 825a9f8
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 17 deletions.
29 changes: 15 additions & 14 deletions src/Tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,8 @@ module.exports = class Tokenizer {
let match = this.rules.inline.emStrong.lDelim.exec(src);
if (!match) return;

if (match[3] && prevChar.match(/[\p{L}\p{N}]/u)) return; // _ can't be between two alphanumerics. \p{L}\p{N} includes non-english alphabet/numbers as well
// _ can't be between two alphanumerics. \p{L}\p{N} includes non-english alphabet/numbers as well
if (match[3] && prevChar.match(/[\p{L}\p{N}]/u)) return;

const nextChar = match[1] || match[2] || '';

Expand All @@ -561,12 +562,13 @@ module.exports = class Tokenizer {
const endReg = match[0][0] === '*' ? this.rules.inline.emStrong.rDelimAst : this.rules.inline.emStrong.rDelimUnd;
endReg.lastIndex = 0;

maskedSrc = maskedSrc.slice(-1 * src.length + lLength); // Bump maskedSrc to same section of string as src (move to lexer?)
// Clip maskedSrc to same section of string as src (move to lexer?)
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; // matched the first alternative in rules.js (skip the * in __abc*abc__)
if (!rDelim) continue; // skip single * in __abc*abc__

rLength = rDelim.length;

Expand All @@ -584,25 +586,24 @@ module.exports = class Tokenizer {

if (delimTotal > 0) continue; // Haven't found enough closing delimiters

// If this is the last rDelimiter, remove extra characters. *a*** -> *a*
if (delimTotal + midDelimTotal - rLength <= 0 && !maskedSrc.slice(endReg.lastIndex).match(endReg)) {
rLength = Math.min(rLength, rLength + delimTotal + midDelimTotal);
}
// Remove extra characters. *a*** -> *a*
rLength = Math.min(rLength, rLength + delimTotal + midDelimTotal);

// Create `em` if smallest delimiter has odd char count. *a***
if (Math.min(lLength, rLength) % 2) {
return {
type: 'em',
raw: src.slice(0, lLength + match.index + rLength + 1),
text: src.slice(1, lLength + match.index + rLength)
};
}
if (Math.min(lLength, rLength) % 2 === 0) {
return {
type: 'strong',
raw: src.slice(0, lLength + match.index + rLength + 1),
text: src.slice(2, lLength + match.index + rLength - 1)
};
}

// Create 'strong' if smallest delimiter has even char count. **a***
return {
type: 'strong',
raw: src.slice(0, lLength + match.index + rLength + 1),
text: src.slice(2, lLength + match.index + rLength - 1)
};
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ const inline = {
emStrong: {
lDelim: /^(?:\*+(?:([punct_])|[^\s*]))|^_+(?:([punct*])|([^\s_]))/,
// (1) and (2) can only be a Right Delimiter. (3) and (4) can only be Left. (5) and (6) can be either Left or Right.
// () Skip other delimiter (1) #*** (2) a***#, a*** (3) #***a, ***a (4) ***# (5) #***# (6) a***a
rDelimAst: /\_\_[^_]*?\*[^_]*?\_\_|[punct_](\*+)(?=[\s]|$)|[^punct*_\s](\*+)(?=[punct_\s]|$)|[punct_\s](\*+)(?=[^punct*_\s])|[\s](\*+)(?=[punct_])|[punct_](\*+)(?=[punct_])|[^punct*_\s](\*+)(?=[^punct*_\s])/,
rDelimUnd: /\*\*[^*]*?\_[^*]*?\*\*|[punct*](\_+)(?=[\s]|$)|[^punct*_\s](\_+)(?=[punct*\s]|$)|[punct*\s](\_+)(?=[^punct*_\s])|[\s](\_+)(?=[punct*])|[punct*](\_+)(?=[punct*])/ // ^- Not allowed for _
// () Skip other delimiter (1) #*** (2) a***#, a*** (3) #***a, ***a (4) ***# (5) #***# (6) a***a
rDelimAst: /\_\_[^_*]*?\*[^_*]*?\_\_|[punct_](\*+)(?=[\s]|$)|[^punct*_\s](\*+)(?=[punct_\s]|$)|[punct_\s](\*+)(?=[^punct*_\s])|[\s](\*+)(?=[punct_])|[punct_](\*+)(?=[punct_])|[^punct*_\s](\*+)(?=[^punct*_\s])/,
rDelimUnd: /\*\*[^_*]*?\_[^_*]*?\*\*|[punct*](\_+)(?=[\s]|$)|[^punct*_\s](\_+)(?=[punct*\s]|$)|[punct*\s](\_+)(?=[^punct*_\s])|[\s](\_+)(?=[punct*])|[punct*](\_+)(?=[punct*])/ // ^- Not allowed for _
},
code: /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/,
br: /^( {2,}|\\)\n(?!\s*$)/,
Expand Down
1 change: 1 addition & 0 deletions test/specs/new/em_strong_complex_nesting.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p><strong>E<em>mp</em><em><strong>ha</strong></em><em>si</em>s</strong></p>
1 change: 1 addition & 0 deletions test/specs/new/em_strong_complex_nesting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**E*mp****ha****si*s**
3 changes: 3 additions & 0 deletions test/specs/new/em_strong_multiline.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p><em>italic <strong>bold</strong> italic</em>
<em>italic <strong>bold</strong> italic</em>
<em>italic <strong>bold</strong> italic</em></p>
3 changes: 3 additions & 0 deletions test/specs/new/em_strong_multiline.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
_italic **bold** italic_
_italic **bold** italic_
_italic **bold** italic_

1 comment on commit 825a9f8

@vercel
Copy link

@vercel vercel bot commented on 825a9f8 Jun 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.