mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-21 08:20:12 +01:00
Convert markdownlint library to an ECMAScript module, replace markdownlint-micromark with micromark, stop publishing (large) markdownlint-browser.js, see https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c for guidance.
This commit is contained in:
parent
191226f070
commit
1e71f6f44e
140 changed files with 1087 additions and 10428 deletions
78
lib/md010.mjs
Normal file
78
lib/md010.mjs
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
// @ts-check
|
||||
|
||||
import { addError, hasOverlap } from "../helpers/helpers.cjs";
|
||||
import { getDescendantsByType } from "../helpers/micromark-helpers.cjs";
|
||||
import { filterByTypesCached } from "./cache.mjs";
|
||||
|
||||
const tabRe = /\t+/g;
|
||||
|
||||
/** @type {import("./markdownlint.mjs").Rule} */
|
||||
export default {
|
||||
"names": [ "MD010", "no-hard-tabs" ],
|
||||
"description": "Hard tabs",
|
||||
"tags": [ "whitespace", "hard_tab" ],
|
||||
"parser": "micromark",
|
||||
"function": function MD010(params, onError) {
|
||||
const codeBlocks = params.config.code_blocks;
|
||||
const includeCode = (codeBlocks === undefined) ? true : !!codeBlocks;
|
||||
const ignoreCodeLanguages = new Set(
|
||||
(params.config.ignore_code_languages || [])
|
||||
.map((language) => language.toLowerCase())
|
||||
);
|
||||
const spacesPerTab = params.config.spaces_per_tab;
|
||||
const spaceMultiplier = (spacesPerTab === undefined) ?
|
||||
1 :
|
||||
Math.max(0, Number(spacesPerTab));
|
||||
/** @type {import("../helpers/micromark-helpers.cjs").TokenType[]} */
|
||||
const exclusionTypes = [];
|
||||
if (includeCode) {
|
||||
if (ignoreCodeLanguages.size > 0) {
|
||||
exclusionTypes.push("codeFenced");
|
||||
}
|
||||
} else {
|
||||
exclusionTypes.push("codeFenced", "codeIndented", "codeText");
|
||||
}
|
||||
const codeTokens = filterByTypesCached(exclusionTypes).filter((token) => {
|
||||
if ((token.type === "codeFenced") && (ignoreCodeLanguages.size > 0)) {
|
||||
const fenceInfos = getDescendantsByType(token, [ "codeFencedFence", "codeFencedFenceInfo" ]);
|
||||
return fenceInfos.every((fenceInfo) => ignoreCodeLanguages.has(fenceInfo.text.toLowerCase()));
|
||||
}
|
||||
return true;
|
||||
});
|
||||
const codeRanges = codeTokens.map((token) => {
|
||||
const { type, startLine, startColumn, endLine, endColumn } = token;
|
||||
const codeFenced = (type === "codeFenced");
|
||||
return {
|
||||
"startLine": startLine + (codeFenced ? 1 : 0),
|
||||
"startColumn": codeFenced ? 0 : startColumn,
|
||||
"endLine": endLine - (codeFenced ? 1 : 0),
|
||||
"endColumn": codeFenced ? Number.MAX_SAFE_INTEGER : endColumn
|
||||
};
|
||||
});
|
||||
for (let lineIndex = 0; lineIndex < params.lines.length; lineIndex++) {
|
||||
const line = params.lines[lineIndex];
|
||||
let match = null;
|
||||
while ((match = tabRe.exec(line)) !== null) {
|
||||
const lineNumber = lineIndex + 1;
|
||||
const column = match.index + 1;
|
||||
const length = match[0].length;
|
||||
/** @type {import("../helpers/helpers.cjs").FileRange} */
|
||||
const range = { "startLine": lineNumber, "startColumn": column, "endLine": lineNumber, "endColumn": column + length - 1 };
|
||||
if (!codeRanges.some((codeRange) => hasOverlap(codeRange, range))) {
|
||||
addError(
|
||||
onError,
|
||||
lineNumber,
|
||||
"Column: " + column,
|
||||
undefined,
|
||||
[ column, length ],
|
||||
{
|
||||
"editColumn": column,
|
||||
"deleteCount": length,
|
||||
"insertText": "".padEnd(length * spaceMultiplier)
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue