All Projects → gjtorikian → Commonmarker

gjtorikian / Commonmarker

Licence: mit
Ruby wrapper for libcmark (CommonMark parser)

Programming Languages

c
50402 projects - #5 most used programming language

Projects that are alternatives of or similar to Commonmarker

swift-markdownkit
A framework for parsing and transforming text in Markdown format written in Swift 5 for macOS, iOS, and Linux. The supported syntax is based on the CommonMark specification. The framework defines an abstract syntax for Markdown, provides a parser for parsing strings into abstract syntax trees, and comes with generators for creating HTML and attr…
Stars: ✭ 64 (-75.94%)
Mutual labels:  commonmark
Publish.jl
A universal document authoring package for Julia.
Stars: ✭ 86 (-67.67%)
Mutual labels:  commonmark
NeoMarkdigXaml
Markdig Xaml/Wpf Renderer
Stars: ✭ 18 (-93.23%)
Mutual labels:  commonmark
front-matter
The most featured front matter (yaml, json, neon, toml) parser and dumper for PHP.
Stars: ✭ 23 (-91.35%)
Mutual labels:  commonmark
comark
Comark is CommonMark (Markdown) library for Haxe
Stars: ✭ 14 (-94.74%)
Mutual labels:  commonmark
guile-commonmark
Implementation of CommonMark for Guile
Stars: ✭ 31 (-88.35%)
Mutual labels:  commonmark
clj-pdf-markdown
Library for rendering markdown to clj-pdf data-structure syntax.
Stars: ✭ 20 (-92.48%)
Mutual labels:  commonmark
mdformat
CommonMark compliant Markdown formatter
Stars: ✭ 90 (-66.17%)
Mutual labels:  commonmark
thegreatmarkdown
《了不起的 Markdown》
Stars: ✭ 44 (-83.46%)
Mutual labels:  commonmark
md-toc
Automatically generate a compliant table of contents for a markdown file to improve document readability
Stars: ✭ 18 (-93.23%)
Mutual labels:  commonmark
laravel-etched-blade
A package that uses blade templates to control how markdown is converted to HTML inside Laravel, as well as providing support for markdown files to Laravel views.
Stars: ✭ 19 (-92.86%)
Mutual labels:  commonmark
CommonMarkEmoji
CommonMark extension adds UTF-8 emoji with Github tag for the `league/commonmark` PHP Markdown parsing engine, based on the CommonMark spec.
Stars: ✭ 17 (-93.61%)
Mutual labels:  commonmark
cmark-scala
Parse, manipulate and render CommonMark in Scala Native
Stars: ✭ 13 (-95.11%)
Mutual labels:  commonmark
markdom
A markdown parser for laravel for making beautiful html
Stars: ✭ 42 (-84.21%)
Mutual labels:  commonmark
NativeMarkKit
NativeMark is a flavor of Markdown designed to be rendered by native apps.
Stars: ✭ 36 (-86.47%)
Mutual labels:  commonmark
paka.cmark
Lightweight CFFI-based Python bindings to cmark library (CommonMark implementation in C).
Stars: ✭ 23 (-91.35%)
Mutual labels:  commonmark
smu
Simple MarkUp - markdown/commonmark like syntax
Stars: ✭ 21 (-92.11%)
Mutual labels:  commonmark
jekyll-commonmark
CommonMark generator for Jekyll
Stars: ✭ 28 (-89.47%)
Mutual labels:  commonmark
goldmark-pdf
A PDF renderer for the goldmark markdown parser.
Stars: ✭ 83 (-68.8%)
Mutual labels:  commonmark
nwotable
One app for all your notes on all your devices.
Stars: ✭ 22 (-91.73%)
Mutual labels:  commonmark

CommonMarker

Build Status Gem Version

Ruby wrapper for libcmark-gfm, GitHub's fork of the reference parser for CommonMark. It passes all of the C tests, and is therefore spec-complete. It also includes extensions to the CommonMark spec as documented in the GitHub Flavored Markdown spec, such as support for tables, strikethroughs, and autolinking.

For more information on available extensions, see the documentation below.

Installation

Add this line to your application's Gemfile:

gem 'commonmarker'

And then execute:

$ bundle

Or install it yourself as:

$ gem install commonmarker

Usage

Converting to HTML

Call render_html on a string to convert it to HTML:

require 'commonmarker'
CommonMarker.render_html('Hi *there*', :DEFAULT)
# <p>Hi <em>there</em></p>\n

The second argument is optional--see below for more information.

Generating a document

You can also parse a string to receive a Document node. You can then print that node to HTML, iterate over the children, and other fun node stuff. For example:

require 'commonmarker'

