Skip to content

Commit c1922ad

Browse files
committed
Refactor to use hast-util-heading-rank
1 parent aa1a4b1 commit c1922ad

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

index.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
var headingRank = require('hast-util-heading-rank')
34
var visit = require('unist-util-visit')
45

56
module.exports = shiftHeading
@@ -14,23 +15,15 @@ function shiftHeading(tree, shift) {
1415
throw new Error('Expected a non-null finite integer, not `' + shift + '`')
1516
}
1617

17-
visit(tree, visitor)
18+
visit(tree, 'element', visitor)
1819

1920
return tree
2021

2122
function visitor(node) {
22-
var name = node.tagName
23-
var rank
23+
var rank = headingRank(node)
2424

25-
if (
26-
name === 'h1' ||
27-
name === 'h2' ||
28-
name === 'h3' ||
29-
name === 'h4' ||
30-
name === 'h5' ||
31-
name === 'h6'
32-
) {
33-
rank = name.charCodeAt(1) - 48 /* `0` */ + shift
25+
if (rank) {
26+
rank += shift
3427
node.tagName = 'h' + (rank > 6 ? 6 : rank < 1 ? 1 : rank)
3528
}
3629
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"index.js"
3232
],
3333
"dependencies": {
34+
"hast-util-heading-rank": "^1.0.0",
3435
"unist-util-visit": "^2.0.0"
3536
},
3637
"devDependencies": {

0 commit comments

Comments
 (0)