motion-markdown-it
Markdown parser done right. Fast and easy to extend. For use with Ruby and RubyMotion.
This gem is a port of the markdown-it Javascript package by Vitaly Puzrin and Alex Kocharin.
- 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
Beta
The gem is still a work in progress. There are several areas to get working better, including plugins and performance. It will track as closely as possible to fixes and enhancements in the main markdown-it implementation. Currently synced with markdown-it 4.0.3
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.
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 rulezz!')
Single line rendering, without paragraph wrap:
result = MarkdownIt::Parser.new.renderInline('__markdown-it__ rulezz!')
Init with presets and options
(*) preset define combination of active rules and options. Can be
:commonmark
, :zero
or :default
(if skipped). You can refer to the
markdown-it Javascript API docs for more details.
#--- 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 '' if the source string is not changed and should be escaped externaly.
highlight: lambda {|str, lang| return ''}
})
References / Thanks
Thanks to the authors of the original implementation in Javascript, markdown-it:
- Alex Kocharin github/rlidwka
- Vitaly Puzrin github/puzrin
and to John MacFarlane for his work on the CommonMark spec and reference implementations.
Related Links:
- https://github.com/jgm/CommonMark - reference CommonMark implementations in C & JS, also contains latest spec & online demo.
- http://talk.commonmark.org - CommonMark forum, good place to collaborate developers' efforts.