Convertire Markdown in HTML in Java

Mattepuffo's logo
Convertire Markdown in HTML in Java

Convertire Markdown in HTML in Java

In Java abbiamo la libreria commonmark-java che ci permette di convertire testo Markdown in HTML.

Se usate Maven:

        <dependency>
            <groupId>org.commonmark</groupId>
            <artifactId>commonmark</artifactId>
            <version>0.18.2</version>
        </dependency>

Qui sotto un esempio di codice:

import org.commonmark.node.*;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;

public class Main {

    public static void main(String[] args) {
        String mdString= "# Heading unon"
                + "Heading due alternativon"
                + "---------------n"
                + "Testo in *grassetto*n"
                + "1. Lista unon"
                + "2. Lista duen";

        Parser parser = Parser.builder().build();
        Node document = parser.parse(mdString);

        HtmlRenderer renderer = HtmlRenderer.builder().build();
        System.out.println(renderer.render(document));
    }

}

Enjoy!


Condividi

Commentami!