class

CommonMark

Inherits Reference / Object

Crystal wrapper for libcmark, the reference CommonMark C library.

md = CommonMark.new(text)

Render to another format:

md.to_html  # => HTML
md.to_man   # => man page

Constructors

new(text : String, sourcepos = false, hardbreaks = false, normalize = false, validate_utf8 = false, smart = false, unsafe = false)

Initializes a new CommonMark parser.

md = CommonMark.new(text, smart: true)
md.to_html

Options:

  • hardbreak — render softbreak elements as hard line breaks.
  • unsafe — keep raw HTML and unsafe links (javascript:, vbscript:, file:, and data:, except for image/png, image/gif, image/jpeg, or image/webp mime types). Raw HTML is replaced by a placeholder HTML comment. Unsafe links are replaced by empty strings.
  • sourcepos — include a data-sourcepos attribute on all block elements.
  • smart — convert straight quotes to curly, --- to em dashes, -- to en dashes.
  • normalize — normalize tree by consolidating adjacent text nodes.
  • validate_utf8 — Validate UTF-8 in the input before parsing, replacing illegal sequences with the replacement character U+FFFD.

Instance methods

hardbreaks?

Returns true if the hardbreaks option was set.

normalize?

Returns true if the normalize option was set.

parse
smart?

Returns true if the smart option was set.

sourcepos?

Returns true if the sourcepos option was set.

text

The original markdown text.

to_commonmark(width = 80)

Renders #text as a commonmark page.

to_html

Renders #text as HTML.

to_latex(width = 80)

Renders #text as a latex page.

to_man(width = 80)

Renders #text as a man page.

to_xml

Renders #text as XML.

unsafe?

Returns true if the unsafe option was set.

validate_utf8?

Returns true if the validate_utf8 option was set.