1 # frozen_string_literal: true
5 "Business Description:", "Additional Keywords:"
8 MAX_DESCRIPTION_LENGTH = 500
10 def self.new(format, text)
12 when "html" then HTML.new(text || "")
13 when "markdown" then Markdown.new(text || "")
14 when "text" then Text.new(text || "")
19 include ActionView::Helpers::TextHelper
20 include ActionView::Helpers::OutputSafetyHelper
22 def sanitize(text, _options = {})
23 Sanitize.clean(text, Sanitize::Config::OSM).html_safe
32 doc = Nokogiri::HTML(to_html)
37 doc.xpath("//a").each do |link|
39 link_size += link.content.length
42 link_proportion = link_size.to_f / doc.content.length
45 spammy_phrases = SPAMMY_PHRASES.count do |phrase|
46 doc.content.include?(phrase)
49 ([link_proportion - 0.2, 0.0].max * 200) +
68 def simple_format(text)
69 SimpleFormat.new.simple_format(text, :dir => "auto")
73 Sanitize.clean(text, Sanitize::Config::OSM).html_safe
76 def linkify(text, mode = :urls)
77 link_attr = 'rel="nofollow noopener noreferrer"'
78 Rinku.auto_link(ERB::Util.html_escape(text), mode, link_attr) do |url|
79 url = shorten_host(url, Settings.linkify_hosts, Settings.linkify_hosts_replacement)
80 shorten_host(url, Settings.linkify_wiki_hosts, Settings.linkify_wiki_hosts_replacement) do |path|
81 path.sub(Regexp.new(Settings.linkify_wiki_optional_path_prefix || ""), "")
88 def shorten_host(url, hosts, hosts_replacement)
89 %r{^(https?://([^/]*))(.*)$}.match(url) do |m|
90 scheme_host, host, path = m.captures
91 if hosts&.include?(host)
92 path = yield(path) if block_given?
94 "#{hosts_replacement}#{path}"
96 "#{scheme_host}#{path}"
105 linkify(simple_format(self))
113 class Markdown < Base
115 linkify(sanitize(document.to_html), :all)
123 @image_element = first_image_element(document.root) unless defined? @image_element
124 @image_element.attr["src"] if @image_element
128 @image_element = first_image_element(document.root) unless defined? @image_element
129 @image_element.attr["alt"] if @image_element
133 return @description if defined? @description
135 @description = first_truncated_text_content(document.root)
141 return @document if @document
143 @document = Kramdown::Document.new(self)
145 should_get_dir_auto = lambda do |el|
146 dir_auto_types = [:p, :header, :codespan, :codeblock, :pre, :ul, :ol, :table, :dl, :math]
147 return true if dir_auto_types.include?(el.type)
148 return true if el.type == :a && el.children.length == 1 && el.children[0].type == :text && el.children[0].value == el.attr["href"]
153 add_dir = lambda do |element|
154 element.attr["dir"] ||= "auto" if should_get_dir_auto.call(element)
155 element.children.each(&add_dir)
157 add_dir.call(@document.root)
162 def first_image_element(element)
163 return element if image?(element) && element.attr["src"].present?
165 element.children.find do |child|
166 nested_image = first_image_element(child)
167 break nested_image if nested_image
171 def first_truncated_text_content(element)
172 if paragraph?(element)
173 truncated_text_content(element)
175 element.children.find do |child|
176 text = first_truncated_text_content(child)
177 break text unless text.nil?
182 def truncated_text_content(element)
185 append_text = lambda do |child|
186 if child.type == :text
189 child.children.each do |c|
191 break if text.length > MAX_DESCRIPTION_LENGTH
195 append_text.call(element)
197 return nil if text.blank?
199 text.truncate(MAX_DESCRIPTION_LENGTH)
203 element.type == :img || (element.type == :html_element && element.value == "img")
206 def paragraph?(element)
207 element.type == :p || (element.type == :html_element && element.value == "p")
213 linkify(simple_format(ERB::Util.html_escape(self)))