All Projects → kivikakk → Comrak

kivikakk / Comrak

Licence: other
CommonMark + GFM compatible Markdown parser and renderer

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to Comrak

Tui.editor
🍞📝 Markdown WYSIWYG Editor. GFM Standard + Chart & UML Extensible.
Stars: ✭ 14,016 (+3056.76%)
Mutual labels:  markdown, commonmark
Breakdance
It's time for your markup to get down! HTML to markdown converter. Breakdance is a highly pluggable, flexible and easy to use.
Stars: ✭ 418 (-5.86%)
Mutual labels:  markdown, commonmark
Vditor
♏ 一款浏览器端的 Markdown 编辑器,支持所见即所得(富文本)、即时渲染(类似 Typora)和分屏预览模式。An In-browser Markdown editor, support WYSIWYG (Rich Text), Instant Rendering (Typora-like) and Split View modes.
Stars: ✭ 3,773 (+749.77%)
Mutual labels:  markdown, commonmark
Commonmark
Create, parse, and render Markdown text according to the CommonMark specification
Stars: ✭ 147 (-66.89%)
Mutual labels:  markdown, commonmark
Md4c
C Markdown parser. Fast. SAX-like interface. Compliant to CommonMark specification.
Stars: ✭ 322 (-27.48%)
Mutual labels:  markdown, commonmark
Commonmark
Highly-extensible PHP Markdown parser which fully supports the CommonMark and GFM specs.
Stars: ✭ 2,128 (+379.28%)
Mutual labels:  markdown, commonmark
Lute
🎼 一款结构化的 Markdown 引擎,支持 Go 和 JavaScript。
Stars: ✭ 211 (-52.48%)
Mutual labels:  markdown, commonmark
Texme
Self-rendering Markdown + LaTeX documents
Stars: ✭ 1,970 (+343.69%)
Mutual labels:  markdown, commonmark
Lute
🎼 一款对中文语境优化的 Markdown 引擎,支持 Go 和 JavaScript。A structured Markdown engine that supports Go and JavaScript.
Stars: ✭ 222 (-50%)
Mutual labels:  markdown, commonmark
Markdownlint
A Node.js style checker and lint tool for Markdown/CommonMark files.
Stars: ✭ 2,828 (+536.94%)
Mutual labels:  markdown, commonmark
Markdown It
Markdown parser, done right. 100% CommonMark support, extensions, syntax plugins & high speed
Stars: ✭ 12,638 (+2746.4%)
Mutual labels:  markdown, commonmark
Commonmarkattributedstring
Create NSAttributedStrings from Markdown Text
Stars: ✭ 382 (-13.96%)
Mutual labels:  markdown, commonmark
Vditor
♏ 一款浏览器端的 Markdown 编辑器。
Stars: ✭ 1,742 (+292.34%)
Mutual labels:  markdown, commonmark
Markdown
CommonMark-compliant markdown parser and renderer in Go. This repository has moved to https://gitlab.com/golang-commonmark/markdown
Stars: ✭ 180 (-59.46%)
Mutual labels:  markdown, commonmark
Down
Blazing fast Markdown / CommonMark rendering in Swift, built upon cmark.
Stars: ✭ 1,895 (+326.8%)
Mutual labels:  markdown, commonmark
Markdownui
Render Markdown text in SwiftUI
Stars: ✭ 207 (-53.38%)
Mutual labels:  markdown, commonmark
Editor.md
The open source embeddable online markdown editor (component).
Stars: ✭ 11,741 (+2544.37%)
Mutual labels:  markdown, commonmark
Mdme
Self-rendering Markdown content
Stars: ✭ 140 (-68.47%)
Mutual labels:  markdown, commonmark
Markdig
A fast, powerful, CommonMark compliant, extensible Markdown processor for .NET
Stars: ✭ 2,730 (+514.86%)
Mutual labels:  markdown, commonmark
Markdown Writer Fx
Markdown Writer FX
Stars: ✭ 323 (-27.25%)
Mutual labels:  markdown, commonmark

Comrak

Build Status Spec Status: 671/671 Financial Contributors on Open Collective crates.io version docs.rs

Rust port of github's cmark-gfm.

Installation

Specify it as a requirement in Cargo.toml:

[dependencies]
comrak = "0.9"

Comrak supports Rust stable.

Mac & Linux Binaries

curl https://webinstall.dev/comrak | bash

Windows 10 Binaries

curl.exe -A "MS" https://webinstall.dev/comrak | powershell

Usage

$ comrak --help
comrak 0.9.1
Ashe Connor <[email protected]>
A 100% CommonMark-compatible GitHub Flavored Markdown parser and formatter

USAGE:
    comrak [FLAGS] [OPTIONS] [--] [FILE]...

FLAGS:
        --escape             Escape raw HTML instead of clobbering it
        --gfm                Enable GitHub-flavored markdown extensions strikethrough, tagfilter, table, autolink, and
                             tasklist. It also enables --github-pre-lang.
        --github-pre-lang    Use GitHub-style <pre lang> for code blocks
        --hardbreaks         Treat newlines as hard line breaks
    -h, --help               Prints help information
        --smart              Use smart punctuation
        --unsafe             Allow raw HTML and dangerous URLs
    -V, --version            Prints version information

