Convert Markdown to HTML with flexmark-java

Overview

--Convert Markdown documents to HTML documents using the Java Markdown library flexmark-java

Sample program

File list

├── build.gradle
└── src
    └── main
        ├── java
        │   └── com
        │       └── example
        │           └── App.java
        └── resources
            └── markdown.md

App.java

package com.example;

import com.vladsch.flexmark.ext.anchorlink.AnchorLinkExtension;
import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughExtension;
import com.vladsch.flexmark.ext.tables.TablesExtension;
import com.vladsch.flexmark.ext.toc.TocExtension;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.data.MutableDataSet;

import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

public class App {

  public static void main(String[] args) throws IOException {

    //Set options to support different Markdown notations
    MutableDataSet options = new MutableDataSet();
    options.set(Parser.EXTENSIONS,
      Arrays.asList(
        AnchorLinkExtension.create(), //Anchor headlines
        StrikethroughExtension.create(), //Corresponds to strikethrough
        TablesExtension.create(), //Compatible with tables
        TocExtension.create() // [TOC]Generate a table of contents in the part of
      ));

    //Markdown parser and HTML renderer available
    Parser parser = Parser.builder(options).build();
    HtmlRenderer renderer = HtmlRenderer.builder(options).build();

    //Read Markdown file
    URL url = App.class.getClassLoader().getResource("markdown.md");
    List<String> lines = Files.readAllLines(Paths.get(url.getPath()), StandardCharsets.UTF_8);
    String markdown = String.join("\n", lines);

    //Convert Markdown to HTML and output
    Node document = parser.parse(markdown);
    String html = renderer.render(document);
    System.out.println(html);
  }
}

build.gradle

Describe the settings for introducing flexmark-java.

build.gradle


plugins {
  id 'java'
  id 'application'
}

group 'com.example'
version '0.0.1'

sourceCompatibility = 1.8

repositories {
  mavenCentral()
}

dependencies {
  // flexmark-java 0.50.Introduce 42
  implementation 'com.vladsch.flexmark:flexmark-all:0.50.42'
}

application {
    mainClassName = 'com.example.App'
}

Markdown file

Markdown document to use as the conversion source.

[TOC levels=1-2]

# Markdown

## Paragraphs

Hello, world.

Goodbye, world.

## Emphasis

*Hello, world.*

**Goodbye, world.**

~~Hello, Goodbye.~~

## Bullet List

- Hello
- World
- Goodbye

## Numbered list

1. Hello
2. World
3. Goodbye

## Table

| Left align | Right align | Center align |
|:-----------|------------:|:------------:|
| A          | B           | C            |
| Hello      | world       | Goodbye      |

## URL

http://example.com/

[example.com](http://example.com/)

<a href="http://example.com/">example.com</a>

## Image

![Image](image.png "image")

## Quote

> Hello, world.
> Goorbye, world.

## Source Code

```java
public class A {
  public static void main(String[] args) {
    System.out.println("Hello, world");
  }
}
``` 

Converted output HTML document

<ul>
<li><a href="#markdown">Markdown</a>
<ul>
<li><a href="#paragraphs">Paragraphs</a></li>
<li><a href="#emphasis">Emphasis</a></li>
<li><a href="#bullet-list">Bullet List</a></li>
<li><a href="#numbered-list">Numbered list</a></li>
<li><a href="#table">Table</a></li>
<li><a href="#url">URL</a></li>
<li><a href="#image">Image</a></li>
<li><a href="#quote">Quote</a></li>
<li><a href="#source-code">Source Code</a></li>
</ul>
</li>
</ul>
<h1 id="markdown"><a href="#markdown" id="markdown">Markdown</a></h1>
<h2 id="paragraphs"><a href="#paragraphs" id="paragraphs">Paragraphs</a></h2>
<p>Hello, world.</p>
<p>Goodbye, world.</p>
<h2 id="emphasis"><a href="#emphasis" id="emphasis">Emphasis</a></h2>
<p><em>Hello, world.</em></p>
<p><strong>Goodbye, world.</strong></p>
<p><del>Hello, Goodbye.</del></p>
<h2 id="bullet-list"><a href="#bullet-list" id="bullet-list">Bullet List</a></h2>
<ul>
<li>Hello</li>
<li>World</li>
<li>Goodbye</li>
</ul>
<h2 id="numbered-list"><a href="#numbered-list" id="numbered-list">Numbered list</a></h2>
<ol>
<li>Hello</li>
<li>World</li>
<li>Goodbye</li>
</ol>
<h2 id="table"><a href="#table" id="table">Table</a></h2>
<table>
<thead>
<tr><th align="left">Left align</th><th align="right">Right align</th><th align="center">Center align</th></tr>
</thead>
<tbody>
<tr><td align="left">A</td><td align="right">B</td><td align="center">C</td></tr>
<tr><td align="left">Hello</td><td align="right">world</td><td align="center">Goodbye</td></tr>
</tbody>
</table>
<h2 id="url"><a href="#url" id="url">URL</a></h2>
<p>http://example.com/</p>
<p><a href="http://example.com/">example.com</a></p>
<p><a href="http://example.com/">example.com</a></p>
<h2 id="image"><a href="#image" id="image">Image</a></h2>
<p><img src="image.png " alt="Image" title="image" /></p>
<h2 id="quote"><a href="#quote" id="quote">Quote</a></h2>
<blockquote>
<p>Hello, world.
Goorbye, world.</p>
</blockquote>
<h2 id="source-code"><a href="#source-code" id="source-code">Source Code</a></h2>
<pre><code class="language-java">public class A {
  public static void main(String[] args) {
    System.out.println(&quot;Hello, world&quot;);
  }
}
</code></pre>

This operation check environment

Reference material

Recommended Posts

Convert Markdown to HTML with flexmark-java
Play with Markdown in Java flexmark-java
Convert line feed code to html line feed tag with Thymeleaf and output
Convert C language to JavaScript with Emscripten
Convert JSON to TSV and TSV to JSON with Ruby
Convert a string to a character-by-character array with swift
How to embed JavaScript variables in HTML with Thymeleaf
Convert large XLSX files to CSV with Apache POI
Convert 2D array to csv format with Java 8 Stream API
Convert Java Powerpoint to XPS
Java to play with Function
I want to convert characters ...
Convert to Ruby Leet string
Convert Serializable Object to byte []
How to number (number) with html.erb
How to update with activerecord-import
Connect to DB with Java
Connect to MySQL 8 with Java
Convert from ○ months to ○ years ○ months
How to convert Java radix
[Java] Convert ArrayList to array
Connect to oracle with eclipse!
HTML parsing with JAVA (scraping)
I want to convert an array to Active Record Relation with Rails
How to convert param to hash with Rails controller (updated from time to time)
Edit SVG with Java + Apache Batik to convert to PNG or JPEG