-
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
no-duplicate-headings-in-section
rule
This new rule warns when headings exist on the same level, in the same section. This rule is not yet in any preset. Closes GH-84.
- Loading branch information
Showing
3 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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
77 changes: 77 additions & 0 deletions
77
packages/remark-lint/lib/rules/no-duplicate-headings-in-section.js
This file contains 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,77 @@ | ||
/** | ||
* @author Titus Wormer | ||
* @copyright 2015 Titus Wormer | ||
* @license MIT | ||
* @module no-duplicate-headings-in-section | ||
* @fileoverview | ||
* Warn when duplicate headings are found, | ||
* but only when on the same level, “in” | ||
* the same section. | ||
* | ||
* @example {"name": "valid.md"} | ||
* | ||
* ## Alpha | ||
* | ||
* ### Bravo | ||
* | ||
* ## Charlie | ||
* | ||
* ### Bravo | ||
* | ||
* ### Delta | ||
* | ||
* #### Bravo | ||
* | ||
* #### Echo | ||
* | ||
* ##### Bravo | ||
* | ||
* @example {"name": "invalid.md", "label": "input"} | ||
* | ||
* ## Foxtrot | ||
* | ||
* ### Golf | ||
* | ||
* ### Golf | ||
* | ||
* @example {"name": "invalid.md", "label": "output"} | ||
* | ||
* 5:1-5:9: Do not use headings with similar content per section (3:1) | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/* Dependencies. */ | ||
var position = require('unist-util-position'); | ||
var visit = require('unist-util-visit'); | ||
var toString = require('mdast-util-to-string'); | ||
|
||
/* Expose. */ | ||
module.exports = noDuplicateHeadingsInSection; | ||
|
||
/* Warn when headings with equal content are found in | ||
* a section. Case-insensitive. */ | ||
function noDuplicateHeadingsInSection(tree, file) { | ||
var stack = [{}]; | ||
|
||
visit(tree, 'heading', function (node) { | ||
var depth = node.depth; | ||
var siblings = stack[depth - 1] || {}; | ||
var value = toString(node).toUpperCase(); | ||
var duplicate = siblings[value]; | ||
var pos; | ||
|
||
stack = stack.slice(0, depth); | ||
stack[depth] = {}; | ||
siblings[value] = node; | ||
|
||
if (!position.generated(node) && duplicate && duplicate.type === 'heading') { | ||
pos = position.start(duplicate); | ||
file.message( | ||
'Do not use headings with similar content per section (' + | ||
pos.line + ':' + pos.column + ')', | ||
node | ||
); | ||
} | ||
}); | ||
} |