OPTIONS:
    -c, --config-file <PATH>                    Path to config file containing command-line arguments, or `none'
                                                [default: /Users/kameliya/.config/comrak/config]
        --default-info-string <INFO>            Default value for fenced code block's info strings if none is given
    -e, --extension <EXTENSION>...              Specify an extension name to use [possible values: strikethrough,
                                                tagfilter, table, autolink, tasklist, superscript, footnotes,
                                                description-lists]
    -t, --to <FORMAT>                           Specify output format [default: html]  [possible values: html,
                                                commonmark]
        --front-matter-delimiter <DELIMITER>    Ignore front-matter that starts and ends with the given string
        --header-ids <PREFIX>                   Use the Comrak header IDs extension, with the given ID prefix
        --width <WIDTH>                         Specify wrap width (0 = nowrap) [default: 0]

ARGS:
    <FILE>...    The CommonMark file to parse; or standard input if none passed

By default, Comrak will attempt to read command-line options from a config file specified by --config-file.  This
behaviour can be disabled by passing --config-file none.  It is not an error if the file does not exist.

And there's a Rust interface. You can use comrak::markdown_to_html directly:

use comrak::{markdown_to_html, ComrakOptions};
assert_eq!(markdown_to_html("Hello, **世界**!", &ComrakOptions::default()),
           "<p>Hello, <strong>世界</strong>!</p>\n");

Or you can parse the input into an AST yourself, manipulate it, and then use your desired formatter:

extern crate comrak;
use comrak::{parse_document, format_html, Arena, ComrakOptions};
use comrak::nodes::{AstNode, NodeValue};

// The returned nodes are created in the supplied Arena, and are bound by its lifetime.
let arena = Arena::new();

let root = parse_document(
    &arena,
    "This is my input.\n\n1. Also my input.\n2. Certainly my input.\n",
    &ComrakOptions::default());

fn iter_nodes<'a, F>(node: &'a AstNode<'a>, f: &F)
    where F : Fn(&'a AstNode<'a>) {
    f(node);
    for c in node.children() {
        iter_nodes(c, f);
    }
}

iter_nodes(root, &|node| {
    match &mut node.data.borrow_mut().value {
        &mut NodeValue::Text(ref mut text) => {
            let orig = std::mem::replace(text, vec![]);
            *text = String::from_utf8(orig).unwrap().replace("my", "your").as_bytes().to_vec();
        }
        _ => (),
    }
});

let mut html = vec![];
format_html(root, &ComrakOptions::default(), &mut html).unwrap();

assert_eq!(
    String::from_utf8(html).unwrap(),
    "<p>This is your input.</p>\n\
     <ol>\n\
     <li>Also your input.</li>\n\
     <li>Certainly your input.</li>\n\
     </ol>\n");

Security

As with cmark and cmark-gfm, Comrak will scrub raw HTML and potentially dangerous links. This change was introduced in Comrak 0.4.0 in support of a safe-by-default posture.

To allow these, use the unsafe_ option (or --unsafe with the command line program). If doing so, we recommend the use of a sanitisation library like ammonia configured specific to your needs.

Extensions

Comrak supports the five extensions to CommonMark defined in the GitHub Flavored Markdown Spec:

Comrak additionally supports its own extensions, which are yet to be specced out (PRs welcome!):

  • Superscript
  • Header IDs
  • Footnotes
  • Description lists

By default none are enabled; they are individually enabled with each parse by setting the appropriate values in the ComrakOptions struct.

Related projects

Comrak's design goal is to model the upstream cmark-gfm as closely as possible in terms of code structure. The upside of this is that a change in cmark-gfm has a very predictable change in Comrak. Likewise, any bug in cmark-gfm is likely to be reproduced in Comrak. This could be considered a pro or a con, depending on your use case.

The downside, of course, is that the code is not what I'd call idiomatic Rust (so many RefCells), and while contributors and I have made it as fast as possible, it simply won't be as fast as some other CommonMark parsers depending on your use-case. Here are some other projects to consider:

  • Raph Levien's pulldown-cmark. It's very fast, uses a novel parsing algorithm, and doesn't construct an AST (but you can use it to make one if you want). Recent cargo doc uses this, as do many other projects in the ecosystem. It's not quite at 100% spec compatibility yet.
  • Ben Navetta's rcmark is a set of bindings to libcmark. It hasn't been updated in a while, though there's an open pull request.
  • Know of another library? Please open a PR to add it!

As far as I know, Comrak is the only library to implement all of the GitHub Flavored Markdown extensions to the spec, but this tends to only be important if you want to reproduce GitHub's Markdown rendering exactly, e.g. in a GitHub client app.

Contributing

Contributions are highly encouraged; where possible I practice Optimistic Merging as described by Peter Hintjens. Please keep the code of conduct in mind when interacting with this project.

Thank you to comrak's many contributors for PRs and issues opened!

Code Contributors

Financial Contributors

Become a financial contributor and help us sustain our community. [Contribute]

Individuals

Organizations

Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]

Contact

Ashe Connor <ashe kivikakk ee>

Legal

Copyright (c) 2017–2021, Ashe Connor. Licensed under the 2-Clause BSD License.

cmark itself is is copyright (c) 2014, John MacFarlane.

See COPYING for all the details.

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].