1
+ /**
2
+ * @typedef {import('mdast').BlockContent } BlockContent
3
+ * @typedef {import('mdast').FootnoteDefinition } FootnoteDefinition
4
+ * @typedef {import('mdast').Link } Link
5
+ * @typedef {import('mdast').ListItem } ListItem
6
+ * @typedef {import('mdast').Paragraph } Paragraph
7
+ * @typedef {import('./index.js').H } H
8
+ */
9
+
1
10
import { thematicBreak } from './handlers/thematic-break.js'
2
11
import { list } from './handlers/list.js'
3
12
import { wrap } from './wrap.js'
4
13
14
+ /**
15
+ * @param {H } h
16
+ */
5
17
export function footer ( h ) {
6
18
var footnoteById = h . footnoteById
7
19
var footnoteOrder = h . footnoteOrder
8
- var length = footnoteOrder . length
9
20
var index = - 1
21
+ /** @type {Array.<ListItem> } */
10
22
var listItems = [ ]
23
+ /** @type {FootnoteDefinition } */
11
24
var def
25
+ /** @type {Link } */
12
26
var backReference
13
- var content
27
+ /** @type { Paragraph } */
14
28
var tail
29
+ /** @type {Array.<BlockContent> } */
30
+ var content
15
31
16
- while ( ++ index < length ) {
32
+ while ( ++ index < footnoteOrder . length ) {
17
33
def = footnoteById [ footnoteOrder [ index ] . toUpperCase ( ) ]
18
34
19
35
if ( ! def ) {
20
36
continue
21
37
}
22
38
23
- content = def . children . concat ( )
24
- tail = content [ content . length - 1 ]
39
+ content = [ ...def . children ]
25
40
backReference = {
26
41
type : 'link' ,
27
42
url : '#fnref-' + def . identifier ,
28
43
data : { hProperties : { className : [ 'footnote-backref' ] } } ,
29
44
children : [ { type : 'text' , value : '↩' } ]
30
45
}
31
46
32
- if ( ! tail || tail . type !== 'paragraph' ) {
47
+ if (
48
+ content [ content . length - 1 ] &&
49
+ content [ content . length - 1 ] . type === 'paragraph'
50
+ ) {
51
+ // @ts -ignore it’s a paragraph, TypeScript...
52
+ tail = content [ content . length - 1 ]
53
+ } else {
33
54
tail = { type : 'paragraph' , children : [ ] }
34
55
content . push ( tail )
35
56
}
@@ -53,10 +74,10 @@ export function footer(h) {
53
74
'div' ,
54
75
{ className : [ 'footnotes' ] } ,
55
76
wrap (
56
- [
77
+ [ ] . concat (
57
78
thematicBreak ( h ) ,
58
79
list ( h , { type : 'list' , ordered : true , children : listItems } )
59
- ] ,
80
+ ) ,
60
81
true
61
82
)
62
83
)
0 commit comments