Search in sources :

Example 1 with Extension

use of org.commonmark.Extension in project FastHub by k0shk0sh.

the class MarkDownProvider method render.

protected static void render(@NonNull TextView textView, String markdown, int width) {
    List<Extension> extensions = Arrays.asList(StrikethroughExtension.create(), AutolinkExtension.create(), TablesExtension.create(), InsExtension.create(), EmojiExtension.create(), MentionExtension.create(), YamlFrontMatterExtension.create());
    Parser parser = Parser.builder().extensions(extensions).build();
    try {
        Node node = parser.parse(markdown);
        String rendered = HtmlRenderer.builder().extensions(extensions).build().render(node);
        HtmlHelper.htmlIntoTextView(textView, rendered, (width - (textView.getPaddingStart() + textView.getPaddingEnd())));
    } catch (Exception ignored) {
        HtmlHelper.htmlIntoTextView(textView, markdown, (width - (textView.getPaddingStart() + textView.getPaddingEnd())));
    }
}
Also used : MentionExtension(com.fastaccess.provider.markdown.extension.mention.MentionExtension) InsExtension(org.commonmark.ext.ins.InsExtension) StrikethroughExtension(org.commonmark.ext.gfm.strikethrough.StrikethroughExtension) EmojiExtension(com.fastaccess.provider.markdown.extension.emoji.EmojiExtension) Extension(org.commonmark.Extension) AutolinkExtension(org.commonmark.ext.autolink.AutolinkExtension) YamlFrontMatterExtension(org.commonmark.ext.front.matter.YamlFrontMatterExtension) TablesExtension(org.commonmark.ext.gfm.tables.TablesExtension) Node(org.commonmark.node.Node) Parser(org.commonmark.parser.Parser)

Example 2 with Extension

use of org.commonmark.Extension in project tale by otale.

the class TaleUtils method mdToHtml.

/**
     * markdown转换为html
     *
     * @param markdown
     * @return
     */
public static String mdToHtml(String markdown) {
    if (StringKit.isBlank(markdown)) {
        return "";
    }
    List<Extension> extensions = Arrays.asList(TablesExtension.create());
    Parser parser = Parser.builder().extensions(extensions).build();
    Node document = parser.parse(markdown);
    HtmlRenderer renderer = HtmlRenderer.builder().extensions(extensions).build();
    String content = renderer.render(document);
    content = Commons.emoji(content);
    // 支持网易云音乐输出
    if (TaleConst.BCONF.getBoolean("app.support_163_music", true) && content.contains("[mp3:")) {
        content = content.replaceAll("\\[mp3:(\\d+)\\]", "<iframe frameborder=\"no\" border=\"0\" marginwidth=\"0\" marginheight=\"0\" width=350 height=106 src=\"//music.163.com/outchain/player?type=2&id=$1&auto=0&height=88\"></iframe>");
    }
    // 支持gist代码输出
    if (TaleConst.BCONF.getBoolean("app.support_gist", true) && content.contains("https://gist.github.com/")) {
        content = content.replaceAll("&lt;script src=\"https://gist.github.com/(\\w+)/(\\w+)\\.js\">&lt;/script>", "<script src=\"https://gist.github.com/$1/$2\\.js\"></script>");
    }
    return content;
}
Also used : Extension(org.commonmark.Extension) TablesExtension(org.commonmark.ext.gfm.tables.TablesExtension) Node(org.commonmark.node.Node) HtmlRenderer(org.commonmark.renderer.html.HtmlRenderer) Parser(org.commonmark.parser.Parser)

Aggregations

Extension (org.commonmark.Extension)2 TablesExtension (org.commonmark.ext.gfm.tables.TablesExtension)2 Node (org.commonmark.node.Node)2 Parser (org.commonmark.parser.Parser)2 EmojiExtension (com.fastaccess.provider.markdown.extension.emoji.EmojiExtension)1 MentionExtension (com.fastaccess.provider.markdown.extension.mention.MentionExtension)1 AutolinkExtension (org.commonmark.ext.autolink.AutolinkExtension)1 YamlFrontMatterExtension (org.commonmark.ext.front.matter.YamlFrontMatterExtension)1 StrikethroughExtension (org.commonmark.ext.gfm.strikethrough.StrikethroughExtension)1 InsExtension (org.commonmark.ext.ins.InsExtension)1 HtmlRenderer (org.commonmark.renderer.html.HtmlRenderer)1