Parse Markdown to HTML with YAML metadata in Java using CommonMark YAML Front Matter Extension
Tags: CommonMark Java CommonMark CommonMark YAML Front Matter CommonMark Extension commonmark-ext-yaml-front-matter Markdown Markdown YAML HTML Convert Parser Parse HtmlRenderer YamlFrontMatterExtension
In this Java tutorial we learn how to convert a markdown string which includes a YAML metadata header to HTML string using the CommonMark library and the CommonMark YAML Front Matter Extension library.
How to add CommonMark and CommonMark YAML Front Matter Extension libraries to the Java project
To use the Java CommonMark and CommonMark YAML Front Matter 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-yaml-front-matter:0.17.2'
To use the Java CommonMark and CommonMark YAML Front Matter 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-yaml-front-matter</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 string contains YAML metadata header 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 YamlFrontMatterExtension class of the CommonMark YAML Front Matter Extension library to convert a given String in markdown syntax including YAML metadata into a String in HTML format, which the output HTML does not include YAML metadata.
ParseMarkdownYAMLToHTML.java
import org.commonmark.Extension;
import org.commonmark.ext.front.matter.YamlFrontMatterExtension;
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 ParseMarkdownYAMLToHTML {
public static void main(String... args) {
String markdownValue = "---\n" +
"key: value\n" +
"list:\n" +
" - value 1\n" +
" - value 2\n" +
"literal: |\n" +
" this is literal value.\n" +
"\n" +
" literal values 2\n" +
"---\n" +
"\n" +
"# Java Tutorial";
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(YamlFrontMatterExtension.create());
Parser parser = Parser.builder().extensions(extensions).build();
Node document = parser.parse(markdown);
HtmlRenderer htmlRenderer = HtmlRenderer.builder().extensions(extensions).build();
return htmlRenderer.render(document);
}
}
Markdown String:
---
key: value
list:
- value 1
- value 2
literal: |
this is literal value.
literal values 2
---
# Java Tutorial
HTML String:
<h1>Java Tutorial</h1>
Happy Coding 😊