How to render HTML to Markdown?

Using Commonmark java how to render html to markdown? I tried this example, but it is not rendering.

@Slf4j
class MarkdownTest {
    @Test
    void markdownTest() {
        String markdown = toMarkdown("<b> Hello World!</b>");
        log.info("markdown ==> {}", markdown);
    }

    String toMarkdown(String html) {
        Node htmlDocument = Parser.builder().build().parse(html);
        return MarkdownRenderer.builder().build().render(htmlDocument);
    }
}

Output:

markdown ==> <b> Hello World!</b>

1 Like

commonmark-java doesn’t include an HTML parser. The only thing the parser accepts is Markdown.

To achieve what you want to do, you would first need to use an HTML parser (e.g. jsoup), then convert the parsed elements to a tree of Node objects in commonmark-java, then render it using the MarkdownRenderer.