From 50219005def9175f0bd635d9853cdbf6a2fc2c60 Mon Sep 17 00:00:00 2001 From: biezhi Date: Sun, 31 Dec 2017 17:24:02 +0800 Subject: [PATCH] :sparkles: resolved (#333) --- src/main/java/com/tale/init/TaleConst.java | 9 +++++- src/main/java/com/tale/utils/TaleUtils.java | 31 ++++++++++++++++----- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/tale/init/TaleConst.java b/src/main/java/com/tale/init/TaleConst.java index 6717084e..7362cfdc 100644 --- a/src/main/java/com/tale/init/TaleConst.java +++ b/src/main/java/com/tale/init/TaleConst.java @@ -86,5 +86,12 @@ public class TaleConst { */ public static final String PLUGINS_MENU_NAME = "plugin_menus"; - + public static final String ENV_SUPPORT_163_MUSIC = "app.support_163_music"; + public static final String ENV_SUPPORT_GIST = "app.support_gist"; + public static final String MP3_PREFIX = "[mp3:"; + public static final String MUSIC_IFRAME = ""; + public static final String MUSIC_REG_PATTERN = "\\[mp3:(\\d+)\\]"; + public static final String GIST_PREFIX_URL = "https://gist.github.com/"; + public static final String GIST_REG_PATTERN = "<script src=\"https://gist.github.com/(\\w+)/(\\w+)\\.js\"></script>"; + public static final String GIST_REPLATE_PATTERN = ""; } \ No newline at end of file diff --git a/src/main/java/com/tale/utils/TaleUtils.java b/src/main/java/com/tale/utils/TaleUtils.java index 694a7d3a..23677cb4 100644 --- a/src/main/java/com/tale/utils/TaleUtils.java +++ b/src/main/java/com/tale/utils/TaleUtils.java @@ -19,8 +19,12 @@ import com.tale.model.entity.Users; import org.commonmark.Extension; import org.commonmark.ext.gfm.tables.TablesExtension; +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 javax.imageio.ImageIO; @@ -34,6 +38,8 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; +import static com.tale.init.TaleConst.*; + /** * Tale工具类 *

@@ -156,22 +162,33 @@ public static String mdToHtml(String markdown) { List 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); + HtmlRenderer renderer = HtmlRenderer.builder() + .attributeProviderFactory(context -> new LinkAttributeProvider()) + .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+)\\]", ""); + if (TaleConst.BCONF.getBoolean(ENV_SUPPORT_163_MUSIC, true) && content.contains(MP3_PREFIX)) { + content = content.replaceAll(MUSIC_REG_PATTERN, MUSIC_IFRAME); } // 支持gist代码输出 - if (TaleConst.BCONF.getBoolean("app.support_gist", true) && content.contains("https://gist.github.com/")) { - content = content.replaceAll("<script src=\"https://gist.github.com/(\\w+)/(\\w+)\\.js\"></script>", ""); + if (TaleConst.BCONF.getBoolean(ENV_SUPPORT_GIST, true) && content.contains(GIST_PREFIX_URL)) { + content = content.replaceAll(GIST_REG_PATTERN, GIST_REPLATE_PATTERN); } - return content; } + static class LinkAttributeProvider implements AttributeProvider { + @Override + public void setAttributes(Node node, String tagName, Map attributes) { + if (node instanceof Link) { + attributes.put("target", "_blank"); + } + } + } + /** * 提取html中的文字 *