/
index.js
279 lines (252 loc) · 7.62 KB
/
index.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
* @typedef {import('hast').Element} Element
* @typedef {import('hast').ElementContent} ElementContent
* @typedef {import('hast').Properties} Properties
* @typedef {import('hast').Root} Root
*
* @typedef {import('hast-util-is-element').Test} Test
*/
/**
* @typedef {'after' | 'append' | 'before' | 'prepend' | 'wrap'} Behavior
* Behavior.
*
* @callback Build
* Generate content.
* @param {Readonly<Element>} element
* Current heading.
* @returns {Array<ElementContent> | ElementContent}
* Content.
*
* @callback BuildProperties
* Generate properties.
* @param {Readonly<Element>} element
* Current heading.
* @returns {Properties}
* Properties.
*
* @typedef Options
* Configuration.
* @property {Behavior | null | undefined} [behavior='prepend']
* How to create links (default: `'prepend'`).
* @property {Readonly<ElementContent> | ReadonlyArray<ElementContent> | Build | null | undefined} [content]
* Content to insert in the link (default: if `'wrap'` then `undefined`,
* otherwise `<span class="icon icon-link"></span>`);
* if `behavior` is `'wrap'` and `Build` is passed, its result replaces the
* existing content, otherwise the content is added after existing content.
* @property {Readonly<ElementContent> | ReadonlyArray<ElementContent> | Build | null | undefined} [group]
* Content to wrap the heading and link with, if `behavior` is `'after'` or
* `'before'` (optional).
* @property {Readonly<Properties> | BuildProperties | null | undefined} [headingProperties]
* Extra properties to set on the heading (optional).
* @property {Readonly<Properties> | BuildProperties | null | undefined} [properties]
* Extra properties to set on the link when injecting (default:
* `{ariaHidden: true, tabIndex: -1}` if `'append'` or `'prepend'`, otherwise
* `undefined`).
* @property {Test | null | undefined} [test]
* Extra test for which headings are linked (optional).
*/
/**
* @template T
* Kind.
* @typedef {(
* T extends Record<any, any>
* ? {-readonly [k in keyof T]: Cloneable<T[k]>}
* : T
* )} Cloneable
* Deep clone.
*
* See: <https://github.com/microsoft/TypeScript-DOM-lib-generator/issues/1237#issuecomment-1345515448>
*/
import structuredClone from '@ungap/structured-clone'
import {headingRank} from 'hast-util-heading-rank'
import {convertElement} from 'hast-util-is-element'
import {SKIP, visit} from 'unist-util-visit'
/** @type {Element} */
const contentDefaults = {
type: 'element',
tagName: 'span',
properties: {className: ['icon', 'icon-link']},
children: []
}
/** @type {Options} */
const emptyOptions = {}
/**
* Add links from headings back to themselves.
*
* ###### Notes
*
* This plugin only applies to headings with `id`s.
* Use `rehype-slug` to generate `id`s for headings that don’t have them.
*
* Several behaviors are supported:
*
* * `'prepend'` (default) — inject link before the heading text
* * `'append'` — inject link after the heading text
* * `'wrap'` — wrap the whole heading text with the link
* * `'before'` — insert link before the heading
* * `'after'` — insert link after the heading
*
* @param {Readonly<Options> | null | undefined} [options]
* Configuration (optional).
* @returns
* Transform.
*/
export default function rehypeAutolinkHeadings(options) {
const settings = options || emptyOptions
let properties = settings.properties
const headingOroperties = settings.headingProperties
const behavior = settings.behavior || 'prepend'
const content = settings.content
const group = settings.group
const is = convertElement(settings.test)
/** @type {import('unist-util-visit').Visitor<Element>} */
let method
if (behavior === 'after' || behavior === 'before') {
method = around
} else if (behavior === 'wrap') {
method = wrap
} else {
method = inject
if (!properties) {
properties = {ariaHidden: 'true', tabIndex: -1}
}
}
/**
* Transform.
*
* @param {Root} tree
* Tree.
* @returns {undefined}
* Nothing.
*/
return function (tree) {
visit(tree, 'element', function (node, index, parent) {
if (headingRank(node) && node.properties.id && is(node, index, parent)) {
Object.assign(node.properties, toProperties(headingOroperties, node))
return method(node, index, parent)
}
})
}
/** @type {import('unist-util-visit').Visitor<Element>} */
function inject(node) {
const children = toChildren(content || contentDefaults, node)
node.children[behavior === 'prepend' ? 'unshift' : 'push'](
create(node, toProperties(properties, node), children)
)
return [SKIP]
}
/** @type {import('unist-util-visit').Visitor<Element>} */
function around(node, index, parent) {
/* c8 ignore next -- uncommon */
if (typeof index !== 'number' || !parent) return
const children = toChildren(content || contentDefaults, node)
const link = create(node, toProperties(properties, node), children)
let nodes = behavior === 'before' ? [link, node] : [node, link]
if (group) {
const grouping = toNode(group, node)
if (grouping && !Array.isArray(grouping) && grouping.type === 'element') {
grouping.children = nodes
nodes = [grouping]
}
}
parent.children.splice(index, 1, ...nodes)
return [SKIP, index + nodes.length]
}
/** @type {import('unist-util-visit').Visitor<Element>} */
function wrap(node) {
/** @type {Array<ElementContent>} */
let before = node.children
/** @type {Array<ElementContent> | ElementContent} */
let after = []
if (typeof content === 'function') {
before = []
after = content(node)
} else if (content) {
after = clone(content)
}
node.children = [
create(
node,
toProperties(properties, node),
Array.isArray(after) ? [...before, ...after] : [...before, after]
)
]
return [SKIP]
}
}
/**
* Deep clone.
*
* @template T
* Kind.
* @param {T} thing
* Thing to clone.
* @returns {Cloneable<T>}
* Cloned thing.
*/
function clone(thing) {
// Cast because it’s mutable now.
return /** @type {Cloneable<T>} */ (structuredClone(thing))
}
/**
* Create an `a`.
*
* @param {Readonly<Element>} node
* Related heading.
* @param {Properties | undefined} properties
* Properties to set on the link.
* @param {Array<ElementContent>} children
* Content.
* @returns {Element}
* Link.
*/
function create(node, properties, children) {
return {
type: 'element',
tagName: 'a',
properties: {...properties, href: '#' + node.properties.id},
children
}
}
/**
* Turn into children.
*
* @param {Readonly<ElementContent> | ReadonlyArray<ElementContent> | Build} value
* Content.
* @param {Readonly<Element>} node
* Related heading.
* @returns {Array<ElementContent>}
* Children.
*/
function toChildren(value, node) {
const result = toNode(value, node)
return Array.isArray(result) ? result : [result]
}
/**
* Turn into a node.
*
* @param {Readonly<ElementContent> | ReadonlyArray<ElementContent> | Build} value
* Content.
* @param {Readonly<Element>} node
* Related heading.
* @returns {Array<ElementContent> | ElementContent}
* Node.
*/
function toNode(value, node) {
if (typeof value === 'function') return value(node)
return clone(value)
}
/**
* Turn into properties.
*
* @param {Readonly<Properties> | BuildProperties | null | undefined} value
* Properties.
* @param {Readonly<Element>} node
* Related heading.
* @returns {Properties}
* Properties.
*/
function toProperties(value, node) {
if (typeof value === 'function') return value(node)
return value ? clone(value) : {}
}