Markdownテキストをhtmlテキストに変換


1.pomにパッケージを追加
                
			com.atlassian.commonmark
			commonmark
			0.10.0
		

		
			com.atlassian.commonmark
			commonmark-ext-heading-anchor
			0.10.0
		
		
			com.atlassian.commonmark
			commonmark-ext-gfm-tables
			0.10.0
		

2.UtilクラスでMarkdownUtilを作成する.JAvaツールクラス:
package com.xpp2.blog2.Util;
import org.commonmark.Extension;
import org.commonmark.ext.gfm.tables.TableBlock;
import org.commonmark.ext.gfm.tables.TablesExtension;
import org.commonmark.ext.heading.anchor.HeadingAnchorExtension;
import org.commonmark.node.Link;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.AttributeProvider;
import org.commonmark.renderer.html.AttributeProviderContext;
import org.commonmark.renderer.html.AttributeProviderFactory;
import org.commonmark.renderer.html.HtmlRenderer;

import java.util.*;

public class MarkdownUtils {
    /**
     * markdown HTML 
     * @param markdown
     * @return
     */
    public static String markdownToHtml(String markdown) {
        Parser parser = Parser.builder().build();
        Node document = parser.parse(markdown);
        HtmlRenderer renderer = HtmlRenderer.builder().build();
        return renderer.render(document);
    }

    /**
     *  [ , ]
     * Markdown HTML
     * @param markdown
     * @return
     */
    public static String markdownToHtmlExtensions(String markdown) {
        //h id
        Set headingAnchorExtensions = Collections.singleton(HeadingAnchorExtension.create());
        // table HTML
        List tableExtension = Arrays.asList(TablesExtension.create());
        Parser parser = Parser.builder()
                .extensions(tableExtension)
                .build();
        Node document = parser.parse(markdown);
        HtmlRenderer renderer = HtmlRenderer.builder()
                .extensions(headingAnchorExtensions)
                .extensions(tableExtension)
                .attributeProviderFactory(new AttributeProviderFactory() {
                    public AttributeProvider create(AttributeProviderContext context) {
                        return new CustomAttributeProvider();
                    }
                })
                .build();
        return renderer.render(document);
    }

    /**
     *  
     */
    static class CustomAttributeProvider implements AttributeProvider {
        @Override
        public void setAttributes(Node node, String tagName, Map attributes) {
            // a target _blank
            if (node instanceof Link) {
                attributes.put("target", "_blank");
            }
            if (node instanceof TableBlock) {
                attributes.put("class", "ui celled table");
            }
        }
    }


    public static void main(String[] args) {
        String table = "| hello | hi   |     |
" + "| ----- | ---- | ----- |
" + "| | | f |
" + "| | | |
" + "
"; String a = "[imCoding ](http://www.xpp.cn)"; System.out.println(markdownToHtmlExtensions(a)); } }

3.BlogServiceに新しいメソッドを追加する:getAndConvert
BlogServiceImplでの実装:
 @Override
    public Blog getAndConvert(Long id) {
        Blog blog=blogRepository.findById(id).orElse(null);
        if(blog==null){
            throw new NotFoundException(" ");
        }
        Blog b=new Blog();
        BeanUtils.copyProperties(blog,b);
        String content = b.getContent();
        b.setContent( MarkdownUtils.markdownToHtmlExtensions(content));
        return b;
    }

4.Controllorレイヤで呼び出すには:
  @GetMapping("/blog/{id}")
    public String blog(@PathVariable Long id ,Model model)
    {
        model.addAttribute("blog",blogservice.getAndConvert(id));
        return "blog";
    }

5.htmlページでは、必ずエスケープしないutextを使用します.th:utext="${blog.content}">

。 。