doc = CommonMarker.render_doc('*Hello* world', :DEFAULT)
puts(doc.to_html) # <p>Hi <em>there</em></p>\n

doc.walk do |node|
  puts node.type # [:document, :paragraph, :text, :emph, :text]
end

The second argument is optional--see below for more information.

Example: walking the AST

You can use walk or each to iterate over nodes:

  • walk will iterate on a node and recursively iterate on a node's children.
  • each will iterate on a node and its children, but no further.
require 'commonmarker'

# parse the files specified on the command line
doc = CommonMarker.render_doc("# The site\n\n [GitHub](https://www.github.com)")

# Walk tree and print out URLs for links
doc.walk do |node|
  if node.type == :link
    printf("URL = %s\n", node.url)
  end
end

# Capitalize all regular text in headers
doc.walk do |node|
  if node.type == :header
    node.each do |subnode|
      if subnode.type == :text
        subnode.string_content = subnode.string_content.upcase
      end
    end
  end
end

# Transform links to regular text
doc.walk do |node|
  if node.type == :link
    node.insert_before(node.first_child)
    node.delete
  end
end

Creating a custom renderer

You can also derive a class from CommonMarker's HtmlRenderer class. This produces slower output, but is far more customizable. For example:

class MyHtmlRenderer < CommonMarker::HtmlRenderer
  def initialize
    super
    @headerid = 1
  end

  def header(node)
    block do
      out("<h", node.header_level, " id=\"", @headerid, "\">",
               :children, "</h", node.header_level, ">")
      @headerid += 1
    end
  end
end

myrenderer = MyHtmlRenderer.new
puts myrenderer.render(doc)

# Print any warnings to STDERR
renderer.warnings.each do |w|
  STDERR.write("#{w}\n")
end

Options

CommonMarker accepts the same options that CMark does, as symbols. Note that there is a distinction in CMark for "parse" options and "render" options, which are represented in the tables below.

Parse options

Name Description
:DEFAULT The default parsing system.
:UNSAFE Allow raw/custom HTML and unsafe links.
:FOOTNOTES Parse footnotes.
:LIBERAL_HTML_TAG Support liberal parsing of inline HTML tags.
:SMART Use smart punctuation (curly quotes, etc.).
:STRIKETHROUGH_DOUBLE_TILDE Parse strikethroughs by double tildes (compatibility with redcarpet)
:VALIDATE_UTF8 Replace illegal sequences with the replacement character U+FFFD.

Render options

Name Description
:DEFAULT The default rendering system.
:UNSAFE Allow raw/custom HTML and unsafe links.
:GITHUB_PRE_LANG Use GitHub-style <pre lang> for fenced code blocks.
:HARDBREAKS Treat \n as hardbreaks (by adding <br/>).
:NOBREAKS Translate \n in the source to a single whitespace.
:SOURCEPOS Include source position in rendered HTML.
:TABLE_PREFER_STYLE_ATTRIBUTES Use style insted of align for table cells.
:FULL_INFO_STRING Include full info strings of code blocks in separate attribute.
:FOOTNOTES Render footnotes.

Passing options

To apply a single option, pass it in as a symbol argument:

CommonMarker.render_doc("\"Hello,\" said the spider.", :SMART)
# <p>“Hello,” said the spider.</p>\n

To have multiple options applied, pass in an array of symbols:

CommonMarker.render_html("\"'Shelob' is my name.\"", [:HARDBREAKS, :SOURCEPOS])

For more information on these options, see the CMark documentation.

Extensions

Both render_html and render_doc take an optional third argument defining the extensions you want enabled as your CommonMark document is being processed. The documentation for these extensions are defined in this spec, and the rationale is provided in this blog post.

The available extensions are:

  • :table - This provides support for tables.
  • :tasklist - This provides support for task list items.
  • :strikethrough - This provides support for strikethroughs.
  • :autolink - This provides support for automatically converting URLs to anchor tags.
  • :tagfilter - This escapes several "unsafe" HTML tags, causing them to not have any effect.

Developing locally

After cloning the repo:

script/bootstrap
bundle exec rake compile

If there were no errors, you're done! Otherwise, make sure to follow the CMark dependency instructions.

Benchmarks

Some rough benchmarks:

$ bundle exec rake benchmark

input size = 11063727 bytes

redcarpet
  0.070000   0.020000   0.090000 (  0.079641)
github-markdown
  0.070000   0.010000   0.080000 (  0.083535)
commonmarker with to_html
  0.100000   0.010000   0.110000 (  0.111947)
commonmarker with ruby HtmlRenderer
  1.830000   0.030000   1.860000 (  1.866203)
kramdown
  4.610000   0.070000   4.680000 (  4.678398)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].