Top Related Projects
Determines which markup library to use to render a content file (e.g. README) on GitHub
CommonMark parser and renderer in JavaScript
A bidirectional Markdown to HTML to Markdown converter written in Javascript
A markdown parser and compiler. Built for speed.
Markdown parser, done right. Commonmark support, extensions, syntax plugins, high speed - all in one. Gulp and metalsmith plugins available. Used by Facebook, Docusaurus and many others! Use https://github.com/breakdance/breakdance for HTML-to-markdown conversion. Use https://github.com/jonschlinkert/markdown-toc to generate a table of contents.
Quick Overview
Redcarpet is a Ruby library for Markdown processing. It's designed to be fast, safe, and extensible, offering a wide range of features and customization options for parsing and rendering Markdown content.
Pros
- High performance and speed in Markdown processing
- Extensive customization options and support for various Markdown extensions
- Safe by default, with protection against XSS vulnerabilities
- Active maintenance and community support
Cons
- Learning curve for advanced customization
- Some features may require additional setup or configuration
- Occasional inconsistencies with other Markdown parsers in edge cases
- Limited built-in support for certain advanced Markdown features
Code Examples
- Basic Markdown rendering:
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
markdown.render("# Hello, world!")
- Custom renderer with specific options:
renderer = Redcarpet::Render::HTML.new(hard_wrap: true, link_attributes: { target: '_blank' })
markdown = Redcarpet::Markdown.new(renderer, autolink: true, tables: true)
markdown.render("Some *Markdown* text")
- Using a custom render class:
class CustomRender < Redcarpet::Render::HTML
def header(text, header_level)
"<h#{header_level} class='custom-header'>#{text}</h#{header_level}>"
end
end
markdown = Redcarpet::Markdown.new(CustomRender)
markdown.render("# Custom Header")
Getting Started
To use Redcarpet in your Ruby project:
-
Add to your Gemfile:
gem 'redcarpet'
-
Install the gem:
bundle install
-
Use in your Ruby code:
require 'redcarpet' markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true) html_output = markdown.render("# Hello\n\nThis is **Markdown**!")
This setup creates a Markdown parser with autolink and table support, then renders a simple Markdown string to HTML.
Competitor Comparisons
Determines which markup library to use to render a content file (e.g. README) on GitHub
Pros of Markup
- Supports multiple markup languages (Markdown, reStructuredText, textile, etc.)
- Integrates well with GitHub's ecosystem and rendering pipeline
- Actively maintained with regular updates and bug fixes
Cons of Markup
- Slower rendering performance compared to Redcarpet
- Less customizable and extensible than Redcarpet
- Larger codebase and dependencies, potentially increasing complexity
Code Comparison
Markup (Ruby):
GitHub::Markup.render('README.markdown', File.read('README.markdown'))
Redcarpet (Ruby):
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
markdown.render(File.read('README.md'))
Both examples demonstrate basic usage for rendering Markdown content. Markup's approach is more generalized, allowing for different file formats, while Redcarpet is specifically tailored for Markdown rendering.
Markup offers broader language support and tighter GitHub integration, making it suitable for projects requiring multiple markup formats. Redcarpet, on the other hand, excels in performance and customization for Markdown-specific use cases. The choice between the two depends on project requirements, with Markup being more versatile and Redcarpet offering better performance for Markdown-centric applications.
CommonMark parser and renderer in JavaScript
Pros of commonmark.js
- Written in JavaScript, making it easily integrable into web applications
- Strictly adheres to the CommonMark specification, ensuring consistent parsing
- Actively maintained with regular updates and improvements
Cons of commonmark.js
- May have slower performance compared to Redcarpet's C implementation
- Less feature-rich, focusing primarily on core Markdown functionality
- Potentially steeper learning curve for developers not familiar with JavaScript
Code Comparison
commonmark.js
var reader = new commonmark.Parser();
var writer = new commonmark.HtmlRenderer();
var parsed = reader.parse("Hello *world*");
var result = writer.render(parsed);
Redcarpet
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
result = markdown.render("Hello *world*")
Summary
commonmark.js is a JavaScript-based Markdown parser that strictly follows the CommonMark specification, making it ideal for web applications and environments where JavaScript is prevalent. It offers consistent parsing but may sacrifice some performance and advanced features compared to Redcarpet.
Redcarpet, on the other hand, is a Ruby-based Markdown parser with a C extension, providing excellent performance and a rich set of features. It's well-suited for Ruby applications and environments where speed is crucial, but may not be as easily integrated into web-based projects as commonmark.js.
A bidirectional Markdown to HTML to Markdown converter written in Javascript
Pros of Showdown
- Written in JavaScript, making it easily integrable into web applications
- Supports browser-side Markdown conversion, enabling real-time previews
- Extensible through plugins, allowing for customization of Markdown parsing
Cons of Showdown
- Generally slower performance compared to Redcarpet's C implementation
- Less comprehensive GitHub Flavored Markdown (GFM) support out of the box
- May have inconsistencies in parsing complex Markdown structures
Code Comparison
Redcarpet (Ruby):
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true)
html_output = markdown.render("# Hello, Markdown!")
Showdown (JavaScript):
var converter = new showdown.Converter();
var html_output = converter.makeHtml('# Hello, Markdown!');
Both libraries offer simple APIs for converting Markdown to HTML. Redcarpet uses a more object-oriented approach with configuration options passed during initialization, while Showdown uses a more functional style with a converter object.
Redcarpet's C core provides better performance for large-scale Markdown processing, making it suitable for server-side applications. Showdown's JavaScript implementation offers flexibility for client-side use, enabling features like live previews in web applications.
A markdown parser and compiler. Built for speed.
Pros of marked
- Written in JavaScript, making it ideal for Node.js and browser environments
- Faster parsing and rendering speed compared to Redcarpet
- More active development and community support
Cons of marked
- Less extensive feature set compared to Redcarpet
- May have some inconsistencies in parsing edge cases
- Limited support for custom renderers and extensions
Code Comparison
marked:
import { marked } from 'marked';
const html = marked.parse('# Heading\n\nParagraph text');
console.log(html);
Redcarpet:
require 'redcarpet'
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
html = markdown.render("# Heading\n\nParagraph text")
puts html
Summary
marked is a popular JavaScript-based Markdown parser, while Redcarpet is a Ruby-based alternative. marked excels in speed and is well-suited for JavaScript environments, but Redcarpet offers more extensive features and customization options. The choice between the two depends on the specific project requirements, programming language preference, and desired level of control over the parsing process.
Markdown parser, done right. Commonmark support, extensions, syntax plugins, high speed - all in one. Gulp and metalsmith plugins available. Used by Facebook, Docusaurus and many others! Use https://github.com/breakdance/breakdance for HTML-to-markdown conversion. Use https://github.com/jonschlinkert/markdown-toc to generate a table of contents.
Pros of Remarkable
- Written in JavaScript, making it more accessible for web developers
- Highly extensible with a plugin system
- Faster parsing and rendering performance
Cons of Remarkable
- Less mature and battle-tested compared to Redcarpet
- Smaller community and ecosystem
- May lack some advanced features present in Redcarpet
Code Comparison
Remarkable:
var md = new Remarkable();
var result = md.render('# Hello, world!');
Redcarpet:
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
result = markdown.render("# Hello, world!")
Both libraries offer simple APIs for rendering Markdown to HTML, but Remarkable's JavaScript implementation may be more familiar to web developers. Redcarpet, being a Ruby gem, is typically used in Ruby-based projects.
Remarkable's extensibility allows for easy customization:
md.use(plugin1).use(plugin2);
Redcarpet offers similar functionality through render options:
markdown = Redcarpet::Markdown.new(renderer, extensions: {})
While both libraries are powerful Markdown parsers, the choice between them often depends on the project's language requirements and specific feature needs.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Redcarpet is written with sugar, spice and everything nice
Redcarpet is a Ruby library for Markdown processing that smells like butterflies and popcorn.
This library is written by people
Redcarpet was written by Vicent MartÃ. It is maintained by Robin Dupret and Matt Rogers.
Redcarpet would not be possible without the Sundown library and its authors (Natacha Porté, Vicent MartÃ, and its many awesome contributors).
You can totally install it as a Gem
Redcarpet is readily available as a Ruby gem. It will build some native extensions, but the parser is standalone and requires no installed libraries. Starting with Redcarpet 3.0, the minimum required Ruby version is 1.9.2 (or Rubinius in 1.9 mode).
$ [sudo] gem install redcarpet
If you need to use it with Ruby 1.8.7, you will have to stick with 2.3.0:
$ [sudo] gem install redcarpet -v 2.3.0
The Redcarpet source is available at GitHub:
$ git clone git://github.com/vmg/redcarpet.git
And it's like really simple to use
The core of the Redcarpet library is the Redcarpet::Markdown
class. Each
instance of the class is attached to a Renderer
object; the Markdown class
performs parsing of a document and uses the attached renderer to generate
output.
The Redcarpet::Markdown
object is encouraged to be instantiated once with the
required settings, and reused between parses.
# Initializes a Markdown parser
markdown = Redcarpet::Markdown.new(renderer, extensions = {})
Here, the renderer
variable refers to a renderer object, inheriting
from Redcarpet::Render::Base
. If the given object has not been
instantiated, the library will do it with default arguments.
Rendering with the Markdown
object is done through Markdown#render
.
Unlike in the RedCloth API, the text to render is passed as an argument
and not stored inside the Markdown
instance, to encourage reusability.
Example:
markdown.render("This is *bongos*, indeed.")
# => "<p>This is <em>bongos</em>, indeed.</p>"
You can also specify a hash containing the Markdown extensions which the parser will identify. The following extensions are accepted:
-
:no_intra_emphasis
: do not parse emphasis inside of words. Strings such asfoo_bar_baz
will not generate<em>
tags. -
:tables
: parse tables, PHP-Markdown style. -
:fenced_code_blocks
: parse fenced code blocks, PHP-Markdown style. Blocks delimited with 3 or more~
or backticks will be considered as code, without the need to be indented. An optional language name may be added at the end of the opening fence for the code block. -
:autolink
: parse links even when they are not enclosed in<>
characters. Autolinks for the http, https and ftp protocols will be automatically detected. Email addresses and http links without protocol, but starting withwww
are also handled. -
:disable_indented_code_blocks
: do not parse usual markdown code blocks. Markdown converts text with four spaces at the front of each line to code blocks. This option prevents it from doing so. Recommended to use withfenced_code_blocks: true
. -
:strikethrough
: parse strikethrough, PHP-Markdown style. Two~
characters mark the start of a strikethrough, e.g.this is ~~good~~ bad
. -
:lax_spacing
: HTML blocks do not require to be surrounded by an empty line as in the Markdown standard. -
:space_after_headers
: A space is always required between the hash at the beginning of a header and its name, e.g.#this is my header
would not be a valid header. -
:superscript
: parse superscripts after the^
character; contiguous superscripts are nested together, and complex values can be enclosed in parenthesis, e.g.this is the 2^(nd) time
. -
:underline
: parse underscored emphasis as underlines.This is _underlined_ but this is still *italic*
. -
:highlight
: parse highlights.This is ==highlighted==
. It looks like this:<mark>highlighted</mark>
-
:quote
: parse quotes.This is a "quote"
. It looks like this:<q>quote</q>
-
:footnotes
: parse footnotes, PHP-Markdown style. A footnote works very much like a reference-style link: it consists of a marker next to the text (e.g.This is a sentence.[^1]
) and a footnote definition on its own line anywhere within the document (e.g.[^1]: This is a footnote.
).
Example:
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true)
Darling, I packed you a couple renderers for lunch
Redcarpet comes with two built-in renderers, Redcarpet::Render::HTML
and
Redcarpet::Render::XHTML
, which output HTML and XHTML, respectively. These
renderers are actually implemented in C and hence offer brilliant
performance â several degrees of magnitude faster than other Ruby Markdown
solutions.
All the rendering flags that previously applied only to HTML output have
now been moved to the Redcarpet::Render::HTML
class, and may be enabled when
instantiating the renderer:
Redcarpet::Render::HTML.new(render_options = {})
Initializes an HTML renderer. The following flags are available:
-
:filter_html
: do not allow any user-inputted HTML in the output. -
:no_images
: do not generate any<img>
tags. -
:no_links
: do not generate any<a>
tags. -
:no_styles
: do not generate any<style>
tags. -
:escape_html
: escape any HTML tags. This option has precedence over:no_styles
,:no_links
,:no_images
and:filter_html
which means that any existing tag will be escaped instead of being removed. -
:safe_links_only
: only generate links for protocols which are considered safe. -
:with_toc_data
: add HTML anchors to each header in the output HTML, to allow linking to each section. -
:hard_wrap
: insert HTML<br>
tags inside paragraphs where the original Markdown document had newlines (by default, Markdown ignores these newlines). -
:xhtml
: output XHTML-conformant tags. This option is always enabled in theRender::XHTML
renderer. -
:prettify
: add prettyprint classes to<code>
tags for google-code-prettify. -
:link_attributes
: hash of extra attributes to add to links.
Example:
renderer = Redcarpet::Render::HTML.new(no_links: true, hard_wrap: true)
The HTML
renderer has an alternate version, Redcarpet::Render::HTML_TOC
,
which will output a table of contents in HTML based on the headers of the
Markdown document.
When instantiating this render object, you can optionally pass a nesting_level
option which takes an integer or a range and allows you to make it render only
headers at certain levels.
Redcarpet also includes a plaintext renderer, Redcarpet::Render::StripDown
, that
strips out all the formatting:
require 'redcarpet'
require 'redcarpet/render_strip'
markdown = Redcarpet::Markdown.new(Redcarpet::Render::StripDown)
markdown.render("**This** _is_ an [example](http://example.org/).")
# => "This is an example (http://example.org/)."
And you can even cook your own
Custom renderers are created by inheriting from an existing renderer. The
built-in renderers, HTML
and XHTML
may be extended as such:
# Create a custom renderer that sets a custom class for block-quotes.
class CustomRender < Redcarpet::Render::HTML
def block_quote(quote)
%(<blockquote class="my-custom-class">#{quote}</blockquote>)
end
end
markdown = Redcarpet::Markdown.new(CustomRender, fenced_code_blocks: true)
But new renderers can also be created from scratch by extending the abstract
base class Redcarpet::Render::Base
(see lib/redcarpet/render_man.rb
for
an example implementation of a Manpage renderer):
class ManPage < Redcarpet::Render::Base
# you get the drill -- keep going from here
end
The following instance methods may be implemented by the renderer:
Block-level calls
If the return value of the method is nil
, the block will be skipped.
Therefore, make sure that your renderer has at least a paragraph
method
implemented. If the method for a document element is not implemented, the
block will be skipped.
Example:
class RenderWithoutCode < Redcarpet::Render::HTML
def block_code(code, language)
nil
end
end
- block_code(code, language)
- block_quote(quote)
- block_html(raw_html)
- footnotes(content)
- footnote_def(content, number)
- header(text, header_level)
- hrule()
- list(contents, list_type)
- list_item(text, list_type)
- paragraph(text)
- table(header, body)
- table_row(content)
- table_cell(content, alignment, header)
Span-level calls
A return value of nil
will not output any data. If the method for
a document element is not implemented, the contents of the span will
be copied verbatim:
- autolink(link, link_type)
- codespan(code)
- double_emphasis(text)
- emphasis(text)
- image(link, title, alt_text)
- linebreak()
- link(link, title, content)
- raw_html(raw_html)
- triple_emphasis(text)
- strikethrough(text)
- superscript(text)
- underline(text)
- highlight(text)
- quote(text)
- footnote_ref(number)
Note: When overriding a renderer's method, be sure to return a HTML element with a level that matches the level of that method (e.g. return a block element when overriding a block-level callback). Otherwise, the output may be unexpected.
Low level rendering
- entity(text)
- normal_text(text)
Header of the document
Rendered before any another elements:
- doc_header()
Footer of the document
Rendered after all the other elements:
- doc_footer()
Pre/post-process
Special callback: preprocess or postprocess the whole document before or after the rendering process begins:
- preprocess(full_document)
- postprocess(full_document)
You can look at "How to extend the Redcarpet 2 Markdown library?" for some more explanations.
Also, now our Pants are much smarter
Redcarpet 2 comes with a standalone SmartyPants implementation. It is fully compliant with the original implementation. It is the fastest SmartyPants parser there is, with a difference of several orders of magnitude.
The SmartyPants parser can be found in Redcarpet::Render::SmartyPants
. It has
been implemented as a module, so it can be used standalone or as a mixin.
When mixed with a Renderer class, it will override the postprocess
method
to perform SmartyPants replacements once the rendering is complete.
# Mixin
class HTMLWithPants < Redcarpet::Render::HTML
include Redcarpet::Render::SmartyPants
end
# Standalone
Redcarpet::Render::SmartyPants.render("<p>Oh SmartyPants, you're so crazy...</p>")
SmartyPants works on top of already-rendered HTML, and will ignore replacements
inside the content of HTML tags and inside specific HTML blocks (pre
, code
,
var
, samp
, kbd
, math
, script
, style
).
What? You really want to mix Markdown renderers?
Redcarpet used to be a drop-in replacement for Redcloth. This is no longer the case since version 2 -- it now has its own API, but retains the old name. Yes, that does mean that Redcarpet is not backwards-compatible with the 1.X versions.
Each renderer has its own API and its own set of extensions: you should choose one (it doesn't have to be Redcarpet, though that would be great!), write your software accordingly, and force your users to install it. That's the only way to have reliable and predictable Markdown output on your program.
Markdown is already ill-specified enough; if you create software that is renderer-independent, the results will be completely unreliable!
Still, if major forces (let's say, tornadoes or other natural disasters) force you to keep a Markdown-compatibility layer, Redcarpet also supports this:
require 'redcarpet/compat'
Requiring the compatibility library will declare a Markdown
class with the
classical RedCloth API, e.g.
Markdown.new('this is my text').to_html
This class renders 100% standards compliant Markdown with 0 extensions. Nada. Don't even try to enable extensions with a compatibility layer, because that's a maintenance nightmare and won't work.
On a related topic: if your Markdown gem has a lib/markdown.rb
file that
monkeypatches the Markdown class, you're a terrible human being. Just saying.
Boring legal stuff
Copyright (c) 2011-2016, Vicent MartÃ
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Top Related Projects
Determines which markup library to use to render a content file (e.g. README) on GitHub
CommonMark parser and renderer in JavaScript
A bidirectional Markdown to HTML to Markdown converter written in Javascript
A markdown parser and compiler. Built for speed.
Markdown parser, done right. Commonmark support, extensions, syntax plugins, high speed - all in one. Gulp and metalsmith plugins available. Used by Facebook, Docusaurus and many others! Use https://github.com/breakdance/breakdance for HTML-to-markdown conversion. Use https://github.com/jonschlinkert/markdown-toc to generate a table of contents.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot