Update MD041/first-line-heading to avoid scanning every token with Array.filter.

This commit is contained in:
David Anson 2025-03-16 20:03:58 -07:00
parent 8baa843ce3
commit 95f82760ce

View file

@ -11,10 +11,15 @@ export default {
"parser": "micromark", "parser": "micromark",
"function": function MD041(params, onError) { "function": function MD041(params, onError) {
const level = Number(params.config.level || 1); const level = Number(params.config.level || 1);
if (!frontMatterHasTitle(params.frontMatterLines, params.config.front_matter_title)) { const { tokens } = params.parsers.micromark;
params.parsers.micromark.tokens if (
.filter((token) => !nonContentTokens.has(token.type) && !isHtmlFlowComment(token)) !frontMatterHasTitle(
.every((token) => { params.frontMatterLines,
params.config.front_matter_title
)
) {
for (const token of tokens) {
if (!nonContentTokens.has(token.type) && !isHtmlFlowComment(token)) {
let isError = true; let isError = true;
if ((token.type === "atxHeading") || (token.type === "setextHeading")) { if ((token.type === "atxHeading") || (token.type === "setextHeading")) {
isError = (getHeadingLevel(token) !== level); isError = (getHeadingLevel(token) !== level);
@ -26,8 +31,9 @@ export default {
if (isError) { if (isError) {
addErrorContext(onError, token.startLine, params.lines[token.startLine - 1]); addErrorContext(onError, token.startLine, params.lines[token.startLine - 1]);
} }
return false; break;
}); }
}
} }
} }
}; };