Refactor helpers.emphasisMarkersInContent slightly to avoid duplicate/unnecessary work.

This commit is contained in:
David Anson 2021-11-26 05:37:04 +00:00 committed by GitHub
parent 11806dc5cb
commit a508824b0f
3 changed files with 34 additions and 34 deletions

View file

@ -613,6 +613,18 @@ module.exports.frontMatterHasTitle =
function emphasisMarkersInContent(params) { function emphasisMarkersInContent(params) {
var lines = params.lines; var lines = params.lines;
var byLine = new Array(lines.length); 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 // Search code spans
filterTokens(params, "inline", function (token) { filterTokens(params, "inline", function (token) {
var children = token.children, lineNumber = token.lineNumber, map = token.map; 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) { forEachInlineCodeSpan(tokenLines.join("\n"), function (code, lineIndex, column, tickCount) {
var codeLines = code.split(newLineRe); var codeLines = code.split(newLineRe);
codeLines.forEach(function (codeLine, codeLineIndex) { 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; var match = null;
while ((match = emphasisMarkersRe.exec(codeLine))) { 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); 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; return byLine;
} }
module.exports.emphasisMarkersInContent = emphasisMarkersInContent; module.exports.emphasisMarkersInContent = emphasisMarkersInContent;
@ -3486,7 +3486,7 @@ module.exports = {
var match = null; var match = null;
// Match all emphasis-looking runs in the line... // Match all emphasis-looking runs in the line...
while ((match = emphasisRe.exec(line))) { while ((match = emphasisRe.exec(line))) {
var ignoreMarkersForLine = ignoreMarkersByLine[lineIndex] || []; var ignoreMarkersForLine = ignoreMarkersByLine[lineIndex];
var matchIndex = match.index + match[1].length; var matchIndex = match.index + match[1].length;
if (ignoreMarkersForLine.includes(matchIndex)) { if (ignoreMarkersForLine.includes(matchIndex)) {
// Ignore emphasis markers inside code spans and links // Ignore emphasis markers inside code spans and links

View file

@ -632,6 +632,18 @@ module.exports.frontMatterHasTitle =
function emphasisMarkersInContent(params) { function emphasisMarkersInContent(params) {
const { lines } = params; const { lines } = params;
const byLine = new Array(lines.length); 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 // Search code spans
filterTokens(params, "inline", (token) => { filterTokens(params, "inline", (token) => {
const { children, lineNumber, map } = token; const { children, lineNumber, map } = token;
@ -642,31 +654,19 @@ function emphasisMarkersInContent(params) {
(code, lineIndex, column, tickCount) => { (code, lineIndex, column, tickCount) => {
const codeLines = code.split(newLineRe); const codeLines = code.split(newLineRe);
codeLines.forEach((codeLine, codeLineIndex) => { codeLines.forEach((codeLine, codeLineIndex) => {
const byLineIndex = lineNumber - 1 + lineIndex + codeLineIndex;
const inLine = byLine[byLineIndex];
const codeLineOffset = codeLineIndex ? 0 : column - 1 + tickCount;
let match = null; let match = null;
while ((match = emphasisMarkersRe.exec(codeLine))) { 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); 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; return byLine;
} }
module.exports.emphasisMarkersInContent = emphasisMarkersInContent; module.exports.emphasisMarkersInContent = emphasisMarkersInContent;

View file

@ -105,7 +105,7 @@ module.exports = {
let match = null; let match = null;
// Match all emphasis-looking runs in the line... // Match all emphasis-looking runs in the line...
while ((match = emphasisRe.exec(line))) { while ((match = emphasisRe.exec(line))) {
const ignoreMarkersForLine = ignoreMarkersByLine[lineIndex] || []; const ignoreMarkersForLine = ignoreMarkersByLine[lineIndex];
const matchIndex = match.index + match[1].length; const matchIndex = match.index + match[1].length;
if (ignoreMarkersForLine.includes(matchIndex)) { if (ignoreMarkersForLine.includes(matchIndex)) {
// Ignore emphasis markers inside code spans and links // Ignore emphasis markers inside code spans and links