Skip to content

Commit

Permalink
Removed redundancy in "startEM" check
Browse files Browse the repository at this point in the history
  • Loading branch information
calculuschild committed Jul 8, 2020
1 parent 211b9f9 commit cc778ad
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/Lexer.js
Expand Up @@ -338,8 +338,8 @@ module.exports = class Lexer {
}
}
// Mask out other blocks
while ((match = this.tokenizer.rules.inline.emSkip.exec(maskedSrc)) != null) {
maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.emSkip.lastIndex);
while ((match = this.tokenizer.rules.inline.blockSkip.exec(maskedSrc)) != null) {
maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);
}

while (src) {
Expand Down
30 changes: 13 additions & 17 deletions src/Tokenizer.js
Expand Up @@ -491,8 +491,8 @@ module.exports = class Tokenizer {

strong(src, maskedSrc, prevChar = '') {
let match = this.rules.inline.strStart.exec(src);

if (match) {
if (match && (!match[1] || (match[1] && (prevChar === '' || this.rules.inline.punctuation.exec(prevChar))))) {
maskedSrc = maskedSrc.slice(-1 * src.length);
let strEnd;

Expand All @@ -511,21 +511,19 @@ module.exports = class Tokenizer {
}

if (cap) {
if (!cap[1] || (cap[1] && (prevChar === '' || this.rules.inline.punctuation.exec(prevChar)))) {
return {
type: 'strong',
raw: src.slice(0, cap[0].length),
text: src.slice(2, cap[0].length - 2)
};
}
return {
type: 'strong',
raw: src.slice(0, cap[0].length),
text: src.slice(2, cap[0].length - 2)
};
}
}
}

em(src, maskedSrc, prevChar = '') {
let match = this.rules.inline.emStart.exec(src);

if (match) {
if (match && (!match[1] || (match[1] && (prevChar === '' || this.rules.inline.punctuation.exec(prevChar))))) {
maskedSrc = maskedSrc.slice(-1 * src.length);
let emEnd;

Expand All @@ -544,13 +542,11 @@ module.exports = class Tokenizer {
}

if (cap) {
if (!cap[1] || (cap[1] && (prevChar === '' || this.rules.inline.punctuation.exec(prevChar)))) {
return {
type: 'em',
raw: src.slice(0, cap[0].length),
text: src.slice(1, cap[0].length - 1)
};
}
return {
type: 'em',
raw: src.slice(0, cap[0].length),
text: src.slice(1, cap[0].length - 1)
};
}
}
}
Expand Down
39 changes: 23 additions & 16 deletions src/rules.js
Expand Up @@ -169,15 +169,15 @@ const inline = {
reflink: /^!?\[(label)\]\[(?!\s*\])((?:\\[\[\]]?|[^\[\]\\])+)\]/,
nolink: /^!?\[(?!\s*\])((?:\[[^\[\]]*\]|\\[\[\]]|[^\[\]])*)\](?:\[\])?/,
reflinkSearch: 'reflink|nolink(?!\\()',
strStart: /^\*\*|__/,
strEndAst: /[^punctuation\s]\*\*(?!\*)|[punctuation]\*\*(?!\*)(?:(?=[punctuation\s]|$))/,
strEndUnd: /[^\s]__(?!_)(?:(?=[punctuation\s])|$)/,
strong: /^(?:(\*\*(?=[*punctuation]))|\*\*)(?![\s])((?:(?:(?!evSkip)(?:[^*]|\\\*)|evSkip)|(?:(?:(?!evSkip)(?:[^*]|\\\*)|evSkip)*?(?<!\\)\*){2})+?)\*\*$|^__(?![\s])((?:(?:(?!evSkip)(?:[^_]|\\_)|evSkip)|(?:(?:(?!evSkip)(?:[^_]|\\_)|evSkip)*?(?<!\\)_){2})+?)__$/,
emStart: /^[*_]/,
emEndAst: /[^punctuation\s]\*(?!\*)|[punctuation]\*(?!\*)(?:(?=[punctuation\s]|$))/,
emEndUnd: /[^\s]_(?!_)(?:(?=[punctuation\s])|$)/,
// (1) returns if starts w/ punctuation | (2)Check groups to skip over ⬐ skip if needed ⬐repeat logic for inner *'s (must be in pairs) ⬐last char can't be punct OR final * must also be followed by punct (or endline) | (3) Underscores ⬐Check groups to skip over ⬐skip if needed ⬐repeat logic for inner _'s (must be in pairs)⬎ ⬐last char can't be a space, and final _ must preceed punct or \s (or endline)
em: /^(?:(\*(?=[punctuation]))|\*)(?![*\s])(?:(?:(?!evSkip)(?:[^*]|\\\*)|evSkip)|\*(?:(?!evSkip)(?:[^*]|\\\*)|evSkip)*?\*)*?\*$|^_(?![_\s])(?:(?:(?!evSkip)(?:[^_]|\\_)|evSkip)|(?:(?:(?!evSkip)(?:[^_]|\\_)|evSkip)*?_){2})*?_$/,
strStart: /^(?:(\*\*(?=[*punctuation]))|\*\*)(?![\s])|__/, // (1) returns if starts w/ punctuation
strEndAst: /[^punctuation\s]\*\*(?!\*)|[punctuation]\*\*(?!\*)(?:(?=[punctuation\s]|$))/, // last char can't be punct, or final * must also be followed by punct (or endline)
strEndUnd: /[^\s]__(?!_)(?:(?=[punctuation\s])|$)/, // last char can't be a space, and final _ must preceed punct or \s (or endline)
strong: /^\*\*(?:(?:(?!overlapSkip)(?:[^*]|\\\*)|overlapSkip)|\*(?:(?!overlapSkip)(?:[^*]|\\\*)|overlapSkip)*?\*)+?\*\*$|^__(?![\s])((?:(?:(?!overlapSkip)(?:[^_]|\\_)|overlapSkip)|_(?:(?!overlapSkip)(?:[^_]|\\_)|overlapSkip)*?_)+?)__$/,
emStart: /^(?:(\*(?=[punctuation]))|\*)(?![*\s])|_/, // (1) returns if starts w/ punctuation
emEndAst: /[^punctuation\s]\*(?!\*)|[punctuation]\*(?!\*)(?:(?=[punctuation\s]|$))/, // last char can't be punct, or final * must also be followed by punct (or endline)
emEndUnd: /[^\s]_(?!_)(?:(?=[punctuation\s])|$)/, // last char can't be a space, and final _ must preceed punct or \s (or endline)
// skip overlapping Strong ⬐repeat logic for inner *'s (must be in pairs)| Underscores skip overlapping Strong ⬐repeat logic for inner _'s (must be in pairs)⬎
em: /^\*(?:(?:(?!overlapSkip)(?:[^*]|\\\*)|overlapSkip)|\*(?:(?!overlapSkip)(?:[^*]|\\\*)|overlapSkip)*?\*)+?\*$|^_(?![_\s])(?:(?:(?!overlapSkip)(?:[^_]|\\_)|overlapSkip)|_(?:(?!overlapSkip)(?:[^_]|\\_)|overlapSkip)*?_)+?_$/,
code: /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/,
br: /^( {2,}|\\)\n(?!\s*$)/,
del: noopTest,
Expand All @@ -191,15 +191,18 @@ inline._punctuation = '!"#$%&\'()+\\-.,/:;<=>?@\\[\\]`^{|}~';
inline.punctuation = edit(inline.punctuation).replace(/punctuation/g, inline._punctuation).getRegex();

// sequences em should skip over [title](link), `code`, <html>
inline._emSkip = '\\[[^\\]]*?\\]\\([^\\)]*?\\)|`[^`]*?`|<[^>]*?>';
inline._strSkip = '\\[[^\\]]*?\\]\\([^\\)]*?\\)|`[^`]*?`|<[^>]*?>';
inline._evSkip = '__[^_]*?__';
inline._blockSkip = '\\[[^\\]]*?\\]\\([^\\)]*?\\)|`[^`]*?`|<[^>]*?>';
inline._overlapSkip = '__[^_]*?__|\\*\\*\\[^\\*\\]*?\\*\\*';

inline.em = edit(inline.em)
.replace(/punctuation/g, inline._punctuation)
.replace(/evSkip/g, inline._evSkip)
.replace(/overlapSkip/g, inline._overlapSkip)
.getRegex();

inline.emStart = edit(inline.emStart)
.replace(/punctuation/g, inline._punctuation)
.getRegex();

inline.emEndAst = edit(inline.emEndAst, 'g')
.replace(/punctuation/g, inline._punctuation)
.getRegex();
Expand All @@ -208,17 +211,21 @@ inline.emEndUnd = edit(inline.emEndUnd, 'g')
.replace(/punctuation/g, inline._punctuation)
.getRegex();

inline.emSkip = edit(inline._emSkip, 'g')
inline.blockSkip = edit(inline._blockSkip, 'g')
.getRegex();

inline.evSkip = edit(inline._evSkip, 'g')
inline.overlapSkip = edit(inline._overlapSkip, 'g')
.getRegex();

inline.strong = edit(inline.strong)
.replace(/punctuation/g, inline._punctuation)
.replace(/emSkip/g, inline._emSkip)
.replace(/blockSkip/g, inline._blockSkip)
.getRegex();

inline.strStart = edit(inline.strStart)
.replace(/punctuation/g, inline._punctuation)
.getRegex();

inline.strEndAst = edit(inline.strEndAst, 'g')
.replace(/punctuation/g, inline._punctuation)
.getRegex();
Expand Down

0 comments on commit cc778ad

Please sign in to comment.