Class: Banzai::Filter::AutolinkFilter

Inherits:
HTML::Pipeline::Filter
  • Object
show all
Includes:
ActionView::Helpers::TagHelper, Gitlab::Utils::SanitizeNodeLink
Defined in:
lib/banzai/filter/autolink_filter.rb

Overview

HTML Filter for auto-linking URLs in HTML.

Based on HTML::Pipeline::AutolinkFilter

Note that our CommonMark parser, `commonmarker` (using the autolink extension) handles standard autolinking, like http/https. We detect additional schemes (smb, rdar, etc).

Context options:

:autolink  - Boolean, skips all processing done by this filter when false
:link_attr - Hash of attributes for the generated links

Constant Summary collapse

%r{([a-z][a-z0-9\+\.-]+://[^\s>]+)(?<!\?|!|\.|,|:)}.freeze
IGNORE_PARENTS =

Text matching LINK_PATTERN inside these elements will not be linked

%w(a code kbd pre script style).to_set
TEXT_QUERY =

The XPath query to use for finding text nodes to parse.

%Q(descendant-or-self::text()[
  not(#{IGNORE_PARENTS.map { |p| "ancestor::#{p}" }.join(' or ')})
  and contains(., '://')
]).freeze
PUNCTUATION_PAIRS =
{
  "'" => "'",
  '"' => '"',
  ')' => '(',
  ']' => '[',
  '}' => '{'
}.freeze

Constants included from Gitlab::Utils::SanitizeNodeLink

Gitlab::Utils::SanitizeNodeLink::ATTRS_TO_SANITIZE, Gitlab::Utils::SanitizeNodeLink::UNSAFE_PROTOCOLS

Instance Method Summary collapse

Methods included from Gitlab::Utils::SanitizeNodeLink

#remove_unsafe_links, #safe_protocol?

Instance Method Details

#callObject


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/banzai/filter/autolink_filter.rb', line 56

def call
  return doc if context[:autolink] == false

  doc.xpath(TEXT_QUERY).each do |node|
    content = node.to_html

    next unless content.match(LINK_PATTERN)

    html = autolink_filter(content)

    next if html == content

    node.replace(html)
  end

  doc
end