Welcome to mirror list, hosted at ThFree Co, Russian Federation.

html_to_markdown_parser.rb « email « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 42dd012308b02cd4fee900b95c7370d5a2600671 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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