Convert Figma logo to code with AI

commonmark logocmark

CommonMark parsing and rendering library and program in C

1,623
540
1,623
65

Top Related Projects

CommonMark spec, with reference implementations in C and JavaScript

32,762

A markdown parser and compiler. Built for speed.

Markdown parser, done right. 100% CommonMark support, extensions, syntax plugins & high speed

14,196

A bidirectional Markdown to HTML to Markdown converter written in Javascript

Blackfriday: a markdown processor for Go

Quick Overview

cmark is a CommonMark parsing and rendering library and program in C. It provides a fast and accurate implementation of the CommonMark spec for standardized Markdown. The project aims to be highly portable and easy to integrate into other applications.

Pros

  • High performance and efficiency
  • Strict adherence to the CommonMark specification
  • Extensive test suite ensuring reliability
  • Easy to integrate into other projects due to its C implementation

Cons

  • Limited flexibility for custom extensions beyond CommonMark
  • Requires C knowledge for deep customization or integration
  • May be overkill for simple Markdown parsing needs
  • Documentation could be more comprehensive for advanced usage

Code Examples

  1. Basic parsing and rendering:
#include <stdio.h>
#include <stdlib.h>
#include "cmark.h"

int main() {
    const char *markdown = "# Hello, *world*!";
    char *html = cmark_markdown_to_html(markdown, strlen(markdown), CMARK_OPT_DEFAULT);
    printf("%s\n", html);
    free(html);
    return 0;
}
  1. Creating and manipulating a document tree:
#include "cmark.h"

int main() {
    cmark_node *doc = cmark_parse_document("# Heading\n\nParagraph", 19, CMARK_OPT_DEFAULT);
    cmark_node *paragraph = cmark_node_new(CMARK_NODE_PARAGRAPH);
    cmark_node_append_child(doc, paragraph);
    cmark_node_free(doc);
    return 0;
}
  1. Custom HTML rendering:
#include "cmark.h"

static void print_node(cmark_node *node) {
    char *xml = cmark_render_xml(node, CMARK_OPT_DEFAULT);
    printf("%s", xml);
    free(xml);
}

int main() {
    cmark_node *doc = cmark_parse_document("# Custom rendering", 19, CMARK_OPT_DEFAULT);
    print_node(doc);
    cmark_node_free(doc);
    return 0;
}

Getting Started

To use cmark in your project:

  1. Install cmark:

    git clone https://github.com/commonmark/cmark.git
    cd cmark
    mkdir build
    cd build
    cmake ..
    make
    sudo make install
    
  2. Include cmark in your C project:

    #include <cmark.h>
    
  3. Compile with cmark:

    gcc your_file.c -lcmark -o your_program
    
  4. Run your program:

    ./your_program
    

Competitor Comparisons

CommonMark spec, with reference implementations in C and JavaScript

Pros of commonmark-spec

  • Comprehensive specification and test suite for CommonMark
  • Provides a clear reference for implementers and users
  • Includes extensive examples and edge cases

Cons of commonmark-spec

  • Not a functional implementation, requires separate parser/renderer
  • May be more complex for casual users to understand

Code comparison

commonmark-spec (example from test suite):

*foo bar*

*foo*

cmark (C implementation):

cmark_node *document = cmark_parse_document(buffer, len, CMARK_OPT_DEFAULT);
char *html = cmark_render_html(document, CMARK_OPT_DEFAULT);

Summary

commonmark-spec serves as the definitive specification for CommonMark, offering a comprehensive test suite and examples. It's invaluable for implementers and those seeking a deep understanding of the format. However, it's not a functional parser or renderer on its own.

cmark, on the other hand, is a reference implementation in C, providing a practical, high-performance CommonMark parser and renderer. It's more suitable for direct integration into projects but may not cover all edge cases as thoroughly as the spec.

Choosing between them depends on whether you need a specification (commonmark-spec) or a working implementation (cmark) for your project.

32,762

A markdown parser and compiler. Built for speed.

Pros of marked

  • Written in JavaScript, making it easy to integrate into web applications
  • Highly extensible with plugins and custom renderers
  • Faster parsing and rendering compared to cmark

Cons of marked

  • Less strict adherence to CommonMark specification
  • May produce inconsistent output for edge cases
  • Lacks some advanced features available in cmark

Code comparison

marked:

import { marked } from 'marked';

const html = marked('# Hello, world!');
console.log(html);

cmark:

#include <stdio.h>
#include <stdlib.h>
#include "cmark.h"

int main() {
    char *html = cmark_markdown_to_html("# Hello, world!", 15, CMARK_OPT_DEFAULT);
    printf("%s\n", html);
    free(html);
    return 0;
}

Key differences

  • Language: marked is written in JavaScript, while cmark is written in C
  • Usage: marked is typically used in web environments, cmark in various applications
  • Specification: cmark strictly follows CommonMark, marked is more flexible
  • Performance: cmark generally offers better performance for large documents
  • Ecosystem: marked has a larger ecosystem of plugins and extensions

Markdown parser, done right. 100% CommonMark support, extensions, syntax plugins & high speed

Pros of markdown-it

  • Written in JavaScript, making it easy to integrate into web applications
  • Highly extensible with a plugin system for custom syntax and features
  • Faster parsing and rendering compared to many other JavaScript-based Markdown parsers

Cons of markdown-it

  • Less strict adherence to CommonMark specification compared to cmark
  • Potentially larger bundle size when used in browser environments
  • May require additional configuration to match cmark's output in some cases

Code comparison

markdown-it:

const md = require('markdown-it')();
const result = md.render('# Hello, world!');

cmark:

#include <stdio.h>
#include "cmark.h"

int main() {
    char *html = cmark_markdown_to_html("# Hello, world!", 15, CMARK_OPT_DEFAULT);
    printf("%s", html);
    free(html);
    return 0;
}

Additional notes

Both markdown-it and cmark are popular Markdown parsing libraries, but they serve different use cases. markdown-it is ideal for JavaScript-based projects, especially web applications, while cmark is better suited for applications requiring a C library or stricter CommonMark compliance.

14,196

A bidirectional Markdown to HTML to Markdown converter written in Javascript

Pros of Showdown

  • Written in JavaScript, making it easy to integrate into web applications
  • Supports a wide range of Markdown extensions and flavors
  • Active community and regular updates

Cons of Showdown

  • Slower performance compared to cmark, especially for large documents
  • Less strict adherence to CommonMark specification
  • Larger file size, which may impact load times in web applications

Code Comparison

Showdown

var converter = new showdown.Converter();
var html = converter.makeHtml('# Hello, Markdown!');

cmark

char *html = cmark_markdown_to_html("# Hello, Markdown!", 18, CMARK_OPT_DEFAULT);

Key Differences

  • cmark is written in C, offering better performance but less flexibility
  • Showdown provides more customization options and extensions
  • cmark strictly follows the CommonMark specification
  • Showdown is more suitable for web-based projects, while cmark is better for command-line tools and applications requiring high performance

Both projects have their strengths, and the choice between them depends on specific project requirements, performance needs, and the development environment.

Blackfriday: a markdown processor for Go

Pros of Blackfriday

  • Written in Go, offering better performance for Go-based applications
  • More extensive feature set, including support for custom renderers and extensions
  • Active development with frequent updates and community contributions

Cons of Blackfriday

  • Less strict adherence to CommonMark specification
  • May produce inconsistent output compared to other CommonMark implementations
  • Larger codebase, potentially making it more challenging to maintain and debug

Code Comparison

Blackfriday:

import "github.com/russross/blackfriday/v2"

markdown := []byte("# Hello, World!")
html := blackfriday.Run(markdown)

cmark:

#include <cmark.h>

const char *markdown = "# Hello, World!";
char *html = cmark_markdown_to_html(markdown, strlen(markdown), 0);

Both libraries provide simple APIs for converting Markdown to HTML, but Blackfriday's Go implementation may be more appealing for Go developers. cmark, being written in C, offers a lower-level interface and potentially better cross-language compatibility.

While Blackfriday provides more features and customization options, cmark focuses on strict CommonMark compliance and simplicity. The choice between the two depends on specific project requirements, language preferences, and the importance of CommonMark compatibility.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

cmark

CI
tests

cmark is the C reference implementation of CommonMark, a rationalized version of Markdown syntax with a spec. (For the JavaScript reference implementation, see commonmark.js.)

It provides a shared library (libcmark) with functions for parsing CommonMark documents to an abstract syntax tree (AST), manipulating the AST, and rendering the document to HTML, groff man, LaTeX, CommonMark, or an XML representation of the AST. It also provides a command-line program (cmark) for parsing and rendering CommonMark documents.

