From a508824b0f43c0ca25131fa295fdd527c4f1bb7e Mon Sep 17 00:00:00 2001 From: David Anson Date: Fri, 26 Nov 2021 05:37:04 +0000 Subject: [PATCH] Refactor helpers.emphasisMarkersInContent slightly to avoid duplicate/unnecessary work. --- demo/markdownlint-browser.js | 34 +++++++++++++++++----------------- helpers/helpers.js | 32 ++++++++++++++++---------------- lib/md037.js | 2 +- 3 files changed, 34 insertions(+), 34 deletions(-) diff --git a/demo/markdownlint-browser.js b/demo/markdownlint-browser.js index 91363bb5..81406185 100644 --- a/demo/markdownlint-browser.js +++ b/demo/markdownlint-browser.js @@ -613,6 +613,18 @@ module.exports.frontMatterHasTitle = function emphasisMarkersInContent(params) { var lines = params.lines; var byLine = new Array(lines.length); + // Search links + lines.forEach(function (tokenLine, tokenLineIndex) { + var inLine = []; + var linkMatch = null; + while ((linkMatch = linkRe.exec(tokenLine))) { + var markerMatch = null; + while ((markerMatch = emphasisMarkersRe.exec(linkMatch[0]))) { + inLine.push(linkMatch.index + markerMatch.index); + } + } + byLine[tokenLineIndex] = inLine; + }); // Search code spans filterTokens(params, "inline", function (token) { var children = token.children, lineNumber = token.lineNumber, map = token.map; @@ -621,30 +633,18 @@ function emphasisMarkersInContent(params) { forEachInlineCodeSpan(tokenLines.join("\n"), function (code, lineIndex, column, tickCount) { var codeLines = code.split(newLineRe); codeLines.forEach(function (codeLine, codeLineIndex) { + var byLineIndex = lineNumber - 1 + lineIndex + codeLineIndex; + var inLine = byLine[byLineIndex]; + var codeLineOffset = codeLineIndex ? 0 : column - 1 + tickCount; var match = null; while ((match = emphasisMarkersRe.exec(codeLine))) { - var byLineIndex = lineNumber - 1 + lineIndex + codeLineIndex; - var inLine = byLine[byLineIndex] || []; - var codeLineOffset = codeLineIndex ? 0 : column - 1 + tickCount; inLine.push(codeLineOffset + match.index); - byLine[byLineIndex] = inLine; } + byLine[byLineIndex] = inLine; }); }); } }); - // Search links - lines.forEach(function (tokenLine, tokenLineIndex) { - var linkMatch = null; - while ((linkMatch = linkRe.exec(tokenLine))) { - var markerMatch = null; - while ((markerMatch = emphasisMarkersRe.exec(linkMatch[0]))) { - var inLine = byLine[tokenLineIndex] || []; - inLine.push(linkMatch.index + markerMatch.index); - byLine[tokenLineIndex] = inLine; - } - } - }); return byLine; } module.exports.emphasisMarkersInContent = emphasisMarkersInContent; @@ -3486,7 +3486,7 @@ module.exports = { var match = null; // Match all emphasis-looking runs in the line... while ((match = emphasisRe.exec(line))) { - var ignoreMarkersForLine = ignoreMarkersByLine[lineIndex] || []; + var ignoreMarkersForLine = ignoreMarkersByLine[lineIndex]; var matchIndex = match.index + match[1].length; if (ignoreMarkersForLine.includes(matchIndex)) { // Ignore emphasis markers inside code spans and links diff --git a/helpers/helpers.js b/helpers/helpers.js index 500483ee..6e2dd1f4 100644 --- a/helpers/helpers.js +++ b/helpers/helpers.js @@ -632,6 +632,18 @@ module.exports.frontMatterHasTitle = function emphasisMarkersInContent(params) { const { lines } = params; const byLine = new Array(lines.length); + // Search links + lines.forEach((tokenLine, tokenLineIndex) => { + const inLine = []; + let linkMatch = null; + while ((linkMatch = linkRe.exec(tokenLine))) { + let markerMatch = null; + while ((markerMatch = emphasisMarkersRe.exec(linkMatch[0]))) { + inLine.push(linkMatch.index + markerMatch.index); + } + } + byLine[tokenLineIndex] = inLine; + }); // Search code spans filterTokens(params, "inline", (token) => { const { children, lineNumber, map } = token; @@ -642,31 +654,19 @@ function emphasisMarkersInContent(params) { (code, lineIndex, column, tickCount) => { const codeLines = code.split(newLineRe); codeLines.forEach((codeLine, codeLineIndex) => { + const byLineIndex = lineNumber - 1 + lineIndex + codeLineIndex; + const inLine = byLine[byLineIndex]; + const codeLineOffset = codeLineIndex ? 0 : column - 1 + tickCount; let match = null; while ((match = emphasisMarkersRe.exec(codeLine))) { - const byLineIndex = lineNumber - 1 + lineIndex + codeLineIndex; - const inLine = byLine[byLineIndex] || []; - const codeLineOffset = codeLineIndex ? 0 : column - 1 + tickCount; inLine.push(codeLineOffset + match.index); - byLine[byLineIndex] = inLine; } + byLine[byLineIndex] = inLine; }); } ); } }); - // Search links - lines.forEach((tokenLine, tokenLineIndex) => { - let linkMatch = null; - while ((linkMatch = linkRe.exec(tokenLine))) { - let markerMatch = null; - while ((markerMatch = emphasisMarkersRe.exec(linkMatch[0]))) { - const inLine = byLine[tokenLineIndex] || []; - inLine.push(linkMatch.index + markerMatch.index); - byLine[tokenLineIndex] = inLine; - } - } - }); return byLine; } module.exports.emphasisMarkersInContent = emphasisMarkersInContent; diff --git a/lib/md037.js b/lib/md037.js index 14c5c032..1d46163b 100644 --- a/lib/md037.js +++ b/lib/md037.js @@ -105,7 +105,7 @@ module.exports = { let match = null; // Match all emphasis-looking runs in the line... while ((match = emphasisRe.exec(line))) { - const ignoreMarkersForLine = ignoreMarkersByLine[lineIndex] || []; + const ignoreMarkersForLine = ignoreMarkersByLine[lineIndex]; const matchIndex = match.index + match[1].length; if (ignoreMarkersForLine.includes(matchIndex)) { // Ignore emphasis markers inside code spans and links