Skip to content

Commit e643b5a

Browse files
committed
Refactor to move implementation to lib/
1 parent e28baca commit e643b5a

File tree

3 files changed

+52
-50
lines changed

3 files changed

+52
-50
lines changed

index.js

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1 @@
1-
/**
2-
* @typedef {import('hast').Root} Root
3-
* @typedef {import('hast').Content} Content
4-
*/
5-
6-
/**
7-
* @typedef {Content | Root} Node
8-
*/
9-
10-
import {headingRank} from 'hast-util-heading-rank'
11-
import {visit} from 'unist-util-visit'
12-
13-
// To do next major: don’t return node.
14-
/**
15-
* Change the rank of all headings (`h1` to `h6`) in `tree`.
16-
*
17-
* Mutates the tree.
18-
* Caps the rank so that shifting would not create invalid headings (so no `h0` or
19-
* `h7`).
20-
*
21-
* @template {Node} T
22-
* Node type.
23-
* @param {T} tree
24-
* Tree to change.
25-
* @param {number} shift
26-
* Non-null finite integer to use to shift ranks.
27-
* @returns {T}
28-
* Given, modified, tree.
29-
*/
30-
export function shiftHeading(tree, shift) {
31-
if (
32-
typeof shift !== 'number' ||
33-
!shift ||
34-
!Number.isFinite(shift) ||
35-
Math.floor(shift) !== shift
36-
) {
37-
throw new Error('Expected a non-null finite integer, not `' + shift + '`')
38-
}
39-
40-
visit(tree, 'element', (node) => {
41-
let rank = headingRank(node)
42-
43-
if (rank) {
44-
rank += shift
45-
node.tagName = 'h' + (rank > 6 ? 6 : rank < 1 ? 1 : rank)
46-
}
47-
})
48-
49-
return tree
50-
}
1+
export {shiftHeading} from './lib/index.js'

lib/index.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @typedef {import('hast').Root} Root
3+
* @typedef {import('hast').Content} Content
4+
*/
5+
6+
/**
7+
* @typedef {Content | Root} Node
8+
*/
9+
10+
import {headingRank} from 'hast-util-heading-rank'
11+
import {visit} from 'unist-util-visit'
12+
13+
// To do next major: don’t return node.
14+
/**
15+
* Change the rank of all headings (`h1` to `h6`) in `tree`.
16+
*
17+
* Mutates the tree.
18+
* Caps the rank so that shifting would not create invalid headings (so no `h0` or
19+
* `h7`).
20+
*
21+
* @template {Node} T
22+
* Node type.
23+
* @param {T} tree
24+
* Tree to change.
25+
* @param {number} shift
26+
* Non-null finite integer to use to shift ranks.
27+
* @returns {T}
28+
* Given, modified, tree.
29+
*/
30+
export function shiftHeading(tree, shift) {
31+
if (
32+
typeof shift !== 'number' ||
33+
!shift ||
34+
!Number.isFinite(shift) ||
35+
Math.floor(shift) !== shift
36+
) {
37+
throw new Error('Expected a non-null finite integer, not `' + shift + '`')
38+
}
39+
40+
visit(tree, 'element', (node) => {
41+
let rank = headingRank(node)
42+
43+
if (rank) {
44+
rank += shift
45+
node.tagName = 'h' + (rank > 6 ? 6 : rank < 1 ? 1 : rank)
46+
}
47+
})
48+
49+
return tree
50+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"main": "index.js",
3333
"types": "index.d.ts",
3434
"files": [
35+
"lib/",
3536
"index.d.ts",
3637
"index.js"
3738
],

0 commit comments

Comments
 (0)