Reimplement MD003/heading-style using micromark tokens.

This commit is contained in:
David Anson 2024-06-08 14:44:49 -07:00
parent e447db33c9
commit 6daaa43410
10 changed files with 132 additions and 148 deletions

View file

@ -2,8 +2,9 @@
"use strict";
const { addErrorDetailIf, filterTokens, headingStyleFor } =
require("../helpers");
const { addErrorDetailIf } = require("../helpers");
const { filterByTypes, getHeadingLevel, getHeadingStyle, inHtmlFlow } =
require("../helpers/micromark.cjs");
// eslint-disable-next-line jsdoc/valid-types
/** @type import("./markdownlint").Rule */
@ -11,16 +12,20 @@ module.exports = {
"names": [ "MD003", "heading-style" ],
"description": "Heading style",
"tags": [ "headings" ],
"parser": "markdownit",
"parser": "micromark",
"function": function MD003(params, onError) {
let style = String(params.config.style || "consistent");
filterTokens(params, "heading_open", function forToken(token) {
const styleForToken = headingStyleFor(token);
const headings = filterByTypes(
params.parsers.micromark.tokens,
[ "atxHeading", "setextHeading" ]
);
for (const heading of headings) {
const styleForToken = getHeadingStyle(heading);
if (style === "consistent") {
style = styleForToken;
}
if (styleForToken !== style) {
const h12 = /h[12]/.test(token.tag);
if ((styleForToken !== style) && !inHtmlFlow(heading)) {
const h12 = getHeadingLevel(heading) <= 2;
const setextWithAtx =
(style === "setext_with_atx") &&
((h12 && (styleForToken === "setext")) ||
@ -36,10 +41,14 @@ module.exports = {
} else if (style === "setext_with_atx_closed") {
expected = h12 ? "setext" : "atx_closed";
}
addErrorDetailIf(onError, token.lineNumber,
expected, styleForToken);
addErrorDetailIf(
onError,
heading.startLine,
expected,
styleForToken
);
}
}
});
}
}
};