@@ -29,21 +29,30 @@ def call
29
29
# Sanitization stripped off the section wrapper - add it back in
30
30
first_footnote . parent . wrap ( '<section class="footnotes">' )
31
31
rand_suffix = "-#{ random_number } "
32
+ modified_footnotes = { }
32
33
33
34
doc . css ( 'sup > a[id]' ) . each do |link_node |
34
35
ref_num = link_node [ :id ] . delete_prefix ( FOOTNOTE_LINK_ID_PREFIX )
35
36
footnote_node = doc . at_css ( "li[id=#{ fn_id ( ref_num ) } ]" )
36
- backref_node = footnote_node . at_css ( "a[href=\" ##{ fnref_id ( ref_num ) } \" ]" )
37
37
38
- if ref_num =~ INTEGER_PATTERN && footnote_node && backref_node
39
- link_node [ :href ] += rand_suffix
40
- link_node [ :id ] += rand_suffix
41
- footnote_node [ :id ] += rand_suffix
42
- backref_node [ :href ] += rand_suffix
38
+ if INTEGER_PATTERN . match? ( ref_num ) && ( footnote_node || modified_footnotes [ ref_num ] )
39
+ link_node [ :href ] += rand_suffix
40
+ link_node [ :id ] += rand_suffix
43
41
44
42
# Sanitization stripped off class - add it back in
45
43
link_node . parent . append_class ( 'footnote-ref' )
46
- backref_node . append_class ( 'footnote-backref' )
44
+
45
+ unless modified_footnotes [ ref_num ]
46
+ footnote_node [ :id ] += rand_suffix
47
+ backref_node = footnote_node . at_css ( "a[href=\" ##{ fnref_id ( ref_num ) } \" ]" )
48
+
49
+ if backref_node
50
+ backref_node [ :href ] += rand_suffix
51
+ backref_node . append_class ( 'footnote-backref' )
52
+ end
53
+
54
+ modified_footnotes [ ref_num ] = true
55
+ end
47
56
end
48
57
end
49
58
0 commit comments