Skip to content

feat: add frontmatterTitle option to heading-increment #454

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
62 changes: 62 additions & 0 deletions docs/rules/heading-increment.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,68 @@ Goodbye World!
#EEE Goodbye World!
```

## Options

The following options are available on this rule:

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

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

```markdown
<!-- eslint markdown/heading-increment: "error" -->

---
title: My Title
---

### Heading 3 with YAML front matter
```

```markdown
<!-- eslint markdown/heading-increment: "error" -->

+++
title = "My Title"
+++

### Heading 3 with TOML front matter
```

```markdown
<!-- eslint markdown/heading-increment: "error" -->

---
{ "title": "My Title" }
---

### Heading 3 with JSON front matter
```

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

```markdown
<!-- eslint markdown/heading-increment: ["error", { frontmatterTitle: "\\s*heading\\s*[:=]" }] -->

---
heading: My Title
---

### Heading 3
```

Examples of **correct** code when configured as `"heading-increment": ["error", { frontmatterTitle: "" }]`:

```markdown
<!-- eslint markdown/heading-increment: ["error", { frontmatterTitle: "" }] -->

---
title: My Title
---

### Heading 3
```

## When Not to Use It

If you aren't concerned with enforcing heading levels increment by one, you can safely disable this rule.
Expand Down
51 changes: 49 additions & 2 deletions src/rules/heading-increment.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@
* @author Nicholas C. Zakas
*/

//-----------------------------------------------------------------------------
// Imports
//-----------------------------------------------------------------------------

import { frontmatterHasTitle } from "../util.js";

//-----------------------------------------------------------------------------
// Type Definitions
//-----------------------------------------------------------------------------

/**
* @import { MarkdownRuleDefinition } from "../types.js";
* @typedef {"skippedHeading"} HeadingIncrementMessageIds
* @typedef {[]} HeadingIncrementOptions
* @typedef {MarkdownRuleDefinition<{ RuleOptions: HeadingIncrementOptions, MessageIds: HeadingIncrementMessageIds }>} HeadingIncrementRuleDefinition
* @typedef {[{ frontmatterTitle?: string }]} HeadingIncrementOptions
* @typedef {MarkdownRuleDefinition<{ RuleOptions: HeadingIncrementOptions, MessageIds: HeadingIncrementMessageIds }>}
* HeadingIncrementRuleDefinition
*/

//-----------------------------------------------------------------------------
Expand All @@ -33,12 +40,52 @@ export default {
skippedHeading:
"Heading level skipped from {{fromLevel}} to {{toLevel}}.",
},

schema: [
{
type: "object",
properties: {
frontmatterTitle: {
type: "string",
},
},
additionalProperties: false,
},
],

defaultOptions: [
{
frontmatterTitle:
"^(?!\\s*['\"]title[:=]['\"])\\s*\\{?\\s*['\"]?title['\"]?\\s*[:=]",
},
],
},

create(context) {
const [{ frontmatterTitle }] = context.options;
const titlePattern =
frontmatterTitle === "" ? null : new RegExp(frontmatterTitle, "iu");
let lastHeadingDepth = 0;

return {
yaml(node) {
if (frontmatterHasTitle(node.value, titlePattern)) {
lastHeadingDepth = 1;
}
},

toml(node) {
if (frontmatterHasTitle(node.value, titlePattern)) {
lastHeadingDepth = 1;
}
},

json(node) {
if (frontmatterHasTitle(node.value, titlePattern)) {
lastHeadingDepth = 1;
}
},

heading(node) {
if (lastHeadingDepth > 0 && node.depth > lastHeadingDepth + 1) {
context.report({
Expand Down
21 changes: 1 addition & 20 deletions src/rules/no-multiple-h1.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// Imports
//-----------------------------------------------------------------------------

import { findOffsets } from "../util.js";
import { findOffsets, frontmatterHasTitle } from "../util.js";
Copy link
Preview

Copilot AI Jul 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The imported frontmatterHasTitle is never used in this rule. Either integrate it to support a frontmatterTitle option or remove the unused import.

Copilot uses AI. Check for mistakes.


//-----------------------------------------------------------------------------
// Type Definitions
Expand All @@ -26,25 +26,6 @@ import { findOffsets } from "../util.js";

const h1TagPattern = /(?<!<!--[\s\S]*?)<h1[^>]*>[\s\S]*?<\/h1>/giu;

/**
* Checks if a frontmatter block contains a title matching the given pattern
* @param {string} value The frontmatter content
* @param {RegExp|null} pattern The pattern to match against
* @returns {boolean} Whether a title was found
*/
function frontmatterHasTitle(value, pattern) {
if (!pattern) {
return false;
}
const lines = value.split("\n");
for (const line of lines) {
if (pattern.test(line)) {
return true;
}
}
return false;
}

//-----------------------------------------------------------------------------
// Rule Definition
//-----------------------------------------------------------------------------
Expand Down
19 changes: 19 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,22 @@ export function findOffsets(text, offset) {
columnOffset,
};
}

/**
* Checks if a frontmatter block contains a title matching the given pattern
* @param {string} value The frontmatter content
* @param {RegExp|null} pattern The pattern to match against
* @returns {boolean} Whether a title was found
*/
export function frontmatterHasTitle(value, pattern) {
if (!pattern) {
return false;
}
const lines = value.split("\n");
for (const line of lines) {
if (pattern.test(line)) {
return true;
}
}
return false;
}
Loading