Parse Markdown Tables to HTML in Java with CommonMark GFM Tables Extension

Tags: CommonMark Java CommonMark CommonMark GFM Tables CommonMark Extension commonmark-ext-gfm-tables Markdown GFM Tables GitHub Flavored Markdown HTML Convert Parser Parse HtmlRenderer TablesExtension

In this Java tutorial we learn how to parse markdown string with GFM tables using “|” pipes (GitHub Flavored Markdown) to HTML string using the CommonMark and CommonMark GFM Tables Extension libraries.

How to add CommonMark and CommonMark GFM Tables Extension libraries to the Java project

To use the Java CommonMark and CommonMark GFM Tables 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-gfm-tables:0.17.2'

To use the Java CommonMark and CommonMark GFM Tables 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-gfm-tables</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 parse markdown GFM tables to HTML in Java

In the following Java example code we show you how to use the Parser, HtmlRenderer classes of the CommonMark library and TablesExtension class of the CommonMark GFM Tables Extension library to convert a given String in markdown syntax including GitHub Flavored Markdown tables into a String in HTML format.

ParseMarkdownTableToHTML.java

import org.commonmark.Extension;
import org.commonmark.ext.gfm.tables.TablesExtension;
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 ParseMarkdownTableToHTML {

    public static void main(String... args) {
        String markdownValue = "# Sample Table\n" +
                "| First Header  | Second Header |\n" +
                "| ------------- | ------------- |\n" +
                "| Content Cell  | Content Cell  |\n" +
                "| Content Cell  | Content Cell  |";

        String htmlValue = convertMarkdownToHTML(markdownValue);

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

    public static String convertMarkdownToHTML(String markdown) {
        List<Extension> extensions = Arrays.asList(TablesExtension.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:
# Sample Table
| First Header  | Second Header |
| ------------- | ------------- |
| Content Cell  | Content Cell  |
| Content Cell  | Content Cell  |
HTML String:
<h1>Sample Table</h1>
<table>
<thead>
<tr>
<th>First Header</th>
<th>Second Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>Content Cell</td>
<td>Content Cell</td>
</tr>
<tr>
<td>Content Cell</td>
<td>Content Cell</td>
</tr>
</tbody>
</table>

Happy Coding 😊