cmark-gfm
GitHub's fork of cmark, a CommonMark parsing and rendering library and program in C
Top Related Projects
CommonMark parsing and rendering library and program in C
CommonMark spec, with reference implementations in C and JavaScript
Markdown parser, done right. 100% CommonMark support, extensions, syntax plugins & high speed
A bidirectional Markdown to HTML to Markdown converter written in Javascript
A markdown parser and compiler. Built for speed.
Blackfriday: a markdown processor for Go
Quick Overview
GitHub's cmark-gfm is a C implementation of CommonMark with GitHub Flavored Markdown (GFM) extensions. It provides a fast and compliant parser for Markdown, supporting standard CommonMark syntax as well as GitHub-specific features like tables, task lists, and autolinks.
Pros
- High performance and efficiency due to C implementation
- Compliant with CommonMark specification and GFM extensions
- Well-maintained and actively developed by GitHub
- Provides both library and command-line interface options
Cons
- Requires C programming knowledge for direct library usage
- May have a steeper learning curve compared to pure JavaScript alternatives
- Limited built-in support for custom extensions beyond GFM
- Dependency management can be more complex in some environments
Code Examples
- Basic Markdown to HTML conversion:
#include <stdio.h>
#include <string.h>
#include "cmark-gfm.h"
int main() {
const char *markdown = "# Hello, world!\n\nThis is **bold** text.";
char *html = cmark_markdown_to_html(markdown, strlen(markdown), CMARK_OPT_DEFAULT);
printf("%s\n", html);
free(html);
return 0;
}
- Parsing Markdown and traversing the AST:
#include "cmark-gfm.h"
void print_node_type(cmark_node *node) {
printf("Node type: %s\n", cmark_node_get_type_string(node));
}
int main() {
const char *markdown = "# Heading\n\nParagraph";
cmark_node *doc = cmark_parse_document(markdown, strlen(markdown), CMARK_OPT_DEFAULT);
cmark_iter *iter = cmark_iter_new(doc);
cmark_event_type ev_type;
cmark_node *node;
while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) {
node = cmark_iter_get_node(iter);
if (ev_type == CMARK_EVENT_ENTER) {
print_node_type(node);
}
}
cmark_iter_free(iter);
cmark_node_free(doc);
return 0;
}
- Using GFM extensions:
#include "cmark-gfm.h"
#include "cmark-gfm-core-extensions.h"
int main() {
cmark_gfm_core_extensions_ensure_registered();
const char *markdown = "| Header 1 | Header 2 |\n|----------|----------|\n| Cell 1 | Cell 2 |";
char *html = cmark_markdown_to_html(markdown, strlen(markdown),
CMARK_OPT_DEFAULT | CMARK_OPT_TABLE_PREFER_STYLE_ATTRIBUTES);
printf("%s\n", html);
free(html);
return 0;
}
Getting Started
-
Clone the repository:
git clone https://github.com/github/cmark-gfm.git cd cmark-gfm -
Build the project:
mkdir build cd build cmake .. make -
Install (optional):
sudo make install -
Use in your C project:
#include <cmark-gfm.h> // Your code here -
Compile with:
gcc your_file.c -lcmark-gfm -o your_program
Competitor Comparisons
CommonMark parsing and rendering library and program in C
Pros of cmark
- More focused on the CommonMark specification, providing a reference implementation
- Potentially more stable and consistent with the core CommonMark standard
- Simpler codebase, easier to understand and maintain for core CommonMark features
Cons of cmark
- Lacks GitHub Flavored Markdown (GFM) extensions
- May have fewer features and customization options
- Potentially slower development cycle for new Markdown features
Code Comparison
cmark:
void cmark_node_free(cmark_node *node) {
cmark_mem *mem = node->mem;
if (node->last_child) {
cmark_node *child = node->first_child;
while (child) {
cmark_node *next = child->next;
cmark_node_free(child);
child = next;
}
}
...
}
cmark-gfm:
void cmark_node_free(cmark_node *node) {
cmark_mem *mem = node->mem;
if (node->last_child) {
cmark_node *child = node->first_child;
while (child) {
cmark_node *next = child->next;
cmark_node_free(child);
child = next;
}
}
...
}
The core node freeing function is identical in both projects, reflecting their shared codebase. The main differences lie in the additional features and extensions implemented in cmark-gfm, which are not present in the core cmark library.
CommonMark spec, with reference implementations in C and JavaScript
Pros of commonmark-spec
- Focuses on the specification itself, providing a clear reference for CommonMark
- Includes extensive test cases for validating CommonMark implementations
- Serves as the authoritative source for the CommonMark standard
Cons of commonmark-spec
- Lacks a direct implementation of the CommonMark parser
- May require additional effort to integrate into projects compared to cmark-gfm
- Updates to the specification might lag behind practical implementations
Code Comparison
commonmark-spec (example test case):
example: 189
markdown: |
[foo]: /url "title"
[foo]
html: |
<p><a href="/url" title="title">foo</a></p>
cmark-gfm (example C code snippet):
cmark_node *document = cmark_parse_document(buffer, len, CMARK_OPT_DEFAULT);
char *html = cmark_render_html(document, CMARK_OPT_DEFAULT);
printf("%s", html);
free(html);
cmark_node_free(document);
Summary
commonmark-spec is ideal for those seeking a comprehensive understanding of the CommonMark standard and for developing compliant implementations. cmark-gfm, on the other hand, provides a ready-to-use implementation with GitHub Flavored Markdown extensions, making it more suitable for direct integration into projects requiring Markdown parsing and rendering capabilities.
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 cmark-gfm
Cons of markdown-it
- Less strict adherence to CommonMark specification
- May produce different output for edge cases compared to cmark-gfm
- Requires more setup and configuration for GitHub Flavored Markdown support
Code Comparison
markdown-it:
const md = require('markdown-it')();
const result = md.render('# Hello, world!');
cmark-gfm:
#include <cmark-gfm.h>
char *result = cmark_markdown_to_html("# Hello, world!", 15, CMARK_OPT_DEFAULT);
Both examples demonstrate basic usage for rendering Markdown to HTML. markdown-it uses a JavaScript API, while cmark-gfm uses a C API. The markdown-it example is more concise and easier to integrate into JavaScript projects, while cmark-gfm offers a lower-level implementation that can be compiled and used in various programming languages through bindings.
A bidirectional Markdown to HTML to Markdown converter written in Javascript
Pros of Showdown
- JavaScript-based, making it easy to integrate into web applications
- Highly extensible with a plugin system for custom syntax and features
- Supports both client-side and server-side usage
Cons of Showdown
- Generally slower performance compared to cmark-gfm
- Less strict adherence to CommonMark specification
- May produce inconsistent output across different versions or configurations
Code Comparison
Showdown (JavaScript):
var converter = new showdown.Converter();
var html = converter.makeHtml('# Hello, Markdown!');
cmark-gfm (C):
char *html = cmark_markdown_to_html("# Hello, Markdown!", 18, CMARK_OPT_DEFAULT);
Key Differences
- cmark-gfm is written in C, offering better performance but less flexibility
- Showdown provides more customization options and is easier to integrate into JavaScript projects
- cmark-gfm follows the GitHub Flavored Markdown spec more closely
- Showdown has a larger ecosystem of plugins and extensions
- cmark-gfm is maintained by GitHub and used in their products, ensuring compatibility with GitHub's Markdown rendering
A markdown parser and compiler. Built for speed.
Pros of marked
- Written in JavaScript, making it easy to integrate into Node.js and browser-based applications
- Highly extensible with a plugin system for custom rendering and parsing
- Faster parsing and rendering compared to cmark-gfm in most scenarios
Cons of marked
- Less strict adherence to CommonMark and GFM specifications
- May produce inconsistent output for edge cases or complex Markdown structures
- Lacks some advanced features present in cmark-gfm, such as footnotes and task lists
Code Comparison
marked:
import { marked } from 'marked';
const html = marked('# Hello, world!');
console.log(html);
cmark-gfm:
#include <stdio.h>
#include <stdlib.h>
#include "cmark-gfm.h"
int main() {
char *html = cmark_markdown_to_html("# Hello, world!", 15, CMARK_OPT_DEFAULT);
printf("%s\n", html);
free(html);
return 0;
}
The code examples demonstrate the simplicity of using marked in JavaScript environments, while cmark-gfm requires C programming and memory management. marked offers a more straightforward API for web developers, whereas cmark-gfm provides lower-level control and potentially better performance for system-level applications.
Blackfriday: a markdown processor for Go
Pros of Blackfriday
- Written in Go, offering better performance for Go-based applications
- More customizable with various extension options
- Smaller codebase, potentially easier to understand and contribute to
Cons of Blackfriday
- Less strict adherence to CommonMark specification
- May have fewer security guarantees compared to cmark-gfm
- Not officially supported by GitHub, potentially less aligned with GitHub Flavored Markdown
Code Comparison
cmark-gfm (C):
cmark_node *document = cmark_parse_document(buffer, len, CMARK_OPT_DEFAULT);
char *html = cmark_render_html(document, CMARK_OPT_DEFAULT, NULL);
cmark_node_free(document);
Blackfriday (Go):
markdown := blackfriday.Run(input)
html := blackfriday.Run(input, blackfriday.WithExtensions(blackfriday.CommonExtensions))
Both libraries offer straightforward ways to convert Markdown to HTML, but Blackfriday's API is more concise in this basic usage. cmark-gfm provides more granular control over the parsing and rendering process, which can be beneficial for complex use cases.
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
cmark-gfm
cmark-gfm is an extended version of the C reference implementation of
CommonMark, a rationalized version of Markdown syntax with a spec. This
repository adds GitHub Flavored Markdown extensions to
the upstream implementation, as defined in the spec.
The rest of the README is preserved as-is from the upstream source. Note that
the library and binaries produced by this fork are suffixed with -gfm in
order to distinguish them from the upstream.
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.json the client to preview content that will be rendered on the server usingcmark. -
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,
Tcl,
Scala and
Node.js.
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 but, because libFuzzer is still
under active development, may not work with your system-installed version of
clang. Assuming LLVM has been built in $HOME/src/llvm/build the fuzzer can be
run with:
CC="$HOME/src/llvm/build/bin/clang" LIB_FUZZER_PATH="$HOME/src/llvm/lib/Fuzzer/libFuzzer.a" make libFuzzer
To make a release tarball and zip archive:
make archive
Installing (Windows)
To compile with MSVC and NMAKE:
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.
Top Related Projects
CommonMark parsing and rendering library and program in C
CommonMark spec, with reference implementations in C and JavaScript
Markdown parser, done right. 100% CommonMark support, extensions, syntax plugins & high speed
A bidirectional Markdown to HTML to Markdown converter written in Javascript
A markdown parser and compiler. Built for speed.
Blackfriday: a markdown processor for Go
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