mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 14:00:13 +01:00
Update MD025/single-title to ignore preceding blank lines and comments when looking for a title (top-level heading) (fixes #1420).
This commit is contained in:
parent
cb39972d9b
commit
5fc91f6977
6 changed files with 123 additions and 15 deletions
|
|
@ -1,7 +1,7 @@
|
|||
// @ts-check
|
||||
|
||||
import { addErrorContext, frontMatterHasTitle } from "../helpers/helpers.cjs";
|
||||
import { getHeadingLevel, getHeadingText } from "../helpers/micromark-helpers.cjs";
|
||||
import { getHeadingLevel, getHeadingText, isHtmlFlowComment, nonContentTokens } from "../helpers/micromark-helpers.cjs";
|
||||
import { filterByTypesCached } from "./cache.mjs";
|
||||
|
||||
/** @type {import("markdownlint").Rule} */
|
||||
|
|
@ -12,24 +12,32 @@ export default {
|
|||
"parser": "micromark",
|
||||
"function": function MD025(params, onError) {
|
||||
const level = Number(params.config.level || 1);
|
||||
const foundFrontMatterTitle =
|
||||
frontMatterHasTitle(
|
||||
params.frontMatterLines,
|
||||
params.config.front_matter_title
|
||||
);
|
||||
let hasTopLevelHeading = false;
|
||||
for (const heading of filterByTypesCached([ "atxHeading", "setextHeading" ])) {
|
||||
const headingLevel = getHeadingLevel(heading);
|
||||
if (headingLevel === level) {
|
||||
if (hasTopLevelHeading || foundFrontMatterTitle) {
|
||||
const headingText = getHeadingText(heading);
|
||||
const { tokens } = params.parsers.micromark;
|
||||
const matchingHeadings = filterByTypesCached([ "atxHeading", "setextHeading" ])
|
||||
.filter((heading) => level === getHeadingLevel(heading));
|
||||
if (matchingHeadings.length > 0) {
|
||||
const foundFrontMatterTitle =
|
||||
frontMatterHasTitle(
|
||||
params.frontMatterLines,
|
||||
params.config.front_matter_title
|
||||
);
|
||||
// Front matter title counts as a top-level heading if present
|
||||
let hasTopLevelHeading = foundFrontMatterTitle;
|
||||
if (!hasTopLevelHeading) {
|
||||
// Check if the first matching heading is a top-level heading
|
||||
const previousTokens = tokens.slice(0, tokens.indexOf(matchingHeadings[0]));
|
||||
hasTopLevelHeading = previousTokens.every(
|
||||
(token) => nonContentTokens.has(token.type) || isHtmlFlowComment(token)
|
||||
);
|
||||
}
|
||||
if (hasTopLevelHeading) {
|
||||
// All other matching headings are violations
|
||||
for (const heading of matchingHeadings.slice(foundFrontMatterTitle ? 0 : 1)) {
|
||||
addErrorContext(
|
||||
onError,
|
||||
heading.startLine,
|
||||
headingText
|
||||
getHeadingText(heading)
|
||||
);
|
||||
} else if (heading.startLine === 1) {
|
||||
hasTopLevelHeading = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue