Java library for parsing and rendering Markdown text according to the CommonMark specification
npx @tessl/cli install tessl/maven-org-commonmark--commonmark@0.17.0CommonMark Java is a comprehensive Java implementation of the CommonMark specification for parsing and rendering Markdown text. It provides a complete parsing system that converts Markdown input into an abstract syntax tree (AST) of nodes, supports visiting and manipulating these nodes, and offers HTML and text rendering capabilities. The library is designed for high performance, maintains a small footprint with zero dependencies in the core module, and offers extensive flexibility through AST manipulation and customizable rendering.
<dependency>
<groupId>org.commonmark</groupId>
<artifactId>commonmark</artifactId>
<version>0.17.0</version>
</dependency>import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import org.commonmark.renderer.text.TextContentRenderer;
import org.commonmark.node.Node;import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import org.commonmark.node.Node;
// Parse Markdown text to AST
Parser parser = Parser.builder().build();
Node document = parser.parse("This is *Markdown*");
// Render to HTML
HtmlRenderer renderer = HtmlRenderer.builder().build();
String html = renderer.render(document);
// Result: <p>This is <em>Markdown</em></p>
// Parse from Reader (for files)
try (FileReader reader = new FileReader("document.md")) {
Node document = parser.parseReader(reader);
String html = renderer.render(document);
}CommonMark Java is built around several key components:
Parser class converts Markdown text to AST nodes using configurable block and inline parsersComplete Markdown parsing functionality that converts text input into a structured Abstract Syntax Tree (AST). Supports all CommonMark specification features with configurable parsing options.
public static Parser.Builder builder();
public Node parse(String input);
public Node parseReader(Reader input) throws IOException;Comprehensive node hierarchy representing all CommonMark elements including blocks, inlines, and document structure. Provides visitor pattern support for traversing and manipulating the AST.
public abstract class Node {
public abstract void accept(Visitor visitor);
public Node getNext();
public Node getParent();
public void appendChild(Node child);
}
public interface Visitor {
void visit(Document document);
void visit(Heading heading);
void visit(Paragraph paragraph);
// ... visit methods for all node types
}High-performance HTML rendering with extensive customization options including URL sanitization, attribute providers, and custom node renderers.
public static HtmlRenderer.Builder builder();
public String render(Node node);
public void render(Node node, Appendable output);Plain text rendering for converting CommonMark documents back to text format with optional newline stripping and custom formatting.
public static TextContentRenderer.Builder builder();
public String render(Node node);
public void render(Node node, Appendable output);Comprehensive extension framework for adding custom syntax support, parsers, and renderers. Supports block parser factories, delimiter processors, and post-processors.
public interface Extension {
// Marker interface for extensions
}
public interface Parser.ParserExtension extends Extension {
void extend(Parser.Builder parserBuilder);
}
public interface HtmlRenderer.HtmlRendererExtension extends Extension {
void extend(HtmlRenderer.Builder rendererBuilder);
}// Main entry points
public class Parser {
public static Builder builder();
public Node parse(String input);
public Node parseReader(Reader input) throws IOException;
}
public class HtmlRenderer implements Renderer {
public static Builder builder();
public String render(Node node);
public void render(Node node, Appendable output);
}
public class TextContentRenderer implements Renderer {
public static Builder builder();
public String render(Node node);
public void render(Node node, Appendable output);
}
// Base node hierarchy
public abstract class Node {
public abstract void accept(Visitor visitor);
public Node getNext();
public Node getPrevious();
public Node getFirstChild();
public Node getLastChild();
public Node getParent();
public void appendChild(Node child);
public void prependChild(Node child);
public void unlink();
public void insertAfter(Node sibling);
public void insertBefore(Node sibling);
}
public abstract class Block extends Node {
public Block getParent();
}
// Extension interfaces
public interface Extension {
// Marker interface
}
public interface Renderer {
void render(Node node, Appendable output);
String render(Node node);
}