All formats

flexmark-java

LanguageJava (11+ for 0.64)
LicenseBSD-2-Clause
Repositorygithub.com/vsch/flexmark-java
Documentationwiki, usage examples
Depends onflexmark-util; extensions bring their own (e.g. openhtmltopdf for PDF, docx4j for DOCX)
Read / writeparses CommonMark plus extensions; renders HTML, Markdown, DOCX, PDF; converts HTML to Markdown

flexmark-java began as a fork of commonmark-java and added detailed source tracking, a large set of extensions (tables, task lists, footnotes, front matter, TOC, admonitions, abbreviations, attributes, emoji, wiki links, definition lists and more) and “parser emulation” to mimic other Markdown processors (Pegdown, Kramdown, MultiMarkdown, GitHub). The flexmark-html2md-converter module turns HTML back into Markdown, and flexmark-docx-converter and flexmark-pdf-converter produce Word and PDF files. JetBrains’ Markdown plugin was built on it.

Options are passed through a MutableDataSet. The last release, 0.64.8, is from 2023; the project still receives occasional commits. Use the flexmark-all artifact to get everything, or individual modules to keep dependencies small.

Installation#

Maven:

XML
<dependency>
  <groupId>com.vladsch.flexmark</groupId>
  <artifactId>flexmark-all</artifactId>
  <version>0.64.8</version>
</dependency>

Gradle: implementation("com.vladsch.flexmark:flexmark-all:0.64.8")

Basic usage: convert Markdown to HTML with GFM extensions#

Java
import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughExtension;
import com.vladsch.flexmark.ext.gfm.tasklist.TaskListExtension;
import com.vladsch.flexmark.ext.tables.TablesExtension;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.data.MutableDataSet;
import java.util.List;

public class MdToHtml {
    public static void main(String[] args) {
        MutableDataSet options = new MutableDataSet();
        options.set(Parser.EXTENSIONS, List.of(
            TablesExtension.create(),
            StrikethroughExtension.create(),
            TaskListExtension.create()));

        Parser parser = Parser.builder(options).build();
        HtmlRenderer renderer = HtmlRenderer.builder(options).build();

        Node document = parser.parse("| a | b |\n|---|---|\n| 1 | 2 |\n\n- [x] ~~done~~\n");
        System.out.println(renderer.render(document));
    }
}
Java
import com.vladsch.flexmark.ast.Heading;
import com.vladsch.flexmark.ast.Link;
import com.vladsch.flexmark.util.ast.NodeVisitor;
import com.vladsch.flexmark.util.ast.VisitHandler;

NodeVisitor visitor = new NodeVisitor(
    new VisitHandler<>(Heading.class, h -> System.out.println("h" + h.getLevel() + ": " + h.getText())),
    new VisitHandler<>(Link.class, l -> System.out.println("link: " + l.getUrl()))
);
visitor.visit(document);

NodeVisitor visits children automatically when a handler doesn’t stop it.

How to convert HTML to Markdown#

Java
import com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter;

String md = FlexmarkHtmlConverter.builder().build()
    .convert("<h1>Title</h1><p>Some <b>bold</b> text and <a href=\"https://example.com\">a link</a>.</p>");
// # Title
//
// Some **bold** text and [a link](https://example.com).

How to emulate GitHub’s dialect#

Java
import com.vladsch.flexmark.parser.ParserEmulationProfile;

MutableDataSet options = new MutableDataSet();
options.setFrom(ParserEmulationProfile.GITHUB_DOC);

How to escape raw HTML#

Java
options.set(HtmlRenderer.ESCAPE_HTML, true);

Tips#