diff options
Diffstat (limited to 'lib/banzai/filter/wiki_link_filter.rb')
-rw-r--r-- | lib/banzai/filter/wiki_link_filter.rb | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/lib/banzai/filter/wiki_link_filter.rb b/lib/banzai/filter/wiki_link_filter.rb index 2b95d87ff8e..0736181d940 100644 --- a/lib/banzai/filter/wiki_link_filter.rb +++ b/lib/banzai/filter/wiki_link_filter.rb @@ -36,7 +36,7 @@ module Banzai protected def process_link(link_attr, node) - process_link_attr(link_attr) + process_link_attr(link_attr, node) remove_unsafe_links({ node: node }, remove_invalid_links: false) end @@ -44,14 +44,27 @@ module Banzai !context[:wiki].nil? end - def process_link_attr(html_attr) + def process_link_attr(html_attr, node) return if html_attr.blank? - html_attr.value = apply_rewrite_rules(html_attr.value) + rewritten_value = apply_rewrite_rules(html_attr.value) + + if html_attr.value != rewritten_value + preserve_original_link(html_attr, node) + end + + html_attr.value = rewritten_value rescue URI::Error, Addressable::URI::InvalidURIError # noop end + def preserve_original_link(html_attr, node) + return if html_attr.blank? + return if node.value?('data-canonical-src') + + node.set_attribute('data-canonical-src', html_attr.value) + end + def apply_rewrite_rules(link_string) Rewriter.new(link_string, wiki: context[:wiki], slug: context[:page_slug]).apply_rules end |