diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-01-20 12:16:11 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-01-20 12:16:11 +0300 |
commit | edaa33dee2ff2f7ea3fac488d41558eb5f86d68c (patch) | |
tree | 11f143effbfeba52329fb7afbd05e6e2a3790241 /lib/banzai/filter/footnote_filter.rb | |
parent | d8a5691316400a0f7ec4f83832698f1988eb27c1 (diff) |
Add latest changes from gitlab-org/gitlab@14-7-stable-eev14.7.0-rc42
Diffstat (limited to 'lib/banzai/filter/footnote_filter.rb')
-rw-r--r-- | lib/banzai/filter/footnote_filter.rb | 62 |
1 files changed, 16 insertions, 46 deletions
diff --git a/lib/banzai/filter/footnote_filter.rb b/lib/banzai/filter/footnote_filter.rb index 00a38f02141..537b7c80d91 100644 --- a/lib/banzai/filter/footnote_filter.rb +++ b/lib/banzai/filter/footnote_filter.rb @@ -7,13 +7,14 @@ module Banzai # 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. - # + # Footnotes are numbered as an increasing integer starting at `1`. + # The `id` associated with a footnote is based on the footnote reference + # string. For example, `[^foot]` will generate `id="fn-foot"`. + # 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`. + # can be used for a single render). So you get `id=fn-1-4335` and `id=fn-foot-4335`. # class FootnoteFilter < HTML::Pipeline::Filter FOOTNOTE_ID_PREFIX = 'fn-' @@ -26,53 +27,24 @@ module Banzai CSS_FOOTNOTE = 'sup > a[data-footnote-ref]' XPATH_FOOTNOTE = Gitlab::Utils::Nokogiri.css_to_xpath(CSS_FOOTNOTE).freeze - # only needed when feature flag use_cmark_renderer is turned off - INTEGER_PATTERN = /\A\d+\z/.freeze - FOOTNOTE_ID_PREFIX_OLD = 'fn' - FOOTNOTE_LINK_ID_PREFIX_OLD = 'fnref' - FOOTNOTE_LI_REFERENCE_PATTERN_OLD = /\A#{FOOTNOTE_ID_PREFIX_OLD}\d+\z/.freeze - FOOTNOTE_LINK_REFERENCE_PATTERN_OLD = /\A#{FOOTNOTE_LINK_ID_PREFIX_OLD}\d+\z/.freeze - FOOTNOTE_START_NUMBER = 1 - CSS_SECTION_OLD = "ol > li[id=#{FOOTNOTE_ID_PREFIX_OLD}#{FOOTNOTE_START_NUMBER}]" - XPATH_SECTION_OLD = Gitlab::Utils::Nokogiri.css_to_xpath(CSS_SECTION_OLD).freeze - def call - if Feature.enabled?(:use_cmark_renderer, default_enabled: :yaml) - # Sanitization stripped off the section class - add it back in - return doc unless section_node = doc.at_xpath(XPATH_SECTION) + # Sanitization stripped off the section class - add it back in + return doc unless section_node = doc.at_xpath(XPATH_SECTION) - section_node.append_class('footnotes') - else - return doc unless first_footnote = doc.at_xpath(XPATH_SECTION_OLD) - return doc unless first_footnote.parent - - first_footnote.parent.wrap('<section class="footnotes">') - end + section_node.append_class('footnotes') rand_suffix = "-#{random_number}" modified_footnotes = {} - xpath_footnote = if Feature.enabled?(:use_cmark_renderer, default_enabled: :yaml) - XPATH_FOOTNOTE - else - Gitlab::Utils::Nokogiri.css_to_xpath('sup > a[id]') - end - - doc.xpath(xpath_footnote).each do |link_node| - if Feature.enabled?(:use_cmark_renderer, default_enabled: :yaml) - ref_num = link_node[:id].delete_prefix(FOOTNOTE_LINK_ID_PREFIX) - ref_num.gsub!(/[[:punct:]]/, '\\\\\&') - else - ref_num = link_node[:id].delete_prefix(FOOTNOTE_LINK_ID_PREFIX_OLD) - end + doc.xpath(XPATH_FOOTNOTE).each do |link_node| + ref_num = link_node[:id].delete_prefix(FOOTNOTE_LINK_ID_PREFIX) + ref_num.gsub!(/[[:punct:]]/, '\\\\\&') - css = Feature.enabled?(:use_cmark_renderer, default_enabled: :yaml) ? "section[data-footnotes] li[id=#{fn_id(ref_num)}]" : "li[id=#{fn_id(ref_num)}]" + css = "section[data-footnotes] li[id=#{fn_id(ref_num)}]" node_xpath = Gitlab::Utils::Nokogiri.css_to_xpath(css) footnote_node = doc.at_xpath(node_xpath) if footnote_node || modified_footnotes[ref_num] - next if Feature.disabled?(:use_cmark_renderer, default_enabled: :yaml) && !INTEGER_PATTERN.match?(ref_num) - link_node[:href] += rand_suffix link_node[:id] += rand_suffix @@ -103,13 +75,11 @@ module Banzai end def fn_id(num) - prefix = Feature.enabled?(:use_cmark_renderer, default_enabled: :yaml) ? FOOTNOTE_ID_PREFIX : FOOTNOTE_ID_PREFIX_OLD - "#{prefix}#{num}" + "#{FOOTNOTE_ID_PREFIX}#{num}" end def fnref_id(num) - prefix = Feature.enabled?(:use_cmark_renderer, default_enabled: :yaml) ? FOOTNOTE_LINK_ID_PREFIX : FOOTNOTE_LINK_ID_PREFIX_OLD - "#{prefix}#{num}" + "#{FOOTNOTE_LINK_ID_PREFIX}#{num}" end end end |