Module: Markbridge::Renderers::Discourse::HtmlBlock

Defined in:
lib/markbridge/renderers/discourse/html_block.rb

Overview

The one place that knows how CommonMark HTML blocks (spec §4.6) and Markdown meet in the rendered output.

Inside an HTML block the content passes through as raw HTML. Markdown is parsed again only across blank lines. A tag that renders into such a block therefore picks one of two forms:

  1. raw HTML — an HTML equivalent of the tag's Markdown, spliced into the block as it is;

  2. a Markdown island — the tag's normal Markdown surrounded by blank lines, which end the block and start it again:

    <div align="center">
    
    [a link](https://example.com)
    
    </div>
    

Class Method Summary collapse

Class Method Details

.island(markdown) ⇒ String

Wrap Markdown so that CommonMark parses it even inside an HTML block. Blank lines that the fragment already has at its edges are folded into the wrap.

Examples:

HtmlBlock.island("- a\n") # => "\n\n- a\n\n"

Parameters:

  • markdown (String)

Returns:

  • (String)


123
124
125
# File 'lib/markbridge/renderers/discourse/html_block.rb', line 123

def self.island(markdown)
  "\n\n#{markdown.gsub(BLANK_EDGES, "")}\n\n"
end

.opens?(line) ⇒ Boolean

Whether line starts an HTML block. A closing tag such as </div> starts one too. The block runs until the next blank line.

Parameters:

  • line (String)

Returns:

  • (Boolean)


112
113
114
# File 'lib/markbridge/renderers/discourse/html_block.rb', line 112

def self.opens?(line)
  OPENER.match?(line)
end

.safe?(output) ⇒ Boolean

Whether a fragment can be spliced into an HTML block: raw HTML or plain text without Markdown sigils, or an island.

Used by the html_mode contract check that ships in markbridge/rspec and by this repo's own contract spec.

Parameters:

  • output (String)

    a tag's html_mode render result

Returns:

  • (Boolean)


134
135
136
137
138
# File 'lib/markbridge/renderers/discourse/html_block.rb', line 134

def self.safe?(output)
  return true if output.start_with?("\n\n") && output.end_with?("\n\n")

  !output.match?(MARKDOWN_SIGILS)
end