1 # frozen_string_literal: true
4 DESCRIPTION_MAX_LENGTH = 500
5 DESCRIPTION_WORD_BREAK_THRESHOLD_LENGTH = 450
7 def self.new(format, text)
9 when "html" then HTML.new(text || "")
10 when "markdown" then Markdown.new(text || "")
11 when "text" then Text.new(text || "")
16 include ActionView::Helpers::TextHelper
17 include ActionView::Helpers::OutputSafetyHelper
19 def sanitize(text, _options = {})
20 Sanitize.clean(text, Sanitize::Config::OSM).html_safe
37 def truncate_html(max_length = nil, img_length = 1000)
39 return html_doc if max_length.nil?
41 doc = Nokogiri::HTML::DocumentFragment.parse(html_doc)
42 keep_or_discards = %w[p h1 h2 h3 h4 h5 h6 pre a table ul ol dl]
43 accumulated_length = 0
44 exceeded_node_parent = nil
47 doc.traverse do |node|
48 if accumulated_length >= max_length
49 if node == exceeded_node_parent
50 exceeded_node_parent = node.parent
51 node.remove if keep_or_discards.include?(node.name)
58 next unless node.children.empty?
61 accumulated_length += node.text.length
62 elsif node.name == "img"
63 accumulated_length += img_length
66 if accumulated_length >= max_length
68 exceeded_node_parent = node.parent
74 :truncated => truncated,
75 :html => doc.to_html.html_safe
81 def simple_format(text)
82 SimpleFormat.new.simple_format(text, :dir => "auto")
86 Sanitize.clean(text, Sanitize::Config::OSM).html_safe
89 def linkify(text, mode = :urls)
90 link_attr = 'rel="nofollow noopener noreferrer" dir="auto"'
91 Rinku.auto_link(ERB::Util.html_escape(text), mode, link_attr) do |url|
92 url = shorten_host(url, Settings.linkify_hosts, Settings.linkify_hosts_replacement)
93 shorten_host(url, Settings.linkify_wiki_hosts, Settings.linkify_wiki_hosts_replacement) do |path|
94 path.sub(Regexp.new(Settings.linkify_wiki_optional_path_prefix || ""), "")
101 def shorten_host(url, hosts, hosts_replacement)
102 %r{^(https?://([^/]*))(.*)$}.match(url) do |m|
103 scheme_host, host, path = m.captures
104 if hosts&.include?(host)
105 path = yield(path) if block_given?
107 "#{hosts_replacement}#{path}"
109 "#{scheme_host}#{path}"
118 linkify(simple_format(self))
126 class Markdown < Base
128 linkify(sanitize(document.to_html), :all)
136 @image_element = first_image_element(document.root) unless defined? @image_element
137 @image_element.attr["src"] if @image_element
141 @image_element = first_image_element(document.root) unless defined? @image_element
142 @image_element.attr["alt"] if @image_element
146 return @description if defined? @description
148 @description = first_truncated_text_content(document.root)
154 return @document if @document
156 @document = Kramdown::Document.new(self)
158 should_get_dir_auto = lambda do |el|
159 dir_auto_types = [:p, :header, :codespan, :codeblock, :pre, :ul, :ol, :table, :dl, :math]
160 return true if dir_auto_types.include?(el.type)
161 return true if el.type == :a && el.children.length == 1 && el.children[0].type == :text && el.children[0].value == el.attr["href"]
166 add_dir = lambda do |element|
167 element.attr["dir"] ||= "auto" if should_get_dir_auto.call(element)
168 element.children.each(&add_dir)
170 add_dir.call(@document.root)
175 def first_image_element(element)
176 return element if image?(element) && element.attr["src"].present?
178 element.children.find do |child|
179 nested_image = first_image_element(child)
180 break nested_image if nested_image
184 def first_truncated_text_content(element)
185 if paragraph?(element)
186 truncated_text_content(element)
188 element.children.find do |child|
189 text = first_truncated_text_content(child)
190 break text unless text.nil?
195 def truncated_text_content(element)
198 append_text = lambda do |child|
199 if child.type == :text
202 child.children.each do |c|
204 break if text.length > DESCRIPTION_MAX_LENGTH
208 append_text.call(element)
210 return nil if text.blank?
212 text_truncated_to_word_break = text.truncate(DESCRIPTION_MAX_LENGTH, :separator => /(?<!\s)\s+/)
214 if text_truncated_to_word_break.length >= DESCRIPTION_WORD_BREAK_THRESHOLD_LENGTH
215 text_truncated_to_word_break
217 text.truncate(DESCRIPTION_MAX_LENGTH)
222 element.type == :img || (element.type == :html_element && element.value == "img")
225 def paragraph?(element)
226 element.type == :p || (element.type == :html_element && element.value == "p")
232 linkify(simple_format(ERB::Util.html_escape(self)))