Parse Markdown to HTML with Task List Items in Java using CommonMark Task List Items Extension
Tags: CommonMark Java CommonMark CommonMark Task List Items CommonMark Extension commonmark-ext-task-list-items Markdown Markdown Task List Items HTML Convert Parser Parse HtmlRenderer TaskListItemsExtension
In this Java tutorial we learn how to convert a markdown string with task list items into HTML string using the CommonMark library and the CommonMark Task List Items Extension library.
How to add CommonMark and CommonMark Task List Items libraries to the Java project
To use the Java CommonMark and CommonMark Task List Items 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-task-list-items:0.17.2'
To use the Java CommonMark and CommonMark Task List Items 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-task-list-items</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 task list items in Java
In the following Java example code we show you how to use the Parser, HtmlRenderer classes of the CommonMark library and TaskListItemsExtension class of the CommonMark Task List Items Extension library to convert a given String in markdown syntax including task list item into a String in HTML format.
ParseMarkdownTaskListItemToHTML.java
import org.commonmark.Extension;
import org.commonmark.ext.task.list.items.TaskListItemsExtension;
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 ParseMarkdownTaskListItemToHTML {
public static void main(String... args) {
String markdownValue = "- [ ] task #1\n" +
"- [x] task #2\n" +
"- [ ] task #3\n";
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(TaskListItemsExtension.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:
- [ ] task #1
- [x] task #2
- [ ] task #3
HTML String:
<ul>
<li><input type="checkbox" disabled=""> task #1</li>
<li><input type="checkbox" disabled="" checked=""> task #2</li>
<li><input type="checkbox" disabled=""> task #3</li>
</ul>
Happy Coding 😊