2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
2024-11-28 20:36:44 -08:00
|
|
|
import { addErrorContext, frontMatterHasTitle } from "../helpers/helpers.cjs";
|
|
|
|
import { getHeadingLevel, getHeadingText } from "../helpers/micromark-helpers.cjs";
|
|
|
|
import { filterByTypesCached } from "./cache.mjs";
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2024-12-03 19:58:28 -08:00
|
|
|
/** @type {import("markdownlint").Rule} */
|
2024-11-28 20:36:44 -08:00
|
|
|
export default {
|
2019-03-16 20:21:57 -07:00
|
|
|
"names": [ "MD025", "single-title", "single-h1" ],
|
2020-12-28 13:28:38 -08:00
|
|
|
"description": "Multiple top-level headings in the same document",
|
2023-11-09 20:05:30 -08:00
|
|
|
"tags": [ "headings" ],
|
2024-06-08 20:41:40 -07:00
|
|
|
"parser": "micromark",
|
2018-01-21 21:44:25 -08:00
|
|
|
"function": function MD025(params, onError) {
|
2020-01-25 18:40:39 -08:00
|
|
|
const level = Number(params.config.level || 1);
|
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
|
|
|
|
);
|
2018-04-27 22:05:34 -07:00
|
|
|
let hasTopLevelHeading = false;
|
2024-08-24 22:05:16 -07:00
|
|
|
for (const heading of filterByTypesCached([ "atxHeading", "setextHeading" ])) {
|
2024-06-08 20:41:40 -07:00
|
|
|
const headingLevel = getHeadingLevel(heading);
|
2024-06-09 17:09:03 -07:00
|
|
|
if (headingLevel === level) {
|
2019-03-16 20:21:57 -07:00
|
|
|
if (hasTopLevelHeading || foundFrontMatterTitle) {
|
2024-06-19 21:05:31 -07:00
|
|
|
const headingText = getHeadingText(heading);
|
2024-06-08 20:41:40 -07:00
|
|
|
addErrorContext(
|
|
|
|
onError,
|
|
|
|
heading.startLine,
|
|
|
|
headingText
|
|
|
|
);
|
|
|
|
} else if (heading.startLine === 1) {
|
2018-01-21 21:44:25 -08:00
|
|
|
hasTopLevelHeading = true;
|
|
|
|
}
|
|
|
|
}
|
2024-06-08 20:41:40 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
};
|