mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 14:30:12 +01:00
Update MD018/MD019/MD020/MD021 to report fixInfo for violations.
This commit is contained in:
parent
c8a74bd72c
commit
316bfeadaa
8 changed files with 167 additions and 50 deletions
53
lib/md021.js
53
lib/md021.js
|
|
@ -2,24 +2,57 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
const { addErrorContext, filterTokens, headingStyleFor, rangeFromRegExp } =
|
||||
const { addErrorContext, filterTokens, headingStyleFor } =
|
||||
require("../helpers");
|
||||
|
||||
const atxClosedHeadingSpaceRe = /(?:^#+\s\s+?\S)|(?:\S\s\s+?#+\s*$)/;
|
||||
|
||||
module.exports = {
|
||||
"names": [ "MD021", "no-multiple-space-closed-atx" ],
|
||||
"description": "Multiple spaces inside hashes on closed atx style heading",
|
||||
"tags": [ "headings", "headers", "atx_closed", "spaces" ],
|
||||
"function": function MD021(params, onError) {
|
||||
filterTokens(params, "heading_open", function forToken(token) {
|
||||
filterTokens(params, "heading_open", (token) => {
|
||||
if (headingStyleFor(token) === "atx_closed") {
|
||||
const left = /^#+\s\s/.test(token.line);
|
||||
const right = /\s\s#+$/.test(token.line);
|
||||
if (left || right) {
|
||||
addErrorContext(onError, token.lineNumber, token.line.trim(),
|
||||
left, right,
|
||||
rangeFromRegExp(token.line, atxClosedHeadingSpaceRe));
|
||||
const { line, lineNumber } = token;
|
||||
const match = /^(#+)(\s+)([^#]+?)(\s+)(#+)(\s*)$/.exec(line);
|
||||
if (match) {
|
||||
const [
|
||||
,
|
||||
leftHash,
|
||||
{ "length": leftSpaceLength },
|
||||
content,
|
||||
{ "length": rightSpaceLength },
|
||||
rightHash,
|
||||
{ "length": trailSpaceLength }
|
||||
] = match;
|
||||
const left = leftSpaceLength > 1;
|
||||
const right = rightSpaceLength > 1;
|
||||
if (left || right) {
|
||||
const length = line.length;
|
||||
const leftHashLength = leftHash.length;
|
||||
const rightHashLength = rightHash.length;
|
||||
const range = left ?
|
||||
[
|
||||
1,
|
||||
leftHashLength + leftSpaceLength + 1
|
||||
] :
|
||||
[
|
||||
length - trailSpaceLength - rightHashLength - rightSpaceLength,
|
||||
rightSpaceLength + rightHashLength + 1
|
||||
];
|
||||
addErrorContext(
|
||||
onError,
|
||||
lineNumber,
|
||||
line.trim(),
|
||||
left,
|
||||
right,
|
||||
range,
|
||||
{
|
||||
"editColumn": 1,
|
||||
"deleteLength": length,
|
||||
"insertText": `${leftHash} ${content} ${rightHash}`
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue