1 # frozen_string_literal: true
4 DESCRIPTION_MAX_LENGTH = 500
5 DESCRIPTION_WORD_BREAK_THRESHOLD_LENGTH = 450
6 URL_UNSAFE_CHARS = "[^\\w!#$%&'*+,./:;=?@_~^\\-]"
8 def self.new(format, text)
10 when "html" then HTML.new(text || "")
11 when "markdown" then Markdown.new(text || "")
12 when "text" then Text.new(text || "")
17 include ActionView::Helpers::TextHelper
18 include ActionView::Helpers::OutputSafetyHelper
20 def sanitize(text, _options = {})
21 Sanitize.clean(text, Sanitize::Config::OSM).html_safe
38 def truncate_html(max_length = nil, img_length = 1000)
40 return html_doc if max_length.nil?
42 doc = Nokogiri::HTML::DocumentFragment.parse(html_doc)
43 keep_or_discards = %w[p h1 h2 h3 h4 h5 h6 pre a table ul ol dl]
44 accumulated_length = 0
45 exceeded_node_parent = nil
48 doc.traverse do |node|
49 if accumulated_length >= max_length
50 if node == exceeded_node_parent
51 exceeded_node_parent = node.parent
52 node.remove if keep_or_discards.include?(node.name)
59 next unless node.children.empty?
62 accumulated_length += node.text.length
63 elsif node.name == "img"
64 accumulated_length += img_length
67 if accumulated_length >= max_length
69 exceeded_node_parent = node.parent
75 :truncated => truncated,
76 :html => doc.to_html.html_safe
82 def simple_format(text)
83 SimpleFormat.new.simple_format(text, :dir => "auto")
87 Sanitize.clean(text, Sanitize::Config::OSM).html_safe
90 def linkify(text, mode = :urls)
91 ERB::Util.html_escape(text)
92 .then { |html| expand_link_shorthands(html) }
93 .then { |html| expand_host_shorthands(html) }
94 .then { |html| auto_link(html, mode) }
100 def gsub_pairs_for_linkify_detection
104 def expand_link_shorthands(text)
105 gsub_pairs_for_linkify_detection
106 .reduce(text) { |text, (pattern, replacement)| text.gsub(pattern, replacement) }
109 def expand_host_shorthands(text)
111 .wrap(Settings.linkify&.normalisation_rules)
112 .select { |rule| rule.host_replacement && rule.hosts&.any? }
113 .reduce(text) do |text, rule|
114 text.gsub(/(?<=^|#{URL_UNSAFE_CHARS})\b#{Regexp.escape(rule.host_replacement)}/) do
115 "#{Settings.server_protocol}://#{rule.hosts[0]}"
120 def auto_link(text, mode)
121 link_attr = 'rel="nofollow noopener noreferrer" dir="auto"'
122 Rinku.auto_link(text, mode, link_attr) { |url| format_link_text(url) }
125 def format_link_text(url)
127 .wrap(Settings.linkify&.normalisation_rules)
128 .reduce(url) do |normalised_url, rule|
129 shorten_host(normalised_url, rule.hosts, rule.host_replacement) do |path|
130 path.sub(Regexp.new(rule.optional_path_prefix || ""), "")
133 Array.wrap(Settings.linkify&.display_rules)
134 .select { |rule| rule.pattern && rule.replacement }
135 .reduce(url) { |url, rule| url.sub(Regexp.new(rule.pattern), rule.replacement) }
138 def shorten_host(url, hosts, hosts_replacement)
139 %r{^(https?://([^/]*))(.*)$}.match(url) do |m|
140 scheme_host, host, path = m.captures
141 if hosts&.include?(host)
142 path = yield(path) if block_given?
144 "#{hosts_replacement}#{path}"
146 "#{scheme_host}#{path}"
155 linkify(simple_format(self))
163 class Markdown < Base
165 linkify(sanitize(document.to_html), :all)
173 @image_element = first_image_element(document.root) unless defined? @image_element
174 @image_element.attr["src"] if @image_element
178 @image_element = first_image_element(document.root) unless defined? @image_element
179 @image_element.attr["alt"] if @image_element
183 return @description if defined? @description
185 @description = first_truncated_text_content(document.root)
191 return @document if @document
193 @document = Kramdown::Document.new(self)
195 should_get_dir_auto = lambda do |el|
196 dir_auto_types = [:p, :header, :codespan, :codeblock, :pre, :ul, :ol, :table, :dl, :math]
197 return true if dir_auto_types.include?(el.type)
198 return true if el.type == :a && el.children.length == 1 && el.children[0].type == :text && el.children[0].value == el.attr["href"]
203 add_dir = lambda do |element|
204 element.attr["dir"] ||= "auto" if should_get_dir_auto.call(element)
205 element.children.each(&add_dir)
207 add_dir.call(@document.root)
212 def first_image_element(element)
213 return element if image?(element) && element.attr["src"].present?
215 element.children.find do |child|
216 nested_image = first_image_element(child)
217 break nested_image if nested_image
221 def first_truncated_text_content(element)
222 if paragraph?(element)
223 truncated_text_content(element)
225 element.children.find do |child|
226 text = first_truncated_text_content(child)
227 break text unless text.nil?
232 def truncated_text_content(element)
235 append_text = lambda do |child|
236 if child.type == :text
239 child.children.each do |c|
241 break if text.length > DESCRIPTION_MAX_LENGTH
245 append_text.call(element)
247 return nil if text.blank?
249 text_truncated_to_word_break = text.truncate(DESCRIPTION_MAX_LENGTH, :separator => /(?<!\s)\s+/)
251 if text_truncated_to_word_break.length >= DESCRIPTION_WORD_BREAK_THRESHOLD_LENGTH
252 text_truncated_to_word_break
254 text.truncate(DESCRIPTION_MAX_LENGTH)
259 element.type == :img || (element.type == :html_element && element.value == "img")
262 def paragraph?(element)
263 element.type == :p || (element.type == :html_element && element.value == "p")
269 linkify(simple_format(ERB::Util.html_escape(self)))
278 def gsub_pairs_for_linkify_detection
280 .wrap(Settings.linkify&.detection_rules)
281 .select { |rule| rule.path_template && rule.patterns.is_a?(Array) }
283 expanded_path = "#{rule.host || "#{Settings.server_protocol}://#{Settings.server_url}"}/#{rule.path_template}"
285 .select { |pattern| pattern.is_a?(String) }
286 .map { |pattern| [Regexp.new("(?<=^|#{URL_UNSAFE_CHARS})#{pattern}", Regexp::IGNORECASE), expanded_path] }