Parse Markdown URLs and email addresses to HTML links in Java with CommonMark Autolink Extension

Tags: CommonMark Java CommonMark CommonMark Autolink CommonMark Extension commonmark-ext-autolink Markdown Markdown URL Markdown Email HTML Convert Parser Parse HtmlRenderer AutolinkExtension

In this Java tutorial we learn how to convert a markdown string into HTML string with automatically turning plain URLs and email addresses into HTML links using the CommonMark Java library and CommonMark Autolink Extensions library.

To use the Java CommonMark and CommonMark Autolink 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-autolink:0.17.2'

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


To have more information about the CommonMark Java library you can visit the project repository at

In the following Java example code we show you how to use the Parser, HtmlRenderer classes of the CommonMark library and AutolinkExtension class of the CommonMark Autolink Extension library to convert a given String in markdown syntax including URLs and email addresses into HTML links.

import org.commonmark.Extension;
import org.commonmark.ext.autolink.AutolinkExtension;
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 ParseMarkdownURLToHTML {
    public static void main(String... args) {
        String markdownValue = "the links is\n" +
                "my email:";

        String htmlValue = convertMarkdownToHTML(markdownValue);

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

    public static String convertMarkdownToHTML(String markdown) {
        List<Extension> extensions = Arrays.asList(AutolinkExtension.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:
the links is
my email:

HTML String:
<p>the links is <a href=""></a>
my email: <a href=""></a></p>

Happy Coding 😊