motion-markdown-it

Gem Version

Ruby/RubyMotion version of Markdown-it (CommonMark compliant and extendable)

This gem is a port of the markdown-it Javascript package by Vitaly Puzrin and Alex Kocharin. Currently synced with markdown-it 4.1.0

Javascript Live demo

  • Supports the CommonMark spec + syntax extensions + sugar (URL autolinking, typographer)
  • Configurable syntax. You can add new rules and even replace existing ones.
  • High speed
  • Community-written plugins

Benefit

The benefit of this project, for me at least, is to have a standardized CommonMark compliant, fast, and extendable, Markdown parser which can be used from Javascript, Ruby, and/or RubyMotion, as the development situation warrants.

Performance

Performance is still an issue. While it performs reasonably well with small to medium files, it degrades for large files. Work in progres...

Table of content

Install

Ruby

Add it to your project's Gemfile

gem 'motion-markdown-it'

and run bundle install

RubyMotion

Add it to your project's Gemfile

gem 'motion-markdown-it'

Edit your Rakefile and add

require 'motion-markdown-it'

and run bundle install

Usage examples

Simple

parser = MarkdownIt::Parser.new(:commonmark, { html: false })
parser.render('# markdown-it in **Ruby**')

Single line rendering, without paragraph wrap:

result = MarkdownIt::Parser.new.renderInline('__markdown-it__ in Ruby')

Init with presets and options

(*) presets define combinations of active rules and options. Can be :commonmark, :zero or :default (if skipped).

#--- commonmark mode
parser = MarkdownIt::Parser.new(:commonmark)

#--- default mode
parser = MarkdownIt::Parser.new

#--- enable everything
parser = MarkdownIt::Parser.new({ html: true, linkify: true, typographer: true })

#--- full options list (defaults)
parser = MarkdownIt::Parser.new({
  html:         false,        # Enable HTML tags in source
  xhtmlOut:     false,        # Use '/' to close single tags (<br />).
                              # This is only for full CommonMark compatibility.
  breaks:       false,        # Convert '\n' in paragraphs into <br>
  langPrefix:   'language-',  # CSS language prefix for fenced blocks. Can be
                              # useful for external highlighters.
  linkify:      false,        # Autoconvert URL-like text to links

  # Enable some language-neutral replacement + quotes beautification
  typographer:  false,

  # Double + single quotes replacement pairs, when typographer enabled,
  # and smartquotes on. Set doubles to '«»' for Russian, '„“' for German.
  quotes: '“”‘’',

  # Highlighter function. Should return escaped HTML,
  # or nil if the source string is not changed and should be escaped externaly.
  highlight: lambda {|str, lang| return nil}
})

References / Thanks

Thanks to the authors of the original implementation in Javascript, markdown-it:

and to John MacFarlane for his work on the CommonMark spec and reference implementations.

Related Links:

License

MIT