HTML
Use the HTML parser when your source stores posts or pages as HTML: a forum with a WYSIWYG editor, a blog, a help center, a mailing list archive. Nokogiri parses the HTML, so tags that are not closed or nested wrongly are repaired the way a browser repairs them.
Requirements
Section titled “Requirements”Add nokogiri to your Gemfile. Markbridge does not depend on it, because the BBCode and MediaWiki parsers work without it:
gem "nokogiri"Quick start
Section titled “Quick start”require "markbridge/html"
result = Markbridge.html_to_markdown("<p>Hello <strong>world</strong>!</p>")result.markdown# => "Hello **world**!"result is a Markbridge::Conversion. Next to markdown it has unknown_tags, diagnostics and errors.
To get the AST:
parse = Markbridge.parse_html("<a href='https://example.com'>link</a>")parse.ast# => AST::Document(Url("link", href: "https://example.com"))Supported tags
Section titled “Supported tags”Relative links and images are written as they are, and Discourse drops an address without a scheme and without a leading slash. Give the renderer the address of the source site as base_url: to keep them working.
HandlerRegistry.default is the exact list.
Tags without a handler
Section titled “Tags without a handler”What the parser does with a tag it has no handler for depends on the tag:
- Ignored with their content:
<script>,<style>,<head>,<title>,<noscript>,<template>. Nothing of them reaches the output. - Transparent:
<thead>,<tbody>,<tfoot>. Their children are processed as if the tag were not there. - Unknown: every other tag, like
<div>,<font>or<iframe>. Its children are processed too, andunknown_tagscounts the tag.
A block-level tag among them, like <div> or <section>, still keeps its content on a line of its own: <div>a</div><div>b</div> is a, a line break and b. Which tags are block-level is the block_level_tags set of the registry.
So the only difference between transparent and unknown is the count. When your source is full of a wrapper you expect, add it to the transparent tags. Then unknown_tags only shows what you did not expect:
parser = Markbridge::Parsers::HTML::Parser.new do |registry| registry.transparent_tags.merge(%w[div section font]) end
parser.parse("<div><b>bold</b></div><custom-card>text</custom-card>")parser.unknown_tags# => {"custom-card" => 1}To give such a tag a meaning, for example <details> as a spoiler, register a handler. See Customizing below.
Code languages
Section titled “Code languages”A code block keeps its language for syntax highlighting. The parser takes the first valid one of these:
- A
language-*class on the element. - A
language-*class on its direct<code>child. - The element’s
langattribute. - A single class on the element or its direct
<code>child.
A valid language starts with a letter or a digit and has only letters, digits, _, + and -. With more than one class and none of them a language-* class, like hljs codeblock, the block has no language.
require "markbridge/html"
result = Markbridge.html_to_markdown('<pre><code class="language-ruby">puts 1</code></pre>')result.markdown# => "```ruby\nputs 1\n```"Whitespace
Section titled “Whitespace”HTML source is often indented and wrapped, and a browser does not show that. The parser collapses whitespace the same way:
- Several whitespace characters in a row become one space.
- A space at the start or the end of a block is dropped, and so is a space right after another one, also across tags:
<b>a </b> bkeeps one space. - Text inside
<pre>,<code>,<textarea>and<tt>stays as it is.
Which tags are blocks and which keep their whitespace is set on the HandlerRegistry: block_level_tags and whitespace_preserving_tags.
Customizing
Section titled “Customizing”A handler is an object with #process(element:, parent:). It adds a node to parent and returns the node that the children of the element go into, or nil to skip the children:
class AsideHandler < Markbridge::Parsers::HTML::Handlers::BaseHandler def initialize @element_class = AST::Quote end
attr_reader :element_class
def process(element:, parent:) note = AST::Quote.new parent << note note # the children of <aside> go into the quote endend
handlers = Markbridge::Parsers::HTML::HandlerRegistry.build_from_default do |registry| registry.register("aside", AsideHandler.new) end
Markbridge.html_to_markdown("<aside>heads up</aside>", handlers:)Extending Markbridge has more on handlers.
Using the parser directly
Section titled “Using the parser directly”The parser takes a String or a Nokogiri node, so HTML you already parsed is not parsed again:
parser = Markbridge::Parsers::HTML::Parser.newast = parser.parse("<p>rich <em>content</em></p>")
renderer = Markbridge::Renderers::Discourse::Renderer.newrenderer.render(ast)Limits
Section titled “Limits”The default handlers cover the HTML that has a form in Discourse Markdown. What they leave out:
- Styles. Colors, sizes and fonts in a
styleattribute or a<font>tag are dropped. Only the bold, italic, underline and strikethrough of a<span>are read. - Embeds.
<iframe>,<video>,<audio>and<object>have no handler. Only the fallback content between their tags is written, not the address of the embed. Register a handler that writes a link if you need them. <details>,<dl>,<figure>and other structures have no handler either. Their text is kept, their structure is not.