diff options
Diffstat (limited to 'lib/gitlab/email/html_to_markdown_parser.rb')
-rw-r--r-- | lib/gitlab/email/html_to_markdown_parser.rb | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/gitlab/email/html_to_markdown_parser.rb b/lib/gitlab/email/html_to_markdown_parser.rb new file mode 100644 index 00000000000..42dd012308b --- /dev/null +++ b/lib/gitlab/email/html_to_markdown_parser.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +require 'nokogiri' + +module Gitlab + module Email + class HtmlToMarkdownParser < Html2Text + ADDITIONAL_TAGS = %w[em strong img details].freeze + IMG_ATTRS = %w[alt src].freeze + + def self.convert(html) + html = fix_newlines(replace_entities(html)) + doc = Nokogiri::HTML(html) + + HtmlToMarkdownParser.new(doc).convert + end + + def iterate_over(node) + return super unless ADDITIONAL_TAGS.include?(node.name) + + if node.name == 'img' + node.keys.each { |key| node.remove_attribute(key) unless IMG_ATTRS.include?(key) } # rubocop:disable Style/HashEachMethods + end + + Kramdown::Document.new(node.to_html, input: 'html').to_commonmark + end + end + end +end |