Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[blog] Lint duplicate h1 on the page #40835

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .markdownlint-cli2.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const straightQuotes = require('./packages/markdownlint-rule-mui/straight-quotes
const gitDiff = require('./packages/markdownlint-rule-mui/git-diff');
const tableAlignment = require('./packages/markdownlint-rule-mui/table-alignment');
const terminalLanguage = require('./packages/markdownlint-rule-mui/terminal-language');
const duplicateH1 = require('./packages/markdownlint-rule-mui/duplicate-h1');

// https://github.com/DavidAnson/markdownlint#rules--aliases
module.exports = {
Expand Down Expand Up @@ -35,8 +36,9 @@ module.exports = {
gitDiff: true,
tableAlignment: true,
terminalLanguage: true,
duplicateH1: true,
},
customRules: [straightQuotes, gitDiff, tableAlignment, terminalLanguage],
customRules: [straightQuotes, gitDiff, tableAlignment, terminalLanguage, duplicateH1],
ignores: [
'CHANGELOG.old.md',
'**/node_modules/**',
Expand Down
31 changes: 31 additions & 0 deletions packages/markdownlint-rule-mui/duplicate-h1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// This rule is an extension of MD025/no-multiple-top-level-headings.
// The rule is buggy https://github.com/DavidAnson/markdownlint/pull/1109
// but also blog headers don't tell you that h1 is already injected.
module.exports = {
names: ['duplicateH1'],
description: 'Multiple top-level headings in the same document.',
tags: ['headings'],
function: (params, onError) => {
let hasTopLevelHeading = false;
params.tokens.forEach((token) => {
if (token.type === 'heading_open' && token.tag === 'h1') {
// Avoid duplicate errors with MD025.
if (hasTopLevelHeading !== false && hasTopLevelHeading !== 1) {
onError({
lineNumber: token.lineNumber,
});
} else if (params.name.includes('/docs/pages/blog/')) {
onError({
lineNumber: token.lineNumber,
details: 'In the blog, the h1 is already added using the markdown header.title value.',
});
}

// Store the first h1 of the page.
if (hasTopLevelHeading === false) {
hasTopLevelHeading = token.lineNumber;
}
}
});
},
};
Loading