Skip to content

Commit

Permalink
[blog] Lint duplicate h1 on the page (mui#40835)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari authored Jan 29, 2024
1 parent a9f1a4c commit 2b751cf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
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;
}
}
});
},
};

0 comments on commit 2b751cf

Please sign in to comment.