-
Notifications
You must be signed in to change notification settings - Fork 0
/
remark-initial-heading.ts
75 lines (65 loc) · 1.67 KB
/
remark-initial-heading.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import type { BlockContent, Parent } from 'mdast'
import type { Transformer } from 'unified'
import { u } from 'unist-builder'
import { SKIP, visitParents } from 'unist-util-visit-parents'
import {
isHeadingNode,
isMdxjsEsmNode,
isRootNode,
isYamlNode,
} from './type-utils.js'
const headingDepth = 2
type RemarkInitialHeadingOptions = {
headingDepth: 1 | 2 | 3 | 4 | 5
}
export default function remarkInitialHeading(
options: RemarkInitialHeadingOptions = { headingDepth },
): Transformer<Parent> {
return (tree) => {
let foundHeading = false
visitParents(
tree,
(node, _, parent) => {
if (
!foundHeading &&
isRootNode(parent) &&
!isMdxjsEsmNode(node) &&
!isYamlNode(node) &&
!isHeadingNode(node)
) {
// First real element
foundHeading = true
return true
}
if (isHeadingNode(node)) {
if (node.depth === options.headingDepth) {
foundHeading = true
}
}
return false
},
(node, ancestors) => {
const start = node
const parent: Parent | undefined = ancestors[ancestors.length - 1]
if (!parent) {
return SKIP
}
const startIndex = parent.children.indexOf(start as BlockContent)
const heading = u(
'heading',
{
depth: options.headingDepth,
data: {
hProperties: {
className: 'hidden',
},
},
},
[u('text', 'Introduction')],
)
parent.children.splice(startIndex, 0, heading)
return SKIP
},
)
}
}