or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

ast-nodes.mdcore-parsing.mdextensions.mdhtml-rendering.mdindex.mdtext-rendering.md
tile.json

tessl/maven-org-commonmark--commonmark

Java library for parsing and rendering Markdown text according to the CommonMark specification

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.commonmark/commonmark@0.17.x

To install, run

npx @tessl/cli install tessl/maven-org-commonmark--commonmark@0.17.0

index.mddocs/

CommonMark Java

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

Package Information

  • Package Name: org.commonmark:commonmark
  • Package Type: Maven
  • Language: Java
  • Installation:
    <dependency>
      <groupId>org.commonmark</groupId>
      <artifactId>commonmark</artifactId>
      <version>0.17.0</version>
    </dependency>

Core Imports

import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import org.commonmark.renderer.text.TextContentRenderer;
import org.commonmark.node.Node;

Basic Usage

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);
}

Architecture

CommonMark Java is built around several key components:

  • Parser System: Main Parser class converts Markdown text to AST nodes using configurable block and inline parsers
  • AST Node Hierarchy: Complete set of node classes representing all CommonMark elements with visitor pattern support
  • Rendering Engine: Pluggable renderers for HTML, text, and custom output formats with extensible node renderer system
  • Extension Framework: Interface-based extension system for adding custom syntax, parsers, and renderers
  • Source Tracking: Optional source span tracking for mapping AST nodes back to original input positions

Capabilities

Core Parsing

Complete 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;

Core Parsing

AST Node System

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
}

AST Node System

HTML Rendering

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);

HTML Rendering

Text Content Rendering

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);

Text Content Rendering

Extension System

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);
}

Extension System

Core Types

// 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);
}