CommonMarker+Rouge

RubyGems GitHub Gitlab Bitbucket

A CommonMarker wrapper with syntax highlight support by Rouge.

Important

commonmarker-rouge currently works only with renderer in :UNSAFE mode. Don’t forget to set :UNSAFE manually if you’re overwriting the renderer mode.

If you know how to fix this behavior please comment in Issue #5 on GitHub

Installation

Add these lines to your application’s Gemfile:

gem 'commonmarker-rouge'

Usage

Review the CommonMarker options to understand how to use the render_html command:

# use default CommonMarker class and Rouge::Formatters::HTML formatter
CommonMarker::Rouge.render_html(content)

# Basic examples with 1st and 2nd optional parameters
CommonMarker::Rouge.render_html(content, [:DEFAULT], [:UNSAFE])
CommonMarker::Rouge.render_html(content, [:DEFAULT], [:SOURCEPOS])
CommonMarker::Rouge.render_html(content, [:DEFAULT], [:UNSAFE, :SOURCEPOS])

# use GitHub extensions - both 1st/2nd parameters required
CommonMarker::Rouge.render_html(content, [:DEFAULT], [:DEFAULT], [:table])
CommonMarker::Rouge.render_html(content, [:DEFAULT], [:DEFAULT], [:table, :strikethrough])
CommonMarker::Rouge.render_html(content, [:DEFAULT], [:DEFAULT], [:table, :strikethrough, :autolink])
CommonMarker::Rouge.render_html(content, [:DEFAULT], [:DEFAULT], [:table, :strikethrough, :tagfilter])

# get CommonMarker parsed AST
CommonMarker::Rouge.render_doc(content)

# use custom CommonMarker wrapper (must provide compatible render_doc)
CommonMarker::Rouge.render_html(content, cmark_class: CommonMarkerWrapper)

# use custom Rouge formatter by class
CommonMarker::Rouge.render_html(content, formatter_class: Rouge::Formatters::HTMLLinewise)
# or by instance
CommonMarker::Rouge.render_html(content, formatter: Rouge::Formatters::HTMLTable.new(
  Rouge::Formatters::HTML.new
))

# pass some options to Rouge
CommonMarker::Rouge.render_html(content, options: { css_class: 'custom-class' })
# or
CommonMarker::Rouge.render_html(content, formatter: Rouge::Formatters::HTMLTable.new(
  Rouge::Formatters::HTML.new, code_class: 'rouge-code'
))

License

The gem is available as open source under the terms of the MIT License.