Java Convert Markdown to HTML using flexmark-java

Tags: flexmark-all flexmark-java Markdown to HTML

In this Java flexmark-java tutorial we learn how to use the flexmark-java library in Java application to convert Markdown string into HTML string.

Table of contents

  1. Add flexmark-java library to Java project
  2. How to convert Markdown String to HTML String in Java
  3. How to convert Markdown File into HTML File in Java
  4. Implement reusable MarkdownToHtmlConverter Java class

Add flexmark-java library to Java project

At this first step we add the flexmark-java dependency to the Java project.

To use the flexmark-java library in the Gradle build project, add the following dependency into the build.gradle file.

implementation  group: 'com.vladsch.flexmark', name: 'flexmark-all', version: '0.64.0'

To use the flexmark-java library in the Maven build project, add the following dependency into the pom.xml file.

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

How to convert Markdown String to HTML String in Java

In the following Java example program, we use flexmark-java library to convert a String in Markdown format into HTML string using the HtmlRenderer class.

MarkdownToHtmlExample1.java

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;

public class MarkdownToHtmlExample1 {
    public static void main(String... args) {
        String markdownContent = "# Heading h1\n"
                + "## Heading h2\n"
                + "### Heading h3\n"
                + "#### Heading h4\n"
                + "---";

        MutableDataSet options = new MutableDataSet();
        Parser parser = Parser.builder(options).build();
        HtmlRenderer renderer = HtmlRenderer.builder(options).build();
        Node document = parser.parse(markdownContent);
        String outputHtml = renderer.render(document);

        System.out.println(outputHtml);
    }
}
The output as below.
<h1>Heading h1</h1>
<h2>Heading h2</h2>
<h3>Heading h3</h3>
<h4>Heading h4</h4>
<hr />

How to convert Markdown File into HTML File in Java

At this step, we show you how to convert a Markdown text file into HTML file in Java using flexmark-java library.

For example, we have a Markdown file at D:\SimpleSolution\Data\sample-markdown.md as following screenshot.

Java Convert Markdown to HTML using flexmark-java

The content of sample-markdown.md file as below.

sample-markdown.md

# Simple Solution
Top website for Java, Spring Boot tutorials. 
You can check out our website at [SimpleSolution.dev](https://simplesolution.dev/ "Simple Solution Website")

## Our Contents

* Java tutorials
* Spring Boot tutorials 

## Simple Solution QR Code
 
[![Simple Solution website](https://simplesolution.dev/images/qrcode.png)](https://simplesolution.dev/images/qrcode.png "Simple Solution website.")

The following Java program to show you how to convert the sample-markdown.md file above to a HTML file named output.html in the same folder.

MarkdownToHtmlExample2.java

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.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

public class MarkdownToHtmlExample2 {
    public static void main(String... args) throws IOException {
        String inputMarkdownFilePath = "D:\\SimpleSolution\\Data\\sample-markdown.md";
        String outputHtmlFilePath = "D:\\SimpleSolution\\Data\\output.html";

        String markdownContent = new String(Files.readAllBytes(Paths.get(inputMarkdownFilePath)), StandardCharsets.UTF_8);

        MutableDataSet options = new MutableDataSet();
        Parser parser = Parser.builder(options).build();
        HtmlRenderer renderer = HtmlRenderer.builder(options).build();
        Node document = parser.parse(markdownContent);
        String outputHtml = renderer.render(document);

        Files.write(Paths.get(outputHtmlFilePath), outputHtml.getBytes());
    }
}

Execute the Java application above we have the output.html file be generated as below screenshot.

Java Convert Markdown to HTML using flexmark-java

Below is the content of output.html generated file.

output.html

<h1>Simple Solution</h1>
<p>Top website for Java, Spring Boot tutorials.
You can check out our website at <a href="https://simplesolution.dev/" title="Simple Solution Website">SimpleSolution.dev</a></p>
<h2>Our Contents</h2>
<ul>
<li>Java tutorials</li>
<li>Spring Boot tutorials</li>
</ul>
<h2>Simple Solution QR Code</h2>
<p><a href="https://simplesolution.dev/images/qrcode.png" title="Simple Solution website."><img src="https://simplesolution.dev/images/qrcode.png" alt="Simple Solution website" /></a></p>

Open the output.html file on browser we have the HTML view as below.

Java Convert Markdown to HTML using flexmark-java

Implement reusable MarkdownToHtmlConverter Java class

At this step we implement a new Java class named MarkdownToHtmlConverter which has methods to convert Markdown String or File into HTML to be re-use later in Java project.

Create new Java class named MarkdownToHtmlConverter as below.

MarkdownToHtmlConverter.java

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.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

public class MarkdownToHtmlConverter {

    public String convertMarkdownToHtml(String markdownContent) {
        MutableDataSet options = new MutableDataSet();
        Parser parser = Parser.builder(options).build();
        HtmlRenderer renderer = HtmlRenderer.builder(options).build();
        Node document = parser.parse(markdownContent);
        String outputHtml = renderer.render(document);

        return outputHtml;
    }

    public void convertMarkdownFileToHtmlFile(String inputMarkdownFilePath, String outputHtmlFilePath) {
        try {
            String markdownContent = new String(Files.readAllBytes(Paths.get(inputMarkdownFilePath)), StandardCharsets.UTF_8);
            String outputHtml = convertMarkdownToHtml(markdownContent);
            Files.write(Paths.get(outputHtmlFilePath), outputHtml.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

The following Java example program to show you how to use the MarkdownToHtmlConverter class to convert Markdown String to HTML String.

MarkdownToHtmlExample3.java

public class MarkdownToHtmlExample3 {
    public static void main(String... args) {
        String markdownContent = "# Simple Solution\n"
                + "[SimpleSolution.dev](https://simplesolution.dev/ \"Simple Solution Website\")\n";

        MarkdownToHtmlConverter converter = new MarkdownToHtmlConverter();
        String outputHtml = converter.convertMarkdownToHtml(markdownContent);

        System.out.println(outputHtml);
    }
}
The output as below.
<h1>Simple Solution</h1>
<p><a href="https://simplesolution.dev/" title="Simple Solution Website">SimpleSolution.dev</a></p>

The following Java example program to show you how to use the MarkdownToHtmlConverter class to convert Markdown File to HTML File.

MarkdownToHtmlExample4.java

public class MarkdownToHtmlExample4 {
    public static void main(String... args){
        String inputMarkdownFilePath = "D:\\SimpleSolution\\Data\\sample-markdown.md";
        String outputHtmlFilePath = "D:\\SimpleSolution\\Data\\output.html";

        MarkdownToHtmlConverter converter = new MarkdownToHtmlConverter();
        converter.convertMarkdownFileToHtmlFile(inputMarkdownFilePath, outputHtmlFilePath);
    }
}

Happy Coding 😊