Advantages of this library:

  • Portable. The library and program are written in standard C99 and have no external dependencies. They have been tested with MSVC, gcc, tcc, and clang.

  • Fast. cmark can render a Markdown version of War and Peace in the blink of an eye (127 milliseconds on a ten year old laptop, vs. 100-400 milliseconds for an eye blink). In our benchmarks, cmark is 10,000 times faster than the original Markdown.pl, and on par with the very fastest available Markdown processors.

  • Accurate. The library passes all CommonMark conformance tests.

  • Standardized. The library can be expected to parse CommonMark the same way as any other conforming parser. So, for example, you can use commonmark.js on the client to preview content that will be rendered on the server using cmark.

  • Robust. The library has been extensively fuzz-tested using american fuzzy lop. The test suite includes pathological cases that bring many other Markdown parsers to a crawl (for example, thousands-deep nested bracketed text or block quotes).

  • Flexible. CommonMark input is parsed to an AST which can be manipulated programmatically prior to rendering.

  • Multiple renderers. Output in HTML, groff man, LaTeX, CommonMark, and a custom XML format is supported. And it is easy to write new renderers to support other formats.

  • Free. BSD2-licensed.

It is easy to use libcmark in python, lua, ruby, and other dynamic languages: see the wrappers/ subdirectory for some simple examples.

There are also libraries that wrap libcmark for Go, Haskell, Ruby, Lua, Perl, Python, R, Scala and PHP.

Installing

Building the C program (cmark) and shared library (libcmark) requires cmake. If you modify scanners.re, then you will also need re2c (>= 0.14.2), which is used to generate scanners.c from scanners.re. We have included a pre-generated scanners.c in the repository to reduce build dependencies.

If you have GNU make, you can simply make, make test, and make install. This calls cmake to create a Makefile in the build directory, then uses that Makefile to create the executable and library. The binaries can be found in build/src. The default installation prefix is /usr/local. To change the installation prefix, pass the INSTALL_PREFIX variable if you run make for the first time: make INSTALL_PREFIX=path.

For a more portable method, you can use cmake manually. cmake knows how to create build environments for many build systems. For example, on FreeBSD:

mkdir build
cd build
cmake ..  # optionally: -DCMAKE_INSTALL_PREFIX=path
make      # executable will be created as build/src/cmark
make test
make install

Or, to create Xcode project files on OSX:

mkdir build
cd build
cmake -G Xcode ..
open cmark.xcodeproj

The GNU Makefile also provides a few other targets for developers. To run a benchmark:

make bench

For more detailed benchmarks:

make newbench

To run a test for memory leaks using valgrind:

make leakcheck

To reformat source code using clang-format:

make format

To run a "fuzz test" against ten long randomly generated inputs:

make fuzztest

To do a more systematic fuzz test with american fuzzy lop:

AFL_PATH=/path/to/afl_directory make afl

Fuzzing with libFuzzer is also supported. The fuzzer can be run with:

make libFuzzer

To make a release tarball and zip archive:

make archive

Installing (Windows)

To compile with MSVC and NMAKE:

nmake /f Makefile.nmake

You can cross-compile a Windows binary and dll on linux if you have the mingw32 compiler:

make mingw

The binaries will be in build-mingw/windows/bin.

Usage

Instructions for the use of the command line program and library can be found in the man pages in the man subdirectory.

Security

By default, the library will scrub raw HTML and potentially dangerous links (javascript:, vbscript:, data:, file:).

To allow these, use the option CMARK_OPT_UNSAFE (or --unsafe) with the command line program. If doing so, we recommend you use a HTML sanitizer specific to your needs to protect against XSS attacks.

Contributing

There is a forum for discussing CommonMark; you should use it instead of github issues for questions and possibly open-ended discussions. Use the github issue tracker only for simple, clear, actionable issues.

Authors

John MacFarlane wrote the original library and program. The block parsing algorithm was worked out together with David Greenspan. Vicent Marti optimized the C implementation for performance, increasing its speed tenfold. Kārlis Gaņģis helped work out a better parsing algorithm for links and emphasis, eliminating several worst-case performance issues. Nick Wellnhofer contributed many improvements, including most of the C library's API and its test harness.

NPM DownloadsLast 30 Days