Working with Markdown in Java: Library Overview and Usage Examples
Markdown is a lightweight markup language widely used for formatting text in documentation, README files, blogs, and content management systems. In Java, there are several mature libraries that allow you to parse Markdown, convert it to HTML, and perform reverse operations. In this article, we will take a detailed look at three of the most popular libraries: CommonMark, Flexmark, and Pegdown, and provide practical examples of their use.
1. CommonMark (org.commonmark:commonmark)
CommonMark is a strict Markdown standard that eliminates ambiguities in the original specification. The commonmark-java library is the reference implementation of this standard. It is lightweight, fast, and supports extensions (tables, strikethrough, autolinks).
Dependency (Maven):
<dependency> <groupId>org.commonmark</groupId> <artifactId>commonmark</artifactId> <version>0.21.0</version></dependency>Example of parsing Markdown to HTML:
import org.commonmark.node.*;import org.commonmark.parser.Parser;import org.commonmark.renderer.html.HtmlRenderer;
public class CommonMarkExample { public static void main(String[] args) { String markdown = "# Heading
This is **bold** text and *italic*.
- Item 1- Item 2";
Parser parser = Parser.builder().build(); Node document = parser.parse(markdown);
HtmlRenderer renderer = HtmlRenderer.builder().build(); String html = renderer.render(document);
System.out.println(html); }}Execution result:
<h1>Heading</h1><p>This is <strong>bold</strong> text and <em>italic</em>.</p><ul><li>Item 1</li><li>Item 2</li></ul>To enable extensions (e.g., tables), use:
import org.commonmark.ext.gfm.tables.TablesExtension;
Parser parser = Parser.builder() .extensions(List.of(TablesExtension.create())) .build();2. Flexmark (com.vladsch.flexmark)
Flexmark is a high-performance library for working with Markdown that supports many extensions: GFM tables, footnotes, attributes, typography, syntax highlighting, and much more. It is fully customizable and suitable for complex projects.
Dependency (Maven):
<dependency> <groupId>com.vladsch.flexmark</groupId> <artifactId>flexmark-all</artifactId> <version>0.64.8</version></dependency>Example of converting Markdown to HTML with tables:
import com.vladsch.flexmark.html.HtmlRenderer;import com.vladsch.flexmark.parser.Parser;import com.vladsch.flexmark.util.data.MutableDataSet;import com.vladsch.flexmark.ext.tables.TablesExtension;
import java.util.Arrays;
public class FlexmarkExample { public static void main(String[] args) { MutableDataSet options = new MutableDataSet(); options.set(Parser.EXTENSIONS, Arrays.asList(TablesExtension.create()));
Parser parser = Parser.builder(options).build(); HtmlRenderer renderer = HtmlRenderer.builder(options).build();
String markdown = "| Column 1 | Column 2 ||-----------|-----------|| Cell 1 | Cell 2 |";
String html = renderer.render(parser.parse(markdown)); System.out.println(html); }}Flexmark also allows you to extract metadata (YAML front matter), convert to LaTeX, and work with the AST tree.
<
Also in library
Prometheus Metrics in Kotlin: Integration with Micrometer and Ktor
HTML Parsing in PHP: A Complete Guide to Libraries and Tools
Working with ZIP Archives in Java: A Complete Guide to java.util.zip
Rate Limiting in Kotlin: Libraries and Practical Examples