/
index.js
274 lines (241 loc) · 7.52 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
/**
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast').Heading} Heading
* @typedef {import('mdast').Link} Link
* @typedef {import('mdast').Blockquote} Blockquote
* @typedef {import('mdast').BlockContent} BlockContent
* @typedef {import('mdast').StaticPhrasingContent} StaticPhrasingContent
* @typedef {import('mdast').PhrasingContent} PhrasingContent
* @typedef {import('hast').Element} Element
* @typedef {Element['children'][number]} ElementChild
* @typedef {import('hast').Properties} Properties
*
* @callback Build
* @param {Heading} node
* @returns {ElementChild|ElementChild[]}
*
* @callback BuildGroup
* @param {Heading} node
* @returns {Element}
*
* @typedef BaseOptions
* @property {Properties} [linkProperties]
* Extra properties to set on the link when injecting.
* Defaults to `{ariaHidden: true, tabIndex: -1}` when `'prepend'` or
* `'append'`.
* @property {ElementChild|ElementChild[]|Build} [content={type: 'element', tagName: 'span', properties: {className: ['icon', 'icon-link']}, children: []}]
* Nodes (hast) to insert in the link.
* @property {Element|BuildGroup} [group]
* Node (hast) to wrap the heading and link with, if `behavior` is `'before'` or
* `'after'`.
* There is no default.
*
* @typedef BeforeFields
* @property {'before'} behavior
* Insert link before the heading.
*
* @typedef AfterFields
* @property {'after'} behavior
* Insert link after the heading.
*
* @typedef WrapFields
* @property {'wrap'} behavior
* Wrap the whole heading text with the link.
*
* @typedef PrependFields
* @property {'prepend'} behavior
* Inject link before the heading text.
*
* @typedef AppendFields
* @property {'append'} behavior
* Inject link after the heading text.
*
* @typedef {BaseOptions & BeforeFields} BeforeOptions
* @typedef {BaseOptions & AfterFields} AfterOptions
* @typedef {Pick<BaseOptions, 'linkProperties'> & WrapFields} WrapOptions
* @typedef {Pick<BaseOptions, 'linkProperties' | 'content'> & PrependFields} PrependOptions
* @typedef {Pick<BaseOptions, 'linkProperties' | 'content'> & AppendFields} AppendOptions
*
* @typedef {BaseOptions|BeforeOptions|AfterOptions|WrapOptions|PrependOptions|AppendOptions} Options
*/
import {visit, SKIP} from 'unist-util-visit'
import extend from 'extend'
/** @type {Element} */
const contentDefaults = {
type: 'element',
tagName: 'span',
properties: {className: ['icon', 'icon-link']},
children: []
}
/**
* Plugin to automatically add links to headings (h1-h6).
*
* @type {import('unified').Plugin<[Options?]|void[], Root>}
*/
export default function rehypeAutolinkHeadings(options = {}) {
let props = options.linkProperties
const behavior = ('behavior' in options && options.behavior) || 'prepend'
const content = ('content' in options && options.content) || contentDefaults
const group = 'group' in options ? options.group : undefined
/** @type {import('unist-util-visit').Visitor<Heading>} */
let method
if (behavior === 'wrap') {
method = wrap
} else if (behavior === 'before' || behavior === 'after') {
method = around
} else {
if (!props) {
props = {ariaHidden: 'true', tabIndex: -1}
}
method = inject
}
return (tree) => {
visit(tree, 'heading', method)
}
/** @type {import('unist-util-visit').Visitor<Heading>} */
function inject(node) {
const url = getUrl(node)
if (url) {
const link = create(url)
link.data = Object.assign({}, link.data, {
hProperties: extend(true, {}, props),
hChildren: toChildren(content, node)
})
node.children[behavior === 'prepend' ? 'unshift' : 'push'](link)
}
}
/** @type {import('unist-util-visit').Visitor<Heading>} */
function around(node, index, parent) {
const url = getUrl(node)
if (url && typeof index === 'number' && parent) {
const link = create(url)
const grouping = group ? toGrouping(group, node) : undefined
link.data = Object.assign({}, link.data, {
hProperties: extend(true, {}, props),
hChildren: toChildren(content, node)
})
/** @type {BlockContent[]} */
// @ts-expect-error: links are not allowed, but it works fine.
let nodes = behavior === 'before' ? [link, node] : [node, link]
if (grouping) {
grouping.children = nodes
nodes = [grouping]
}
parent.children.splice(index, 1, ...nodes)
return [SKIP, index + nodes.length]
}
}
/** @type {import('unist-util-visit').Visitor<Heading>} */
function wrap(node) {
const url = getUrl(node)
if (url) {
const link = create(url, toStaticPhrasingContent(node.children))
link.data = {hProperties: extend(true, {}, props)}
node.children = [link]
}
}
/**
* @param {ElementChild|ElementChild[]|Build} value
* @param {Heading} node
* @returns {ElementChild|ElementChild[]}
*/
function toNode(value, node) {
return typeof value === 'function' ? value(node) : value
}
/**
* @param {ElementChild|ElementChild[]|Build} value
* @param {Heading} node
* @returns {ElementChild[]}
*/
function toChildren(value, node) {
const result = toNode(value, node)
const children = Array.isArray(result) ? result : [result]
return typeof value === 'function' ? children : extend(true, [], children)
}
/**
* @param {ElementChild|ElementChild[]|Build} value
* @param {Heading} node
* @returns {Blockquote}
*/
function toGrouping(value, node) {
const grouping = toNode(value, node)
if (Array.isArray(grouping) || grouping.type !== 'element') {
throw new Error('Expected element as grouping')
}
const hName = grouping.tagName
const hProperties = grouping.properties
return {
// @ts-expect-error: custom node.
type: 'heading-group',
data: {
hName,
hProperties:
typeof value === 'function'
? extend(true, {}, hProperties)
: hProperties
},
children: []
}
}
/**
* @param {string} url
* @param {StaticPhrasingContent[]} [children]
* @returns {Link}
*/
function create(url, children) {
return {
type: 'link',
url,
title: null,
children: children || []
}
}
/**
* @param {Heading} node
* @returns {string|undefined}
*/
function getUrl(node) {
const data = node.data || {}
const props = /** @type {Properties} */ (data.hProperties)
const id = props && props.id
return id ? '#' + id : undefined
}
/**
* @param {Array.<PhrasingContent>} [nodes]
* @returns {Array.<StaticPhrasingContent>}
*/
function toStaticPhrasingContent(nodes) {
/** @type {Array.<StaticPhrasingContent>} */
let result = []
let index = -1
if (nodes) {
while (++index < nodes.length) {
result = result.concat(toStaticPhrasingContentOne(nodes[index]))
}
}
return result
}
/**
* @param {PhrasingContent} node
* @returns {StaticPhrasingContent|Array.<StaticPhrasingContent>}
*/
function toStaticPhrasingContentOne(node) {
if (
node.type === 'link' ||
node.type === 'linkReference' ||
node.type === 'footnote' ||
node.type === 'footnoteReference'
) {
// @ts-expect-error Looks like a parent.
return toStaticPhrasingContent(node.children)
}
if ('children' in node) {
const {children, position, ...copy} = node
return Object.assign(extend(true, {}, copy), {
children: toStaticPhrasingContent(node.children)
})
}
const {position, ...copy} = node
return extend(true, {}, copy)
}
}