2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2019-04-13 11:18:57 -07:00
|
|
|
const { addErrorDetailIf, filterTokens, headingStyleFor } =
|
|
|
|
require("../helpers");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
|
|
|
module.exports = {
|
2018-03-19 23:39:42 +01:00
|
|
|
"names": [ "MD003", "heading-style", "header-style" ],
|
|
|
|
"description": "Heading style",
|
|
|
|
"tags": [ "headings", "headers" ],
|
2018-01-21 21:44:25 -08:00
|
|
|
"function": function MD003(params, onError) {
|
2018-04-27 22:05:34 -07:00
|
|
|
let style = params.config.style || "consistent";
|
2019-04-13 11:18:57 -07:00
|
|
|
filterTokens(params, "heading_open", function forToken(token) {
|
|
|
|
const styleForToken = headingStyleFor(token);
|
2018-01-21 21:44:25 -08:00
|
|
|
if (style === "consistent") {
|
|
|
|
style = styleForToken;
|
|
|
|
}
|
|
|
|
if (styleForToken !== style) {
|
2018-04-27 22:05:34 -07:00
|
|
|
const h12 = /h[12]/.test(token.tag);
|
|
|
|
const setextWithAtx =
|
2018-01-21 21:44:25 -08:00
|
|
|
(style === "setext_with_atx") &&
|
|
|
|
((h12 && (styleForToken === "setext")) ||
|
|
|
|
(!h12 && (styleForToken === "atx")));
|
2018-04-27 22:05:34 -07:00
|
|
|
const setextWithAtxClosed =
|
2018-01-21 21:44:25 -08:00
|
|
|
(style === "setext_with_atx_closed") &&
|
|
|
|
((h12 && (styleForToken === "setext")) ||
|
|
|
|
(!h12 && (styleForToken === "atx_closed")));
|
|
|
|
if (!setextWithAtx && !setextWithAtxClosed) {
|
2018-04-27 22:05:34 -07:00
|
|
|
let expected = style;
|
2018-01-21 21:44:25 -08:00
|
|
|
if (style === "setext_with_atx") {
|
|
|
|
expected = h12 ? "setext" : "atx";
|
|
|
|
} else if (style === "setext_with_atx_closed") {
|
|
|
|
expected = h12 ? "setext" : "atx_closed";
|
|
|
|
}
|
2019-04-13 11:18:57 -07:00
|
|
|
addErrorDetailIf(onError, token.lineNumber,
|
2018-01-21 21:44:25 -08:00
|
|
|
expected, styleForToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|