Class: Slack::Notifier::Util::LinkFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/slack-notifier/util/link_formatter.rb

Constant Summary collapse

HTML_PATTERN =
%r{
  <a
  (?:.*?)
  href=['"](.+?)['"]
  (?:.*?)>
  (.+?)
  </a>
}x
VALID_PATH_CHARS =

the path portion of a url can contain these characters

'\w\-\.\~\/\?\#\='
MARKDOWN_PATTERN =

Attempt at only matching pairs of parens per the markdown spec spec.commonmark.org/0.27/#links

rubular.com/r/y107aevxqT

%r{
    \[ ([^\[\]]*?) \]
    \( ((https?://.*?) | (mailto:.*?)) \)
    (?! [#{VALID_PATH_CHARS}]* \) )
}x

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, formats: %i[html markdown]) ⇒ LinkFormatter

Returns a new instance of LinkFormatter.



38
39
40
41
# File 'lib/slack-notifier/util/link_formatter.rb', line 38

def initialize string, formats: %i[html markdown]
  @formats = formats
  @orig    = string.respond_to?(:scrub) ? string.scrub : string
end

Instance Attribute Details

#formatsObject (readonly)

Returns the value of attribute formats.



36
37
38
# File 'lib/slack-notifier/util/link_formatter.rb', line 36

def formats
  @formats
end

Class Method Details

.format(string, opts = {}) ⇒ Object



31
32
33
# File 'lib/slack-notifier/util/link_formatter.rb', line 31

def format string, opts={}
  LinkFormatter.new(string, opts).formatted
end

Instance Method Details

#formattedObject

rubocop:disable Lint/RescueWithoutErrorClass



44
45
46
47
48
49
50
51
# File 'lib/slack-notifier/util/link_formatter.rb', line 44

def formatted
  return @orig unless @orig.respond_to?(:gsub)

  sub_markdown_links(sub_html_links(@orig))
rescue => e
  raise e unless RUBY_VERSION < "2.1" && e.message.include?("invalid byte sequence")
  raise e, "#{e.message}. Consider including the 'string-scrub' gem to strip invalid characters"
end