Refactor to remove micromark helper getExclusionsForToken.

This commit is contained in:
David Anson 2024-09-27 23:58:40 -07:00
parent cfcb5fbd57
commit 5e568d0da9
6 changed files with 231 additions and 124 deletions

View file

@ -2,8 +2,8 @@
"use strict";
const { addError, withinAnyRange } = require("../helpers");
const { addRangeToSet, getExclusionsForToken } = require("../helpers/micromark.cjs");
const { addError, hasOverlap } = require("../helpers");
const { addRangeToSet } = require("../helpers/micromark.cjs");
const { filterByTypesCached } = require("./cache");
const reversedLinkRe =
@ -21,34 +21,35 @@ module.exports = {
for (const codeBlock of filterByTypesCached([ "codeFenced", "codeIndented" ])) {
addRangeToSet(codeBlockLineNumbers, codeBlock.startLine, codeBlock.endLine);
}
const exclusions = [];
for (const codeText of filterByTypesCached([ "codeText" ])) {
exclusions.push(...getExclusionsForToken(params.lines, codeText));
}
const codeTexts = filterByTypesCached([ "codeText" ]);
for (const [ lineIndex, line ] of params.lines.entries()) {
if (!codeBlockLineNumbers.has(lineIndex + 1)) {
const lineNumber = lineIndex + 1;
if (!codeBlockLineNumbers.has(lineNumber)) {
let match = null;
while ((match = reversedLinkRe.exec(line)) !== null) {
const [ reversedLink, preChar, linkText, linkDestination ] = match;
const index = match.index + preChar.length;
const length = match[0].length - preChar.length;
if (
!linkText.endsWith("\\") &&
!linkDestination.endsWith("\\") &&
!withinAnyRange(exclusions, lineIndex + 1, index, length)
!linkDestination.endsWith("\\")
) {
addError(
onError,
lineIndex + 1,
reversedLink.slice(preChar.length),
undefined,
[ index + 1, length ],
{
"editColumn": index + 1,
"deleteCount": length,
"insertText": `[${linkText}](${linkDestination})`
}
);
const column = match.index + preChar.length + 1;
const length = match[0].length - preChar.length;
/** @type {import("../helpers").FileRange} */
const range = { "startLine": lineNumber, "startColumn": column, "endLine": lineNumber, "endColumn": column + length - 1 };
if (!codeTexts.some((codeText) => hasOverlap(codeText, range))) {
addError(
onError,
lineNumber,
reversedLink.slice(preChar.length),
undefined,
[ column, length ],
{
"editColumn": column,
"deleteCount": length,
"insertText": `[${linkText}](${linkDestination})`
}
);
}
}
}
}