-
Notifications
You must be signed in to change notification settings - Fork 80
feat: add no-multiple-h1 rule #377
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
72c70d8
feat: add no-multiple-h1 rule
Pixel998 7c98242
add a test where the heading is inside of a code block
Pixel998 be9de5c
support front matter titles
Pixel998 9b8f13a
Merge branch 'main' into no-multiple-h1
Pixel998 c444a8e
review comments
Pixel998 d628beb
review comments
Pixel998 f052686
review comments
Pixel998 866aaba
review comments
Pixel998 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # no-multiple-h1 | ||
|
|
||
| Disallow multiple h1 headings in the same document. | ||
|
|
||
| ## Background | ||
|
|
||
| An h1 heading is meant to define the main heading of a page, providing important structural information for both users and assistive technologies. Using more than one h1 heading per page can cause confusion for screen readers, dilute SEO signals, and break the logical content hierarchy. While modern search engines are more forgiving, best practice is to use a single h1 heading to ensure clarity and accessibility. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| This rule warns when it finds more than one h1 heading in a Markdown document, whether written as ATX or Setext style. | ||
|
|
||
| Examples of incorrect code: | ||
|
|
||
| ```markdown | ||
Pixel998 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Heading 1 | ||
|
|
||
| # Another h1 heading | ||
| ``` | ||
|
|
||
| ```markdown | ||
Pixel998 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Heading 1 | ||
|
|
||
| Another h1 heading | ||
| ================== | ||
| ``` | ||
|
|
||
| ## When Not to Use It | ||
|
|
||
| If you have a specific use case that requires multiple h1 headings in a single Markdown document, you can safely disable this rule. However, this is rarely recommended. | ||
Pixel998 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Prior Art | ||
|
|
||
| * [MD025 - Multiple top-level headings in the same document](https://github.com/DavidAnson/markdownlint/blob/main/doc/md025.md) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /** | ||
| * @fileoverview Rule to enforce at most one h1 heading in Markdown. | ||
| * @author Pixel998 | ||
| */ | ||
|
|
||
| //----------------------------------------------------------------------------- | ||
| // Type Definitions | ||
| //----------------------------------------------------------------------------- | ||
|
|
||
| /** | ||
| * @typedef {import("../types.ts").MarkdownRuleDefinition<{ RuleOptions: []; }>} | ||
| * NoMultipleH1RuleDefinition | ||
| */ | ||
|
|
||
| //----------------------------------------------------------------------------- | ||
| // Rule Definition | ||
| //----------------------------------------------------------------------------- | ||
|
|
||
| /** @type {NoMultipleH1RuleDefinition} */ | ||
| export default { | ||
| meta: { | ||
| type: "problem", | ||
|
|
||
| docs: { | ||
| recommended: true, | ||
| description: "Disallow multiple h1 headings in the same document", | ||
| url: "https://github.com/eslint/markdown/blob/main/docs/rules/no-multiple-h1.md", | ||
| }, | ||
|
|
||
| messages: { | ||
| multipleH1: "More than one h1 heading found.", | ||
Pixel998 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, | ||
| }, | ||
|
|
||
| create(context) { | ||
| let h1Count = 0; | ||
|
|
||
| return { | ||
| heading(node) { | ||
| if (node.depth === 1) { | ||
| h1Count++; | ||
| if (h1Count > 1) { | ||
| context.report({ | ||
| loc: node.position, | ||
| messageId: "multipleH1", | ||
| }); | ||
| } | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /** | ||
| * @fileoverview Tests for no-multiple-h1 rule. | ||
| * @author Pixel998 | ||
| */ | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // Imports | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| import rule from "../../src/rules/no-multiple-h1.js"; | ||
| import markdown from "../../src/index.js"; | ||
| import { RuleTester } from "eslint"; | ||
| import dedent from "dedent"; | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // Tests | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| const ruleTester = new RuleTester({ | ||
| plugins: { | ||
| markdown, | ||
| }, | ||
| language: "markdown/commonmark", | ||
| }); | ||
|
|
||
| ruleTester.run("no-multiple-h1", rule, { | ||
| valid: [ | ||
| "# Only one h1", | ||
| dedent` | ||
| # Heading 1 | ||
| Text | ||
| `, | ||
| dedent` | ||
| # Heading 1 | ||
| ## Heading 2 | ||
| `, | ||
| dedent` | ||
| Only one h1 | ||
| =========== | ||
| `, | ||
| dedent` | ||
| Heading 1 | ||
| ========== | ||
| Text | ||
| `, | ||
| dedent` | ||
Pixel998 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Heading 1 | ||
| ========== | ||
| Heading 2 | ||
| ---------- | ||
| `, | ||
| dedent` | ||
| # Heading 1 | ||
| \`\`\`markdown | ||
| # Heading 1-2 | ||
| \`\`\` | ||
| `, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: dedent` | ||
| # Heading 1 | ||
| # Another H1 | ||
| `, | ||
| errors: [ | ||
| { | ||
| messageId: "multipleH1", | ||
| line: 2, | ||
| column: 1, | ||
| endLine: 2, | ||
| endColumn: 13, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: dedent` | ||
| # Heading 1 | ||
| Another H1 | ||
| ========== | ||
| `, | ||
| errors: [ | ||
| { | ||
| messageId: "multipleH1", | ||
| line: 2, | ||
| column: 1, | ||
| endLine: 3, | ||
| endColumn: 11, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: dedent` | ||
| Another H1 | ||
| ========== | ||
| # Heading 1 | ||
| `, | ||
| errors: [ | ||
| { | ||
| messageId: "multipleH1", | ||
| line: 3, | ||
| column: 1, | ||
| endLine: 3, | ||
| endColumn: 12, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); | ||
Pixel998 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.