class Slack::Messenger::Util::LinkFormatter
Constants
- HTML_PATTERN
- MARKDOWN_PATTERN
Attempt at only matching pairs of parens per the markdown spec spec.commonmark.org/0.27/#links
- VALID_URI_CHARS
Attributes
formats[R]
Public Class Methods
format(string, **opts)
click to toggle source
# File lib/slack-messenger/util/link_formatter.rb, line 32 def format string, **opts LinkFormatter.new(string, **opts).formatted end
new(string, formats: %i[html markdown])
click to toggle source
# File lib/slack-messenger/util/link_formatter.rb, line 39 def initialize string, formats: %i[html markdown] @formats = formats @orig = string.respond_to?(:scrub) ? string.scrub : string end
Public Instance Methods
formatted()
click to toggle source
rubocop:disable Lint/RescueWithoutErrorClass
# File lib/slack-messenger/util/link_formatter.rb, line 45 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
Private Instance Methods
slack_link(link, text=nil)
click to toggle source
# File lib/slack-messenger/util/link_formatter.rb, line 73 def slack_link link, text=nil "<#{link}" \ "#{text && !text.empty? ? "|#{text}" : ''}" \ ">" end
sub_html_links(string)
click to toggle source
rubocop:enable Lint/RescueWithoutErrorClass
# File lib/slack-messenger/util/link_formatter.rb, line 57 def sub_html_links string return string unless formats.include?(:html) string.gsub(HTML_PATTERN) do slack_link Regexp.last_match[1], Regexp.last_match[2] end end
sub_markdown_links(string)
click to toggle source
# File lib/slack-messenger/util/link_formatter.rb, line 65 def sub_markdown_links string return string unless formats.include?(:markdown) string.gsub(MARKDOWN_PATTERN) do slack_link Regexp.last_match[2], Regexp.last_match[1] end end