Play with Markdown in Java flexmark-java

When it comes to Markdown processor, I feel that Perl (the head family) / Python / Ruby / JavaScript has relatively well-known libraries, but when it comes to Java, I haven't heard that much (or rather standard). I don't have anything).

Current status (2017-03-02) Looking through the Java Markdown processor found in open-sourced, it is still under active development and seems to be satisfactory in terms of functionality and speed [flexmark-java]( I touched https://github.com/vsch/flexmark-java) a little, so it's a light introduction.

Java Markdown Library Overview

flexmark-java facts

Markdown melancholy and flexmark-java extension

As mentioned above, Markdown is famous for having many dialects, but it's almost the same, and I'd like to cover it with a single implementation if possible. So flexmark-java covers many dialects by making it extended (plugin type?).

// https://github.com/vsch/flexmark-java/wiki/Modified Usage example

import com.vladsch.flexmark.ast.Node;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.parser.ParserEmulationProfile;
import com.vladsch.flexmark.util.options.MutableDataHolder;
import com.vladsch.flexmark.util.options.MutableDataSet;

import com.vladsch.flexmark.ext.abbreviation.AbbreviationExtension;
import com.vladsch.flexmark.ext.definition.DefinitionExtension;
import com.vladsch.flexmark.ext.footnotes.FootnoteExtension;
import com.vladsch.flexmark.ext.tables.TablesExtension;
import com.vladsch.flexmark.ext.typographic.TypographicExtension;

public class RenderingExamples {

    //Set options for each Markdown dialect
    private MutableHolder getOptions(String type) {
        MutableDataHolder options = new MutableDataSet();
        switch (type) {
            case "markdown":
                options.setFrom(ParserEmulationProfile.MARKDOWN);
                break;
            case "multimarkdown":
                options.setFrom(ParserEmulationProfile.MULTI_MARKDOWN);
                break;
            case "kramdown":
                options.setFrom(ParserEmulationProfile.KRAMDOWN);
                options.set(Parser.EXTENSIONS, Arrays.asList(
                    AbbreviationExtension.create(),
                    DefinitionExtension.create(),
                    FootnoteExtension.create(),
                    TablesExtension.create(),
                    TypographicExtension.create()
                    ));
                break;
         }
         return options;
    }

    //Generate Parser for each dialect
    private Parser getParser(String type) {
        switch (type) {
            case "commonmark":
                return Parser.builder().build();
            default:
                return Parser.builder(getOptions(type)).build();
        }
    }

    //Generate Renderer for each dialect
    private HtmlRenderer getHtmlRenderer(String type) {
        switch (type) {
            case "commonmark":
                return HtmlRenderer.builder().build();
            default:
                return HtmlRenderer.builder(getOptions(type)).build();
        }
    }

    //Render HTML!
    public String render(String markdownText, String type) {
        Node document = getParser(type).parse(markdownText);
        return getHtmlRenderer(type).render(document);
    }
}

I want to mess with Markdown myself

It seems that there is also a way for the older brothers to play with the parsed Node by themselves.

// https://github.com/vsch/flexmark-java/wiki/Modified Usage example

import com.vladsch.flexmark.ast.Node;
import com.vladsch.flexmark.ast.NodeVisitor;
import com.vladsch.flexmark.ast.Text;
import com.vladsch.flexmark.ast.VisitHandler;

public class NodeVisitingExamples {
    public NodeVisitingExamples() {
        this.visitor = new NodeVisitor(
            new VisitHandler<>(Text.class, NodeVisitingExamples.this::visit)
        );
    }

    public void traverse(String markdownText) {
        Parser parser = Parser.builder().build();
        this.visitor.visit(parser.parse(markdownText));
    }

    private void visit(Text text) {
        System.out.println(text);
    }

    private final NodeVisitor visitor;
}

I want to write an extension myself

(Addition if you feel like this)

I used my pegdown!

Explanation of migration method: https://github.com/vsch/flexmark-java/wiki/Pegdown-Migration

More details

Official: https://github.com/vsch/flexmark-java/wiki/Usage

Sample code

https://gist.github.com/dmikurube/630b82f437977501a415155e3df3d68c

Recommended Posts

Play with Markdown in Java flexmark-java
Java to play with Function
Morphological analysis in Java with Kuromoji
Convert Markdown to HTML with flexmark-java
Concurrency Method in Java with basic example
Play Framework 2.6 (Java) environment construction in Eclipse
Play RAW, WAV, MP3 files in Java
Read xlsx file in Java with Selenium
Split a string with ". (Dot)" in Java
Working with huge JSON in Java Lambda
Partization in Java
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
Read a string in a PDF file with Java
Create a CSR with extended information in Java
Refactored GUI tools made with Java8 + JavaFX in 2016
Static code analysis with Checkstyle in Java + Gradle
Text extraction in Java from PDF with pdfbox-2.0.8
Practice working with Unicode surrogate pairs in Java
[JAVA] [Spring] [MyBatis] Use IN () with SQL Builder
Encrypt / decrypt with AES256 in PHP and Java
Handle JSON in cross domain with Play Framework
Programming with direct sum types in Java (Neta)
Get along with Java containers in Cloud Run
[java] sort in list
Install java with Homebrew
Read JSON in Java
Interpreter implementation in Java
Play Framework2.5 (Java) Tips
How to call functions in bulk with Java reflection
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
Change seats with java
Comments in Java source
Include image in jar file with java static method
Comfortable download with JAVA
Format XML in Java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Play with Java function nodes that can use Java with Node-RED
Hello World in Java
Switch java with direnv
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
I dealt with Azure Functions not working in Java
Ping commands in Java
Various threads in java
[Java] Get the file path in the folder with List
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
Output true with if (a == 1 && a == 2 && a == 3) in Java (Invisible Identifier)