File tree Expand file tree Collapse file tree 3 files changed +52
-50
lines changed Expand file tree Collapse file tree 3 files changed +52
-50
lines changed Original file line number Diff line number Diff line change 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'
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 32
32
"main" : " index.js" ,
33
33
"types" : " index.d.ts" ,
34
34
"files" : [
35
+ " lib/" ,
35
36
" index.d.ts" ,
36
37
" index.js"
37
38
],
You can’t perform that action at this time.
0 commit comments