package com.xiaoniu.system.util;

import org.commonmark.Extension;
import org.commonmark.ext.gfm.strikethrough.StrikethroughExtension;
import org.commonmark.ext.gfm.tables.TablesExtension;
import org.commonmark.ext.heading.anchor.HeadingAnchorExtension;
import org.commonmark.ext.image.attributes.ImageAttributesExtension;
import org.commonmark.ext.task.list.items.TaskListItemsExtension;
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 org.springframework.stereotype.Component;

import java.util.*;

/**
 * Created by cjh on 2022/08/12.
 */
@Component
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<Extension> headingAnchorExtensions = Collections.singleton(HeadingAnchorExtension.create());
        //删除线
        List<Extension>  strikethroughExtension = Arrays.asList(StrikethroughExtension.create());
        //图片属性
        List<Extension>  imgAttrExtension = Arrays.asList(ImageAttributesExtension.create());
        //转换table的HTML
        List<Extension> tableExtension = Arrays.asList(TablesExtension.create());
        //任务列表
        List<Extension>  taskListExtension = Arrays.asList(TaskListItemsExtension.create());

        Parser parser = Parser.builder()
                .extensions(tableExtension)
                .extensions(strikethroughExtension)
                .extensions(imgAttrExtension)
                .extensions(taskListExtension)
                .build();
        Node document = parser.parse(markdown);
        HtmlRenderer renderer = HtmlRenderer.builder()
                .extensions(headingAnchorExtensions)
                .extensions(tableExtension)
                .extensions(strikethroughExtension)
                .extensions(imgAttrExtension)
                .extensions(taskListExtension)
                .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<String, String> 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   | 哈哈哈   |\n" +
                "| ----- | ---- | ----- |\n" +
                "| 斯维尔多  | 士大夫  | f啊    |\n" +
                "| 阿什顿发  | 非固定杆 | 撒阿什顿发 |\n" +
                "\n";
        String a = "![text](/url.png){width=640 height=480}";
        String a1 = "![text](/url.png){width=200 height=480}";
        String img = "![七龙珠](https://t7.baidu.com/it/u=1819248061,230866778&fm=193&f=GIF)";
        String str ="> aa\n" +
                "```yaml\n" +
                "啊啊啊啊啊\n" +
                "谢谢谢谢谢\n" +
                "\n" +
                "```";

        String str1 = "1\n" +
                "\n1";
       //System.out.println(markdownToHtmlExtensions(str1));

        String test = "![Description](https://192.168.199.21/appfile/manualImg/202208151034299883_403.png)\n" +
                "------------------------------------\n" +
                "**a**\n" +
                "*b*\n" +
                "*11111*\n" +
                "------------------------------------\n" +
                "- 1\n" +
                "- 2\n" +
                "- 3";
        System.out.println(markdownToHtmlExtensions(test));

    }

}

