2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2019-04-13 11:18:57 -07:00
|
|
|
const { addErrorContext, frontMatterHasTitle } = require("../helpers");
|
2024-03-18 20:48:22 -07:00
|
|
|
const { filterByTypes, getHeadingLevel, getHtmlTagInfo, isHtmlFlowComment, nonContentTokens } =
|
|
|
|
require("../helpers/micromark.cjs");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2024-02-27 20:42:09 -08:00
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
/** @type import("./markdownlint").Rule */
|
2018-01-21 21:44:25 -08:00
|
|
|
module.exports = {
|
2019-03-13 21:39:15 -07:00
|
|
|
"names": [ "MD041", "first-line-heading", "first-line-h1" ],
|
2020-12-28 13:28:38 -08:00
|
|
|
"description": "First line in a file should be a top-level heading",
|
2023-11-09 20:05:30 -08:00
|
|
|
"tags": [ "headings" ],
|
2024-03-18 20:48:22 -07:00
|
|
|
"parser": "micromark",
|
2018-01-21 21:44:25 -08:00
|
|
|
"function": function MD041(params, onError) {
|
2020-01-25 18:40:39 -08:00
|
|
|
const level = Number(params.config.level || 1);
|
2024-03-18 20:48:22 -07:00
|
|
|
if (!frontMatterHasTitle(params.frontMatterLines, params.config.front_matter_title)) {
|
|
|
|
params.parsers.micromark.tokens.
|
|
|
|
filter((token) => !nonContentTokens.has(token.type) && !isHtmlFlowComment(token)).
|
|
|
|
every((token) => {
|
|
|
|
let isError = true;
|
|
|
|
if ((token.type === "atxHeading") || (token.type === "setextHeading")) {
|
|
|
|
isError = (getHeadingLevel(token) !== level);
|
|
|
|
} else if (token.type === "htmlFlow") {
|
|
|
|
const htmlTexts = filterByTypes(token.children, [ "htmlText" ]);
|
|
|
|
const tagInfo = (htmlTexts.length > 0) && getHtmlTagInfo(htmlTexts[0]);
|
|
|
|
isError = !tagInfo || (tagInfo.name.toLowerCase() !== `h${level}`);
|
2021-01-31 15:48:00 -08:00
|
|
|
}
|
2024-03-18 20:48:22 -07:00
|
|
|
if (isError) {
|
|
|
|
addErrorContext(onError, token.startLine, params.lines[token.startLine - 1]);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
2019-03-10 22:10:33 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
};
|