According to daringfireball.net –

Markdown is a text-to-HTML conversion tool for web writers. Markdown allows you to write using an easy-to-read, easy-to-write plain text format, then convert it to structurally valid XHTML (or HTML).

This is a very easy to use writing tool for the web. I tried a sample project which –

  1. Has a Markdown editor. User can write Markdown syntax there.
  2. Save the Mardown content and converted HTML content to the database.

Following libraries are used in the project –

1). Editormd as the Markdown editor
https://pandao.github.io/editor.md/en.html

2). Commonmark as the Markdown parser
https://github.com/atlassian/commonmark-java

This is the project repository – https://github.com/ronniegnr/poc-project/tree/master/spring-boot-markdown-editor-poc

This model is used –

package bd.com.ronnie.springbootmarkdowneditorpoc.entity;

...

@Entity
public class Post {
    private Long id;
    private String title;
    private String htmlContent;
    private String markdownContent;
    private Status status;
    private Instant createdAt;

    ...

    public enum Status {ACTIVE, DRAFT, INACTIVE}

}

These are the things we needed for the Editormd –

To run the project run this command in project directory –

gradle bootRun

Now the application should be accessible with this URL –

http://localhost:8080/

Use the Post Entry page to create a new post. All saved posts can be found in the Post List page. You can also access the H2 database from its web console with URL http://localhost:8080/h2-console/login.jsp. Just make sure jdbc:h2:mem:testdb is given as JDBC URL and use password as password.

On saving a new post, Markdown content is converted to HTML content using commonmark and saved to Database. This is the parsing part –

public static String markdownToHTML(String markdownContent) {
    Parser parser = Parser.builder().build();
    Node document = parser.parse(markdownContent);
    HtmlRenderer renderer = HtmlRenderer.builder().build();
    return renderer.render(document);
}

Previous Article

Leave a Reply

Your email address will not be published. Required fields are marked *