Skip to content

Commit 33dda18

Browse files
authored
feat: update no-multiple-h1 rule to recognize JSON frontmatter (#413)
* feat: update `no-multiple-h1` to recognize JSON frontmatter. * wip: update documentation * wip: update rule * wip: add more testcases * wip: handle edge case 1 * wip: handle edge case 2 * wip: handle edge case 3 * wip: remove unncessary test case
1 parent bab9670 commit 33dda18

File tree

3 files changed

+362
-5
lines changed

3 files changed

+362
-5
lines changed

docs/rules/no-multiple-h1.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ An H1 heading is meant to define the main heading of a page, providing important
1010

1111
This rule warns when it finds more than one H1 heading in a Markdown document. It checks for:
1212

13-
- ATX-style headings (`# Heading`)
13+
- ATX-style headings (`# Heading` or `# Heading #`)
1414
- Setext-style headings (`Heading\n=========`)
15-
- Front matter title fields (YAML and TOML)
15+
- Front matter title fields ([YAML](https://yaml.org/), [TOML](https://toml.io/en/), and [JSON](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Scripting/JSON))
1616
- HTML h1 tags (`<h1>Heading</h1>`)
1717

1818
Examples of **incorrect** code for this rule:
@@ -46,7 +46,7 @@ Another H1 heading
4646

4747
The following options are available on this rule:
4848

49-
* `frontmatterTitle: string` - A regex pattern to match title fields in front matter. The default pattern matches both YAML (`title:`) and TOML (`title =`) formats. Set to an empty string to disable front matter title checking.
49+
* `frontmatterTitle: string` - A regex pattern to match title fields in front matter. The default pattern matches both YAML (`title:`), TOML (`title =`), and JSON (`"title":`) formats. Set to an empty string to disable front matter title checking.
5050

5151
Examples of **incorrect** code for this rule:
5252

@@ -57,7 +57,27 @@ Examples of **incorrect** code for this rule:
5757
title: My Title
5858
---
5959

60-
# Heading 1
60+
# Heading 1 with YAML front matter
61+
```
62+
63+
```markdown
64+
<!-- eslint markdown/no-multiple-h1: "error" -->
65+
66+
+++
67+
title = "My Title"
68+
+++
69+
70+
# Heading 1 with TOML front matter
71+
```
72+
73+
```markdown
74+
<!-- eslint markdown/no-multiple-h1: "error" -->
75+
76+
---
77+
{ "title": "My Title" }
78+
---
79+
80+
# Heading 1 with JSON front matter
6181
```
6282

6383
Examples of **incorrect** code when configured as `"no-multiple-h1": ["error", { frontmatterTitle: "\\s*heading\\s*[:=]" }]`:

src/rules/no-multiple-h1.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ export default {
7575
],
7676

7777
defaultOptions: [
78-
{ frontmatterTitle: "^\\s*['\"]?title['\"]?\\s*[:=]" },
78+
{
79+
frontmatterTitle:
80+
"^(?!\\s*['\"]title[:=]['\"])\\s*\\{?\\s*['\"]?title['\"]?\\s*[:=]",
81+
},
7982
],
8083
},
8184

@@ -98,6 +101,12 @@ export default {
98101
}
99102
},
100103

104+
json(node) {
105+
if (frontmatterHasTitle(node.value, titlePattern)) {
106+
h1Count++;
107+
}
108+
},
109+
101110
html(node) {
102111
let match;
103112
while ((match = h1TagPattern.exec(node.value)) !== null) {

0 commit comments

Comments
 (0)