Class: Collavre::CommentLinkFormatter

Inherits:
Object
  • Object
show all
Defined in:
app/services/collavre/comment_link_formatter.rb

Constant Summary collapse

URL_REGEX =
URI::DEFAULT_PARSER.make_regexp(%w[http https])
TRAILING_PUNCTUATION =
".,!?;:".freeze

Instance Method Summary collapse

Constructor Details

#initialize(content, metadata_fetcher: nil, logger: Rails.logger) ⇒ CommentLinkFormatter

Returns a new instance of CommentLinkFormatter.



8
9
10
11
12
# File 'app/services/collavre/comment_link_formatter.rb', line 8

def initialize(content, metadata_fetcher: nil, logger: Rails.logger)
  @content = content.to_s
  @metadata_fetcher =  || method(:default_fetch_metadata)
  @logger = logger
end

Instance Method Details

#formatObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/services/collavre/comment_link_formatter.rb', line 14

def format
  return @content if @content.blank?

  @content.gsub(URL_REGEX) do |match|
    match_data = Regexp.last_match
    url, trailing = strip_trailing_punctuation(match)
    next match if markdown_link?(match_data.pre_match)

     = (url)
    title = [:title].presence
    next match if title.blank?

    "[#{title}](#{url})#{trailing}"
  end
end