/
footer.js
250 lines (228 loc) · 7.45 KB
/
footer.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
/**
* @typedef {import('hast').Element} Element
* @typedef {import('hast').ElementContent} ElementContent
*
* @typedef {import('./state.js').State} State
*/
/**
* @callback FootnoteBackContentTemplate
* Generate content for the backreference dynamically.
*
* For the following markdown:
*
* ```markdown
* Alpha[^micromark], bravo[^micromark], and charlie[^remark].
*
* [^remark]: things about remark
* [^micromark]: things about micromark
* ```
*
* This function will be called with:
*
* * `0` and `0` for the backreference from `things about micromark` to
* `alpha`, as it is the first used definition, and the first call to it
* * `0` and `1` for the backreference from `things about micromark` to
* `bravo`, as it is the first used definition, and the second call to it
* * `1` and `0` for the backreference from `things about remark` to
* `charlie`, as it is the second used definition
* @param {number} referenceIndex
* Index of the definition in the order that they are first referenced,
* 0-indexed.
* @param {number} rereferenceIndex
* Index of calls to the same definition, 0-indexed.
* @returns {Array<ElementContent> | ElementContent | string}
* Content for the backreference when linking back from definitions to their
* reference.
*
* @callback FootnoteBackLabelTemplate
* Generate a back label dynamically.
*
* For the following markdown:
*
* ```markdown
* Alpha[^micromark], bravo[^micromark], and charlie[^remark].
*
* [^remark]: things about remark
* [^micromark]: things about micromark
* ```
*
* This function will be called with:
*
* * `0` and `0` for the backreference from `things about micromark` to
* `alpha`, as it is the first used definition, and the first call to it
* * `0` and `1` for the backreference from `things about micromark` to
* `bravo`, as it is the first used definition, and the second call to it
* * `1` and `0` for the backreference from `things about remark` to
* `charlie`, as it is the second used definition
* @param {number} referenceIndex
* Index of the definition in the order that they are first referenced,
* 0-indexed.
* @param {number} rereferenceIndex
* Index of calls to the same definition, 0-indexed.
* @returns {string}
* Back label to use when linking back from definitions to their reference.
*/
import structuredClone from '@ungap/structured-clone'
import {normalizeUri} from 'micromark-util-sanitize-uri'
/**
* Generate the default content that GitHub uses on backreferences.
*
* @param {number} _
* Index of the definition in the order that they are first referenced,
* 0-indexed.
* @param {number} rereferenceIndex
* Index of calls to the same definition, 0-indexed.
* @returns {Array<ElementContent>}
* Content.
*/
export function defaultFootnoteBackContent(_, rereferenceIndex) {
/** @type {Array<ElementContent>} */
const result = [{type: 'text', value: '↩'}]
if (rereferenceIndex > 1) {
result.push({
type: 'element',
tagName: 'sup',
properties: {},
children: [{type: 'text', value: String(rereferenceIndex)}]
})
}
return result
}
/**
* Generate the default label that GitHub uses on backreferences.
*
* @param {number} referenceIndex
* Index of the definition in the order that they are first referenced,
* 0-indexed.
* @param {number} rereferenceIndex
* Index of calls to the same definition, 0-indexed.
* @returns {string}
* Label.
*/
export function defaultFootnoteBackLabel(referenceIndex, rereferenceIndex) {
return (
'Back to reference ' +
(referenceIndex + 1) +
(rereferenceIndex > 1 ? '-' + rereferenceIndex : '')
)
}
/**
* Generate a hast footer for called footnote definitions.
*
* @param {State} state
* Info passed around.
* @returns {Element | undefined}
* `section` element or `undefined`.
*/
// eslint-disable-next-line complexity
export function footer(state) {
const clobberPrefix =
typeof state.options.clobberPrefix === 'string'
? state.options.clobberPrefix
: 'user-content-'
const footnoteBackContent =
state.options.footnoteBackContent || defaultFootnoteBackContent
const footnoteBackLabel =
state.options.footnoteBackLabel || defaultFootnoteBackLabel
const footnoteLabel = state.options.footnoteLabel || 'Footnotes'
const footnoteLabelTagName = state.options.footnoteLabelTagName || 'h2'
const footnoteLabelProperties = state.options.footnoteLabelProperties || {
className: ['sr-only']
}
/** @type {Array<ElementContent>} */
const listItems = []
let referenceIndex = -1
while (++referenceIndex < state.footnoteOrder.length) {
const def = state.footnoteById.get(state.footnoteOrder[referenceIndex])
if (!def) {
continue
}
const content = state.all(def)
const id = String(def.identifier).toUpperCase()
const safeId = normalizeUri(id.toLowerCase())
let rereferenceIndex = 0
/** @type {Array<ElementContent>} */
const backReferences = []
const counts = state.footnoteCounts.get(id)
// eslint-disable-next-line no-unmodified-loop-condition
while (counts !== undefined && ++rereferenceIndex <= counts) {
if (backReferences.length > 0) {
backReferences.push({type: 'text', value: ' '})
}
let children =
typeof footnoteBackContent === 'string'
? footnoteBackContent
: footnoteBackContent(referenceIndex, rereferenceIndex)
if (typeof children === 'string') {
children = {type: 'text', value: children}
}
backReferences.push({
type: 'element',
tagName: 'a',
properties: {
href:
'#' +
clobberPrefix +
'fnref-' +
safeId +
(rereferenceIndex > 1 ? '-' + rereferenceIndex : ''),
dataFootnoteBackref: '',
ariaLabel:
typeof footnoteBackLabel === 'string'
? footnoteBackLabel
: footnoteBackLabel(referenceIndex, rereferenceIndex),
className: ['data-footnote-backref']
},
children: Array.isArray(children) ? children : [children]
})
}
const tail = content[content.length - 1]
if (tail && tail.type === 'element' && tail.tagName === 'p') {
const tailTail = tail.children[tail.children.length - 1]
if (tailTail && tailTail.type === 'text') {
tailTail.value += ' '
} else {
tail.children.push({type: 'text', value: ' '})
}
tail.children.push(...backReferences)
} else {
content.push(...backReferences)
}
/** @type {Element} */
const listItem = {
type: 'element',
tagName: 'li',
properties: {id: clobberPrefix + 'fn-' + safeId},
children: state.wrap(content, true)
}
state.patch(def, listItem)
listItems.push(listItem)
}
if (listItems.length === 0) {
return
}
return {
type: 'element',
tagName: 'section',
properties: {dataFootnotes: true, className: ['footnotes']},
children: [
{
type: 'element',
tagName: footnoteLabelTagName,
properties: {
...structuredClone(footnoteLabelProperties),
id: 'footnote-label'
},
children: [{type: 'text', value: footnoteLabel}]
},
{type: 'text', value: '\n'},
{
type: 'element',
tagName: 'ol',
properties: {},
children: state.wrap(listItems, true)
},
{type: 'text', value: '\n'}
]
}
}