Module: HtmlToMarkdown

Defined in:
lib/html_to_markdown.rb,
lib/html_to_markdown/version.rb

Overview

High-performance HTML to Markdown conversion.

Examples:

Simple conversion

HtmlToMarkdown.convert('<h1>Hello</h1>') # => "# Hello\n\n"

With options

HtmlToMarkdown.convert('<h1>Hello</h1>', heading_style: 'atx')

Constant Summary collapse

VERSION =
'3.2.0'

Class Method Summary collapse

Class Method Details

.convert(html, options = {}) ⇒ String

Convert HTML to Markdown.

Parameters:

  • html (String)

    The HTML content to convert.

  • options (Hash) (defaults to: {})

    Optional conversion options. Supported keys (all optional):

    • :heading_style - ‘atx’, ‘atx_closed’, ‘setext’, ‘underlined’

    • :code_block_style - ‘backticks’, ‘tildes’, ‘indented’

    • :escape_asterisks - Boolean

    • :escape_underscores - Boolean

    • :escape_misc - Boolean

    • :escape_ascii - Boolean

    • :strip_newlines - Boolean

    • :keep_inline_images_in - Array of tag names

    • :strip_tags - Array of tag names to strip

    • :preserve_tags - Array of tag names to preserve verbatim

    (and more, matching ConversionOptions fields)

Returns:

  • (String)

    The converted Markdown content.



31
32
33
34
35
36
37
38
39
# File 'lib/html_to_markdown.rb', line 31

def self.convert(html, options = {})
  opts = if options.nil? || options.empty?
           nil
         else
           HtmlToMarkdownRs::ConversionOptions.new(options)
         end
  result = HtmlToMarkdownRs.convert(html, opts)
  result.content || ''
end