mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2026-02-19 03:28:06 +01:00
Refactor to remove codeBlockAndSpanRanges helper.
This commit is contained in:
parent
c5f4a93cc7
commit
7efc2605b1
7 changed files with 96 additions and 128 deletions
|
|
@ -11,8 +11,6 @@ module.exports.set = (keyValuePairs) => {
|
|||
};
|
||||
module.exports.clear = () => map.clear();
|
||||
|
||||
module.exports.codeBlockAndSpanRanges =
|
||||
() => map.get("codeBlockAndSpanRanges");
|
||||
module.exports.flattenedLists =
|
||||
() => map.get("flattenedLists");
|
||||
module.exports.lineMetadata =
|
||||
|
|
|
|||
|
|
@ -596,14 +596,11 @@ function lintContent(
|
|||
};
|
||||
const lineMetadata =
|
||||
helpers.getLineMetadata(paramsBase);
|
||||
const codeBlockAndSpanRanges =
|
||||
helpers.codeBlockAndSpanRanges(paramsBase, lineMetadata);
|
||||
const flattenedLists =
|
||||
helpers.flattenLists(markdownitTokens);
|
||||
const referenceLinkImageData =
|
||||
helpers.getReferenceLinkImageData(micromarkTokens);
|
||||
cache.set({
|
||||
codeBlockAndSpanRanges,
|
||||
flattenedLists,
|
||||
lineMetadata,
|
||||
referenceLinkImageData
|
||||
|
|
|
|||
20
lib/md010.js
20
lib/md010.js
|
|
@ -3,7 +3,7 @@
|
|||
"use strict";
|
||||
|
||||
const { addError, withinAnyRange } = require("../helpers");
|
||||
const { filterByTypes, getDescendantsByType } = require("../helpers/micromark.cjs");
|
||||
const { filterByTypes, getDescendantsByType, getExclusionsForToken } = require("../helpers/micromark.cjs");
|
||||
|
||||
const tabRe = /\t+/g;
|
||||
|
||||
|
|
@ -47,20 +47,12 @@ module.exports = {
|
|||
return true;
|
||||
});
|
||||
for (const codeToken of codeTokens) {
|
||||
const codeFenced = (codeToken.type === "codeFenced");
|
||||
const startLine = codeToken.startLine + (codeFenced ? 1 : 0);
|
||||
const endLine = codeToken.endLine - (codeFenced ? 1 : 0);
|
||||
for (let lineNumber = startLine; lineNumber <= endLine; lineNumber++) {
|
||||
const startColumn =
|
||||
(lineNumber === codeToken.startLine) ? codeToken.startColumn : 1;
|
||||
const endColumn =
|
||||
(lineNumber === codeToken.endLine) ? codeToken.endColumn : params.lines[lineNumber - 1].length;
|
||||
exclusions.push([
|
||||
lineNumber,
|
||||
startColumn,
|
||||
endColumn - startColumn + 1
|
||||
]);
|
||||
const exclusionsForToken = getExclusionsForToken(params.lines, codeToken);
|
||||
if (codeToken.type === "codeFenced") {
|
||||
exclusionsForToken.pop();
|
||||
exclusionsForToken.shift();
|
||||
}
|
||||
exclusions.push(...exclusionsForToken);
|
||||
}
|
||||
for (let lineIndex = 0; lineIndex < params.lines.length; lineIndex++) {
|
||||
const line = params.lines[lineIndex];
|
||||
|
|
|
|||
24
lib/md011.js
24
lib/md011.js
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
const { addError, forEachLine, withinAnyRange } = require("../helpers");
|
||||
const { codeBlockAndSpanRanges, lineMetadata } = require("./cache");
|
||||
const { addError, withinAnyRange } = require("../helpers");
|
||||
const { addRangeToSet, getExclusionsForToken, filterByTypes } = require("../helpers/micromark.cjs");
|
||||
|
||||
const reversedLinkRe =
|
||||
/(^|[^\\])\(([^()]+)\)\[([^\]^][^\]]*)\](?!\()/g;
|
||||
|
|
@ -14,11 +14,19 @@ module.exports = {
|
|||
"names": [ "MD011", "no-reversed-links" ],
|
||||
"description": "Reversed link syntax",
|
||||
"tags": [ "links" ],
|
||||
"parser": "none",
|
||||
"parser": "micromark",
|
||||
"function": function MD011(params, onError) {
|
||||
const exclusions = codeBlockAndSpanRanges();
|
||||
forEachLine(lineMetadata(), (line, lineIndex, inCode, onFence) => {
|
||||
if (!inCode && !onFence) {
|
||||
const { tokens } = params.parsers.micromark;
|
||||
const codeBlockLineNumbers = new Set();
|
||||
for (const codeBlock of filterByTypes(tokens, [ "codeFenced", "codeIndented" ])) {
|
||||
addRangeToSet(codeBlockLineNumbers, codeBlock.startLine, codeBlock.endLine);
|
||||
}
|
||||
const exclusions = [];
|
||||
for (const codeText of filterByTypes(tokens, [ "codeText" ])) {
|
||||
exclusions.push(...getExclusionsForToken(params.lines, codeText));
|
||||
}
|
||||
for (const [ lineIndex, line ] of params.lines.entries()) {
|
||||
if (!codeBlockLineNumbers.has(lineIndex + 1)) {
|
||||
let match = null;
|
||||
while ((match = reversedLinkRe.exec(line)) !== null) {
|
||||
const [ reversedLink, preChar, linkText, linkDestination ] = match;
|
||||
|
|
@ -27,7 +35,7 @@ module.exports = {
|
|||
if (
|
||||
!linkText.endsWith("\\") &&
|
||||
!linkDestination.endsWith("\\") &&
|
||||
!withinAnyRange(exclusions, lineIndex, index, length)
|
||||
!withinAnyRange(exclusions, lineIndex + 1, index, length)
|
||||
) {
|
||||
addError(
|
||||
onError,
|
||||
|
|
@ -44,6 +52,6 @@ module.exports = {
|
|||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue