docbrown/app/lib/reverse_markdown/converters/custom_text.rb
Hugo Peixoto 24e9f81f5a
Fix newlines being chomped in RSS import (#10476)
The CustomText converter was monkey patched to fix a bug related to
preserve_tags. Somewhere in that process, remove_inner_newlines was
changed to strip newlines completely, instead of replacing them with a
single space. This restores that functionality.

In development mode, files are not eager loaded. That means that the
converters were not being required, so it was not possible to reproduce
that issue in development mode (since it was falling back to the
original converter, which does not have the bug). This commit also adds
an initializer that requires every converter.
2020-09-29 10:38:36 -04:00

55 lines
1.3 KiB
Ruby

module ReverseMarkdown
module Converters
class CustomText < Base
def convert(node, _options = {})
if node.text.strip.empty?
treat_empty(node)
else
treat_text(node)
end
end
private
def treat_empty(node)
parent = node.parent.name.to_sym
if %i[ol ul].include?(parent) # Otherwise the identation is broken
""
elsif node.text == " " # Regular whitespace text node
" "
else
""
end
end
def treat_text(node)
text = node.text
text = preserve_nbsp(text)
text = remove_border_newlines(text)
text = remove_inner_newlines(text)
text = escape_keychars(text)
preserve_keychars_within_backticks(text)
end
def preserve_nbsp(text)
text.gsub(/\u00A0/, "&nbsp;")
end
def remove_border_newlines(text)
text.gsub(/\A\n+/, "").gsub(/\n+\z/, "")
end
def remove_inner_newlines(text)
text.tr("\r\n\t", " ").squeeze(" ")
end
def preserve_keychars_within_backticks(text)
text.gsub(/`.*?`/) do |match|
match.gsub('\_', "_").gsub('\*', "*")
end
end
end
register :text, CustomText.new
end
end