Skip to content

Commit 2b751cf

Browse files
[blog] Lint duplicate h1 on the page (#40835)
1 parent a9f1a4c commit 2b751cf

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

.markdownlint-cli2.cjs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const straightQuotes = require('./packages/markdownlint-rule-mui/straight-quotes
22
const gitDiff = require('./packages/markdownlint-rule-mui/git-diff');
33
const tableAlignment = require('./packages/markdownlint-rule-mui/table-alignment');
44
const terminalLanguage = require('./packages/markdownlint-rule-mui/terminal-language');
5+
const duplicateH1 = require('./packages/markdownlint-rule-mui/duplicate-h1');
56

67
// https://github.com/DavidAnson/markdownlint#rules--aliases
78
module.exports = {
@@ -35,8 +36,9 @@ module.exports = {
3536
gitDiff: true,
3637
tableAlignment: true,
3738
terminalLanguage: true,
39+
duplicateH1: true,
3840
},
39-
customRules: [straightQuotes, gitDiff, tableAlignment, terminalLanguage],
41+
customRules: [straightQuotes, gitDiff, tableAlignment, terminalLanguage, duplicateH1],
4042
ignores: [
4143
'CHANGELOG.old.md',
4244
'**/node_modules/**',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// This rule is an extension of MD025/no-multiple-top-level-headings.
2+
// The rule is buggy https://github.com/DavidAnson/markdownlint/pull/1109
3+
// but also blog headers don't tell you that h1 is already injected.
4+
module.exports = {
5+
names: ['duplicateH1'],
6+
description: 'Multiple top-level headings in the same document.',
7+
tags: ['headings'],
8+
function: (params, onError) => {
9+
let hasTopLevelHeading = false;
10+
params.tokens.forEach((token) => {
11+
if (token.type === 'heading_open' && token.tag === 'h1') {
12+
// Avoid duplicate errors with MD025.
13+
if (hasTopLevelHeading !== false && hasTopLevelHeading !== 1) {
14+
onError({
15+
lineNumber: token.lineNumber,
16+
});
17+
} else if (params.name.includes('/docs/pages/blog/')) {
18+
onError({
19+
lineNumber: token.lineNumber,
20+
details: 'In the blog, the h1 is already added using the markdown header.title value.',
21+
});
22+
}
23+
24+
// Store the first h1 of the page.
25+
if (hasTopLevelHeading === false) {
26+
hasTopLevelHeading = token.lineNumber;
27+
}
28+
}
29+
});
30+
},
31+
};

0 commit comments

Comments
 (0)