Parse Markdown to HTML with Auto Generate IDs for Headings in Java with CommonMark Heading Anchor Extension

Tags: CommonMark Java CommonMark CommonMark Heading Anchor CommonMark Extension commonmark-ext-heading-anchor Markdown Markdown heading HTML Convert Parser Parse HtmlRenderer HeadingAnchorExtension

In this Java tutorial we learn how to convert Markdown string into HTML string with ability to auto generate the ID for headings HTML tabs by using the CommonMark Java library and the CommonMark Heading Anchor Extension library.

How to add CommonMark and Heading Anchor Extension libraries to the Java project

To use the Java CommonMark and CommonMark Heading Anchor Extension libraries in the Gradle build project, add the following dependency into the build.gradle file.

implementation 'org.commonmark:commonmark:0.17.2'
implementation 'org.commonmark:commonmark-ext-heading-anchor:0.17.2'

To use the Java CommonMark and CommonMark Heading Anchor Extension libraries in the Maven build project, add the following dependency into the pom.xml file.

<dependency>
    <groupId>org.commonmark</groupId>
    <artifactId>commonmark</artifactId>
    <version>0.17.2</version>
</dependency>
<dependency>
    <groupId>org.commonmark</groupId>
    <artifactId>commonmark-ext-heading-anchor</artifactId>
    <version>0.17.2</version>
</dependency>

To have more information about the CommonMark Java library you can visit the project repository at github.com/commonmark/commonmark-java

How to convert markdown to HTML with auto generated IDs for headings tags

In the following Java example code we show you how to use the Parser, HtmlRenderer classes of the CommonMark library and HeadingAnchorExtension class of the CommonMark Heading Anchor Extension library to convert a given markdown String to HTML String with auto generated IDs for heading tags.

ParseMarkdownHeadingAnchorToHTML.java

import org.commonmark.Extension;
import org.commonmark.ext.heading.anchor.HeadingAnchorExtension;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;

import java.util.Arrays;
import java.util.List;

public class ParseMarkdownHeadingAnchorToHTML {
    public static void main(String... args) {
        String markdownValue = "# Heading h1\n" +
                "## Heading h2\n" +
                "### Heading h3";

        String htmlValue = convertMarkdownToHTML(markdownValue);

        System.out.println("Markdown String:");
        System.out.println(markdownValue);
        System.out.println();
        System.out.println("HTML String:");
        System.out.println(htmlValue);
    }

    public static String convertMarkdownToHTML(String markdown) {
        List<Extension> extensions = Arrays.asList(HeadingAnchorExtension.create());
        Parser parser = Parser.builder().extensions(extensions).build();
        Node document = parser.parse(markdown);
        HtmlRenderer htmlRenderer = HtmlRenderer.builder().extensions(extensions).build();
        return htmlRenderer.render(document);
    }
}
The output is:
Markdown String:
# Heading h1
## Heading h2
### Heading h3

HTML String:
<h1 id="heading-h1">Heading h1</h1>
<h2 id="heading-h2">Heading h2</h2>
<h3 id="heading-h3">Heading h3</h3>

Happy Coding 😊