include ActionView::Helpers::TextHelper
include ActionView::Helpers::OutputSafetyHelper
- def sanitize(text)
+ def sanitize(text, _options = {})
Sanitize.clean(text, Sanitize::Config::OSM).html_safe
end
end
link_size += link.content.length
end
- link_proportion = link_size.to_f / doc.content.length.to_f
+ link_proportion = link_size.to_f / doc.content.length
end
spammy_phrases = SPAMMY_PHRASES.count do |phrase|
doc.content.include?(phrase)
end
- [link_proportion - 0.2, 0.0].max * 200 +
- link_count * 40 +
- spammy_phrases * 40
+ ([link_proportion - 0.2, 0.0].max * 200) +
+ (link_count * 40) +
+ (spammy_phrases * 40)
+ end
+
+ def image
+ nil
end
protected
def linkify(text, mode = :urls)
if text.html_safe?
- Rinku.auto_link(text, mode, tag_builder.tag_options(:rel => "nofollow noopener noreferer")).html_safe
+ Rinku.auto_link(text, mode, tag_builder.tag_options(:rel => "nofollow noopener noreferrer")).html_safe
else
- Rinku.auto_link(text, mode, tag_builder.tag_options(:rel => "nofollow noopener noreferer"))
+ Rinku.auto_link(text, mode, tag_builder.tag_options(:rel => "nofollow noopener noreferrer"))
end
end
end
class Markdown < Base
def to_html
- linkify(sanitize(Kramdown::Document.new(self).to_html), :all)
+ linkify(sanitize(document.to_html), :all)
end
def to_text
to_s
end
+
+ def image
+ return @image if defined? @image
+
+ @image = first_image_element(document.root)&.attr&.[]("src")
+ end
+
+ private
+
+ def document
+ @document ||= Kramdown::Document.new(self)
+ end
+
+ def first_image_element(element)
+ return element if image?(element) && element.attr["src"].present?
+
+ element.children.find do |child|
+ nested_image = first_image_element(child)
+ break nested_image if nested_image
+ end
+ end
+
+ def image?(element)
+ element.type == :img || (element.type == :html_element && element.value == "img")
+ end
end
class Text < Base