2019-01-05 02:54:02 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Banzai
|
|
|
|
module Filter
|
|
|
|
# HTML Filter for footnotes
|
|
|
|
#
|
|
|
|
# Footnotes are supported in CommonMark. However we were stripping
|
|
|
|
# the ids during sanitization. Those are now allowed.
|
|
|
|
#
|
|
|
|
# Footnotes are numbered the same - the first one has `id=fn1`, the
|
|
|
|
# second is `id=fn2`, etc. In order to allow footnotes when rendering
|
|
|
|
# multiple markdown blocks on a page, we need to make each footnote
|
|
|
|
# reference unique.
|
|
|
|
#
|
|
|
|
# This filter adds a random number to each footnote (the same number
|
|
|
|
# can be used for a single render). So you get `id=fn1-4335` and `id=fn2-4335`.
|
|
|
|
#
|
|
|
|
class FootnoteFilter < HTML::Pipeline::Filter
|
2019-01-17 03:13:44 +00:00
|
|
|
INTEGER_PATTERN = /\A\d+\z/.freeze
|
2019-09-10 08:11:43 +00:00
|
|
|
FOOTNOTE_ID_PREFIX = 'fn'
|
|
|
|
FOOTNOTE_LINK_ID_PREFIX = 'fnref'
|
2019-01-17 03:13:44 +00:00
|
|
|
FOOTNOTE_LI_REFERENCE_PATTERN = /\A#{FOOTNOTE_ID_PREFIX}\d+\z/.freeze
|
|
|
|
FOOTNOTE_LINK_REFERENCE_PATTERN = /\A#{FOOTNOTE_LINK_ID_PREFIX}\d+\z/.freeze
|
|
|
|
FOOTNOTE_START_NUMBER = 1
|
2019-01-05 02:54:02 +00:00
|
|
|
|
|
|
|
def call
|
2019-01-17 03:13:44 +00:00
|
|
|
return doc unless first_footnote = doc.at_css("ol > li[id=#{fn_id(FOOTNOTE_START_NUMBER)}]")
|
2019-01-05 02:54:02 +00:00
|
|
|
|
|
|
|
# Sanitization stripped off the section wrapper - add it back in
|
|
|
|
first_footnote.parent.wrap('<section class="footnotes">')
|
2019-01-10 23:28:44 +00:00
|
|
|
rand_suffix = "-#{random_number}"
|
2019-02-13 16:13:42 +00:00
|
|
|
modified_footnotes = {}
|
2019-01-05 02:54:02 +00:00
|
|
|
|
|
|
|
doc.css('sup > a[id]').each do |link_node|
|
2019-01-17 03:13:44 +00:00
|
|
|
ref_num = link_node[:id].delete_prefix(FOOTNOTE_LINK_ID_PREFIX)
|
|
|
|
footnote_node = doc.at_css("li[id=#{fn_id(ref_num)}]")
|
2019-01-05 02:54:02 +00:00
|
|
|
|
2019-02-13 16:13:42 +00:00
|
|
|
if INTEGER_PATTERN.match?(ref_num) && (footnote_node || modified_footnotes[ref_num])
|
|
|
|
link_node[:href] += rand_suffix
|
|
|
|
link_node[:id] += rand_suffix
|
2019-01-05 02:54:02 +00:00
|
|
|
|
|
|
|
# Sanitization stripped off class - add it back in
|
|
|
|
link_node.parent.append_class('footnote-ref')
|
2019-02-13 16:13:42 +00:00
|
|
|
|
|
|
|
unless modified_footnotes[ref_num]
|
|
|
|
footnote_node[:id] += rand_suffix
|
|
|
|
backref_node = footnote_node.at_css("a[href=\"##{fnref_id(ref_num)}\"]")
|
|
|
|
|
|
|
|
if backref_node
|
|
|
|
backref_node[:href] += rand_suffix
|
|
|
|
backref_node.append_class('footnote-backref')
|
|
|
|
end
|
|
|
|
|
|
|
|
modified_footnotes[ref_num] = true
|
|
|
|
end
|
2019-01-05 02:54:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
doc
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def random_number
|
|
|
|
@random_number ||= rand(10000)
|
|
|
|
end
|
2019-01-17 03:13:44 +00:00
|
|
|
|
|
|
|
def fn_id(num)
|
|
|
|
"#{FOOTNOTE_ID_PREFIX}#{num}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def fnref_id(num)
|
|
|
|
"#{FOOTNOTE_LINK_ID_PREFIX}#{num}"
|
|
|
|
end
|
2019-01-05 02:54:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|