[fixes #825] Add MarkdownUtil

This commit is contained in:
Sibo Van Gool 2022-01-25 14:34:43 +01:00
parent b8a82f49bc
commit 408c8847b3

View File

@ -0,0 +1,30 @@
package net.sf.openrocket.util;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
/**
* This class formats a Markdown text (e.g. from the GitHub API) to HTML
*
* @author Sibo Van Gool <sibo.vangool@hotmail.com>
*/
public class MarkdownUtil {
/**
* Convert input Markdown text to HTML.
* @param markdown text with Markdown styles.
* @return HTML rendering from the Markdown
*/
public static String toHtml(String markdown) {
if (markdown == null) return "";
// Convert JSON string new line to markdown newline
markdown = markdown.replace("\\r\\n", "\n");
Parser parser = Parser.builder().build();
Node document = parser.parse(markdown);
HtmlRenderer renderer = HtmlRenderer.builder().build();
return renderer.render(document);
}
}