File tree 3 files changed +57
-55
lines changed
3 files changed +57
-55
lines changed Original file line number Diff line number Diff line change 1
- /**
2
- * @typedef {import('mdast').Root|import('mdast').Content } Node
3
- * @typedef {import('mdast').Heading } Heading
4
- */
5
-
6
- import { visit } from 'unist-util-visit'
7
-
8
- const max = 6
9
-
10
- /**
11
- * Make sure that there is only one top-level heading in the document by
12
- * adjusting headings depths accordingly.
13
- *
14
- * @template {Node} T
15
- * @param {T } tree
16
- * @returns {T }
17
- */
18
- export function normalizeHeadings ( tree ) {
19
- /** @type {boolean|undefined } */
20
- let multiple
21
- /** @type {Heading|undefined } */
22
- let first
23
- /** @type {Heading|undefined } */
24
- let title
25
-
26
- visit ( tree , 'heading' , ( node ) => {
27
- if ( ! first ) {
28
- first = node
29
- }
30
-
31
- if ( node . depth === 1 ) {
32
- if ( title ) {
33
- multiple = true
34
- } else {
35
- title = node
36
- }
37
- }
38
- } )
39
-
40
- // If there are no titles, but there is a first heading.
41
- if ( ! title && first ) {
42
- first . depth = 1
43
- }
44
-
45
- // If there are multiple titles.
46
- if ( multiple ) {
47
- visit ( tree , 'heading' , ( node ) => {
48
- if ( node !== title && node . depth < max ) {
49
- node . depth ++
50
- }
51
- } )
52
- }
53
-
54
- return tree
55
- }
1
+ export { normalizeHeadings } from './lib/index.js'
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @typedef {import('mdast').Root|import('mdast').Content } Node
3
+ * @typedef {import('mdast').Heading } Heading
4
+ */
5
+
6
+ import { visit } from 'unist-util-visit'
7
+
8
+ const max = 6
9
+
10
+ /**
11
+ * Make sure that there is only one top-level heading in the document by
12
+ * adjusting headings depths accordingly.
13
+ *
14
+ * @template {Node} T
15
+ * @param {T } tree
16
+ * @returns {T }
17
+ */
18
+ export function normalizeHeadings ( tree ) {
19
+ /** @type {boolean|undefined } */
20
+ let multiple
21
+ /** @type {Heading|undefined } */
22
+ let first
23
+ /** @type {Heading|undefined } */
24
+ let title
25
+
26
+ visit ( tree , 'heading' , ( node ) => {
27
+ if ( ! first ) {
28
+ first = node
29
+ }
30
+
31
+ if ( node . depth === 1 ) {
32
+ if ( title ) {
33
+ multiple = true
34
+ } else {
35
+ title = node
36
+ }
37
+ }
38
+ } )
39
+
40
+ // If there are no titles, but there is a first heading.
41
+ if ( ! title && first ) {
42
+ first . depth = 1
43
+ }
44
+
45
+ // If there are multiple titles.
46
+ if ( multiple ) {
47
+ visit ( tree , 'heading' , ( node ) => {
48
+ if ( node !== title && node . depth < max ) {
49
+ node . depth ++
50
+ }
51
+ } )
52
+ }
53
+
54
+ return tree
55
+ }
Original file line number Diff line number Diff line change 33
33
"main" : " index.js" ,
34
34
"types" : " index.d.ts" ,
35
35
"files" : [
36
+ " lib/" ,
36
37
" index.d.ts" ,
37
38
" index.js"
38
39
],
You can’t perform that action at this time.
0 commit comments