From 75e5954e81b2aefe8f3ea5c56f74528f9219895d Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Thu, 27 Feb 2020 19:19:05 -0500 Subject: [PATCH] DRY --- src/highlight.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/highlight.js b/src/highlight.js index b5146c0e63..9b0ca7753b 100644 --- a/src/highlight.js +++ b/src/highlight.js @@ -177,7 +177,6 @@ const HLJS = function(hljs) { function startNewMode(mode) { result += mode.className? buildSpan(mode.className, '', true): ''; top = Object.create(mode, {parent: {value: top}}); - top.terminators.startAt = 0; } function doIgnore(lexeme) { @@ -189,6 +188,7 @@ const HLJS = function(hljs) { } else { // no need to move the cursor, we still have additional regexes to try and // match at this very spot + findNextRegexMatch = true; return 0; } } @@ -203,9 +203,6 @@ const HLJS = function(hljs) { return doIgnore(lexeme); } - // we are not ignoring, so next match should start with first regex in stack - top.terminators.startAt = 0; - if (new_mode && new_mode.endSameAsBegin) { new_mode.endRe = regex.escape( lexeme ); } @@ -229,7 +226,6 @@ const HLJS = function(hljs) { var lexeme = match[0]; var matchPlusRemainder = codeToHighlight.substr(match.index); var end_mode = endOfMode(top, matchPlusRemainder); - top.terminators.startAt = 0; if (!end_mode) { return; } var origin = top; @@ -282,7 +278,6 @@ const HLJS = function(hljs) { // sometimes they can end up matching nothing at all // Ref: https://github.com/highlightjs/highlight.js/issues/2140 if (lastMatch.type=="begin" && match.type=="end" && lastMatch.index == match.index && lexeme === "") { - top.terminators.startAt = 0; // spit the "skipped" character that our regex choked on back into the output sequence mode_buffer += codeToHighlight.slice(match.index, match.index + 1); if (!SAFE_MODE) { @@ -339,11 +334,17 @@ const HLJS = function(hljs) { } var mode_buffer = ''; var relevance = 0; + var findNextRegexMatch = false; try { var match, count, index = 0; - top.terminators.startAt = 0; + while (true) { top.terminators.lastIndex = index; + if (findNextRegexMatch) { + findNextRegexMatch = false; + } else { + top.terminators.startAt = 0; + } match = top.terminators.exec(codeToHighlight); // console.log("match", match[0], match.rule && match.rule.begin) if (!match)