Sha256: 69e2bd38c6a65f2a1b0b5d41d9e12d67b368a85f21cc4bca66e1f8404efd8074

Contents?: true

Size: 1.05 KB

Versions: 13

Compression:

Stored size: 1.05 KB

Contents

module GitHubParser
  extend self

  class HTMLWithPantsRenderer < Redcarpet::Render::HTML
    include Redcarpet::Render::SmartyPants
  end

  def self.parse(markdown)
    html = github_parser.render(markdown.to_s)
    doc = Nokogiri::HTML::DocumentFragment.parse(html)
    special_blocks(doc)
    doc
  end

  private

  def self.github_parser
    @@github_parser ||= Redcarpet::Markdown.new(HTMLWithPantsRenderer, fenced_code_blocks: true, tables: true, no_intra_emphasis: true)
  end

  def self.special_blocks(doc)
    doc.css('blockquote>p:first').each do |node|
      if match = node.inner_html.match(/\A\W*(callout|warning|note)\W/)
        node.parent.name = 'div'
        node.parent['class'] = match[1]

        new_html = node.inner_html.gsub(/\A\W*(callout|warning|note)\W/, '')

        # Assigning inner_html directly causes encoding issues in old libxml versions,
        # workaround from https://github.com/sparklemotion/nokogiri/issues/458#issuecomment-3136620
        node.children = Nokogiri::HTML.fragment(new_html, 'utf-8')
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
devcenter-parser-2.2.2 lib/devcenter-parser/github_parser.rb
devcenter-parser-2.2.1 lib/devcenter-parser/github_parser.rb
devcenter-parser-2.2.0 lib/devcenter-parser/github_parser.rb
devcenter-parser-2.1.2 lib/devcenter-parser/github_parser.rb
devcenter-parser-2.1.1 lib/devcenter-parser/github_parser.rb
devcenter-parser-2.1.0 lib/devcenter-parser/github_parser.rb
devcenter-parser-2.0.2 lib/devcenter-parser/github_parser.rb
devcenter-parser-2.0.1 lib/devcenter-parser/github_parser.rb
devcenter-parser-2.0.0 lib/devcenter-parser/github_parser.rb
devcenter-parser-1.4.9 lib/devcenter-parser/github_parser.rb
devcenter-parser-1.4.8 lib/devcenter-parser/github_parser.rb
devcenter-parser-1.4.7 lib/devcenter-parser/github_parser.rb
devcenter-parser-1.4.6 lib/devcenter-parser/github_parser.rb