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");
|
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-09 16:17:50 -08:00
|
|
|
"parser": "markdownit",
|
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);
|
2018-04-27 22:05:34 -07:00
|
|
|
const tag = "h" + level;
|
2019-03-16 20:21:57 -07:00
|
|
|
const foundFrontMatterTitle =
|
2019-04-13 11:18:57 -07:00
|
|
|
frontMatterHasTitle(
|
2019-03-16 20:21:57 -07:00
|
|
|
params.frontMatterLines,
|
|
|
|
params.config.front_matter_title
|
|
|
|
);
|
2019-03-10 22:10:33 -07:00
|
|
|
if (!foundFrontMatterTitle) {
|
2021-01-31 15:48:00 -08:00
|
|
|
const htmlHeadingRe = new RegExp(`^<h${level}[ />]`, "i");
|
2023-02-18 21:41:07 -08:00
|
|
|
params.parsers.markdownit.tokens.every((token) => {
|
2021-01-31 15:48:00 -08:00
|
|
|
let isError = false;
|
2019-03-10 22:10:33 -07:00
|
|
|
if (token.type === "html_block") {
|
2021-01-31 15:48:00 -08:00
|
|
|
if (token.content.startsWith("<!--")) {
|
|
|
|
// Ignore leading HTML comments
|
|
|
|
return true;
|
|
|
|
} else if (!htmlHeadingRe.test(token.content)) {
|
|
|
|
// Something other than an HTML heading
|
|
|
|
isError = true;
|
|
|
|
}
|
|
|
|
} else if ((token.type !== "heading_open") || (token.tag !== tag)) {
|
|
|
|
// Something other than a Markdown heading
|
|
|
|
isError = true;
|
2019-03-10 22:10:33 -07:00
|
|
|
}
|
2021-01-31 15:48:00 -08:00
|
|
|
if (isError) {
|
2019-04-13 11:18:57 -07:00
|
|
|
addErrorContext(onError, token.lineNumber, token.line);
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2019-03-10 22:10:33 -07:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
};
|