Parse Markdown to HTML with Image Attributes in Java using CommonMark Image Attributes Extension

Tags: CommonMark Java CommonMark CommonMark Image Attributes CommonMark Extension commonmark-ext-image-attributes Markdown Markdown Image Attributes HTML Convert Parser Parse HtmlRenderer ImageAttributesExtension

In this Java tutorial we learn how to convert a given markdown string including images attributes into HTML string using the CommonMark library and the CommonMark Image Attributes Extension library.

How to add CommonMark and CommonMark Image Attributes Extension libraries to the Java project

To use the Java CommonMark and CommonMark Image Attributes 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-image-attributes:0.17.2'

To use the Java CommonMark and CommonMark Image Attributes 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-image-attributes</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 image attributes in Java

In the following Java example code we show you how to use the Parser, HtmlRenderer classes of the CommonMark library and ImageAttributesExtension class of the CommonMark Image Attributes Extension library to convert a given String in markdown syntax into a String in HTML format which generated img tag including attributes.

ParseMarkdownImageAttributesToHTML.java

import org.commonmark.Extension;
import org.commonmark.ext.image.attributes.ImageAttributesExtension;
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 ParseMarkdownImageAttributesToHTML {
    public static void main(String... args) {
        String markdownValue = "![Simple Solution](/https://simplesolution.dev/images/Logo_S_v1.png){width=120 height=120}";

        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(ImageAttributesExtension.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:
![Simple Solution](/https://simplesolution.dev/images/Logo_S_v1.png){width=120 height=120}

HTML String:
<p><img src="/https://simplesolution.dev/images/Logo_S_v1.png" alt="Simple Solution" width="120" height="120" /></p>

Happy Coding 😊