Skip to content

Commit

Permalink
✨ resolved (otale#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Dec 31, 2017
1 parent f6f0cc8 commit 5021900
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
9 changes: 8 additions & 1 deletion src/main/java/com/tale/init/TaleConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<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>";
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 = "&lt;script src=\"https://gist.github.com/(\\w+)/(\\w+)\\.js\">&lt;/script>";
public static final String GIST_REPLATE_PATTERN = "<script src=\"https://gist.github.com/$1/$2\\.js\"></script>";
}
31 changes: 24 additions & 7 deletions src/main/java/com/tale/utils/TaleUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,6 +38,8 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static com.tale.init.TaleConst.*;

/**
* Tale工具类
* <p>
Expand Down Expand Up @@ -156,22 +162,33 @@ public static String mdToHtml(String markdown) {
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);
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+)\\]", "<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>");
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("&lt;script src=\"https://gist.github.com/(\\w+)/(\\w+)\\.js\">&lt;/script>", "<script src=\"https://gist.github.com/$1/$2\\.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<String, String> attributes) {
if (node instanceof Link) {
attributes.put("target", "_blank");
}
}
}

/**
* 提取html中的文字
*
Expand Down

0 comments on commit 5021900

Please sign in to comment.