mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10:13 +01:00
Reimplement MD023/heading-start-left using micromark tokens.
This commit is contained in:
parent
26a0084515
commit
3b9a7fb2dd
5 changed files with 70 additions and 85 deletions
48
lib/md023.js
48
lib/md023.js
|
|
@ -2,9 +2,8 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
const { addErrorContext, filterTokens } = require("../helpers");
|
||||
|
||||
const spaceBeforeHeadingRe = /^(\s+|[>\s]+\s\s)[^>\s]/;
|
||||
const { addErrorContext } = require("../helpers");
|
||||
const { filterByTypes } = require("../helpers/micromark.cjs");
|
||||
|
||||
// eslint-disable-next-line jsdoc/valid-types
|
||||
/** @type import("./markdownlint").Rule */
|
||||
|
|
@ -12,30 +11,33 @@ module.exports = {
|
|||
"names": [ "MD023", "heading-start-left" ],
|
||||
"description": "Headings must start at the beginning of the line",
|
||||
"tags": [ "headings", "spaces" ],
|
||||
"parser": "markdownit",
|
||||
"parser": "micromark",
|
||||
"function": function MD023(params, onError) {
|
||||
filterTokens(params, "heading_open", function forToken(token) {
|
||||
const { lineNumber, line } = token;
|
||||
const match = line.match(spaceBeforeHeadingRe);
|
||||
if (match) {
|
||||
const [ prefixAndFirstChar, prefix ] = match;
|
||||
let deleteCount = prefix.length;
|
||||
const prefixLengthNoSpace = prefix.trimEnd().length;
|
||||
if (prefixLengthNoSpace) {
|
||||
deleteCount -= prefixLengthNoSpace - 1;
|
||||
}
|
||||
const headings = filterByTypes(
|
||||
params.parsers.micromark.tokens,
|
||||
[ "atxHeading", "linePrefix", "setextHeading" ]
|
||||
);
|
||||
for (let i = 0; i < headings.length - 1; i++) {
|
||||
if (
|
||||
(headings[i].type === "linePrefix") &&
|
||||
(headings[i + 1].type !== "linePrefix") &&
|
||||
(headings[i].startLine === headings[i + 1].startLine)
|
||||
) {
|
||||
const { endColumn, startColumn, startLine } = headings[i];
|
||||
const length = endColumn - startColumn;
|
||||
addErrorContext(
|
||||
onError,
|
||||
lineNumber,
|
||||
line,
|
||||
null,
|
||||
null,
|
||||
[ 1, prefixAndFirstChar.length ],
|
||||
startLine,
|
||||
params.lines[startLine - 1],
|
||||
true,
|
||||
false,
|
||||
[ startColumn, length ],
|
||||
{
|
||||
"editColumn": prefixLengthNoSpace + 1,
|
||||
"deleteCount": deleteCount
|
||||
});
|
||||
"editColumn": startColumn,
|
||||
"deleteCount": length
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue