From 0050213f49f2d7cff1f3437e38657be9996d4bae Mon Sep 17 00:00:00 2001 From: ruibaby Date: Wed, 29 May 2019 15:21:12 +0800 Subject: [PATCH] Refactor MarkdownUtils. --- build.gradle | 4 ++ .../run/halo/app/utils/MarkdownUtils.java | 71 ++++++++++++++++--- 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/build.gradle b/build.gradle index 782e8cf2d..bc2b510e4 100644 --- a/build.gradle +++ b/build.gradle @@ -56,6 +56,10 @@ dependencies { implementation 'com.atlassian.commonmark:commonmark:0.12.1' implementation 'com.atlassian.commonmark:commonmark-ext-gfm-tables:0.12.1' implementation 'com.atlassian.commonmark:commonmark-ext-yaml-front-matter:0.12.1' + implementation 'com.atlassian.commonmark:commonmark-ext-autolink:0.12.1' + implementation 'com.atlassian.commonmark:commonmark-ext-gfm-strikethrough:0.12.1' + implementation 'com.atlassian.commonmark:commonmark-ext-heading-anchor:0.12.1' + implementation 'com.atlassian.commonmark:commonmark-ext-ins:0.12.1' implementation 'io.springfox:springfox-swagger2:2.9.2' implementation 'io.springfox:springfox-swagger-ui:2.9.2' implementation 'org.apache.commons:commons-lang3:3.8.1' diff --git a/src/main/java/run/halo/app/utils/MarkdownUtils.java b/src/main/java/run/halo/app/utils/MarkdownUtils.java index acd60e4f3..7f3e9c7e6 100644 --- a/src/main/java/run/halo/app/utils/MarkdownUtils.java +++ b/src/main/java/run/halo/app/utils/MarkdownUtils.java @@ -2,9 +2,13 @@ package run.halo.app.utils; import org.apache.commons.lang3.StringUtils; import org.commonmark.Extension; +import org.commonmark.ext.autolink.AutolinkExtension; import org.commonmark.ext.front.matter.YamlFrontMatterExtension; import org.commonmark.ext.front.matter.YamlFrontMatterVisitor; +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.ins.InsExtension; import org.commonmark.node.Node; import org.commonmark.parser.Parser; import org.commonmark.renderer.html.HtmlRenderer; @@ -26,30 +30,73 @@ import java.util.Set; */ public class MarkdownUtils { - /** - * Front-matter extension - */ - private static final Set EXTENSIONS_YAML = Collections.singleton(YamlFrontMatterExtension.create()); /** - * Table extension + * commonmark-java extension for autolinking */ - private static final Set EXTENSIONS_TABLE = Collections.singleton(TablesExtension.create()); + private static final Set EXTENSIONS_AUTO_LINK = Collections.singleton(AutolinkExtension.create()); + + /** + * commonmark-java extension for strikethrough + */ + private static final Set EXTENSIONS_STRIKETHROUGH = Collections.singleton(StrikethroughExtension.create()); + + /** + * commonmark-java extension for tables + */ + private static final Set EXTENSIONS_TABLES = Collections.singleton(TablesExtension.create()); + + /** + * commonmark-java extension for adding id attributes to h tags + */ + private static final Set EXTENSIONS_HEADING_ANCHOR = Collections.singleton(HeadingAnchorExtension.create()); + + /** + * commonmark-java extension for <ins> (underline) + */ + private static final Set EXTENSIONS_INS = Collections.singleton(InsExtension.create()); + + /** + * commonmark-java extension for YAML front matter + */ + private static final Set EXTENSIONS_YAML_FRONT_MATTER = Collections.singleton(YamlFrontMatterExtension.create()); + /** * Parse Markdown content */ - private static final Parser PARSER = Parser.builder().extensions(EXTENSIONS_YAML).extensions(EXTENSIONS_TABLE).build(); + private static final Parser PARSER = Parser.builder() + .extensions(EXTENSIONS_AUTO_LINK) + .extensions(EXTENSIONS_STRIKETHROUGH) + .extensions(EXTENSIONS_TABLES) + .extensions(EXTENSIONS_HEADING_ANCHOR) + .extensions(EXTENSIONS_INS) + .extensions(EXTENSIONS_YAML_FRONT_MATTER) + .build(); /** * Render HTML content */ - private static final HtmlRenderer RENDERER = HtmlRenderer.builder().extensions(EXTENSIONS_YAML).extensions(EXTENSIONS_TABLE).build(); + private static final HtmlRenderer RENDERER = HtmlRenderer.builder() + .extensions(EXTENSIONS_AUTO_LINK) + .extensions(EXTENSIONS_STRIKETHROUGH) + .extensions(EXTENSIONS_TABLES) + .extensions(EXTENSIONS_HEADING_ANCHOR) + .extensions(EXTENSIONS_INS) + .extensions(EXTENSIONS_YAML_FRONT_MATTER) + .build(); /** * Render text content */ - private static final TextContentRenderer TEXT_CONTENT_RENDERER = TextContentRenderer.builder().extensions(EXTENSIONS_YAML).extensions(EXTENSIONS_TABLE).build(); + private static final TextContentRenderer TEXT_CONTENT_RENDERER = TextContentRenderer.builder() + .extensions(EXTENSIONS_AUTO_LINK) + .extensions(EXTENSIONS_STRIKETHROUGH) + .extensions(EXTENSIONS_TABLES) + .extensions(EXTENSIONS_HEADING_ANCHOR) + .extensions(EXTENSIONS_INS) + .extensions(EXTENSIONS_YAML_FRONT_MATTER) + .build(); /** * Render Markdown content @@ -59,20 +106,25 @@ public class MarkdownUtils { * @see TaleUtils.java */ public static String renderMarkdown(String content) { + final Node document = PARSER.parse(content); String renderContent = RENDERER.render(document); + // render netease music short url if (content.contains(HaloConst.NETEASE_MUSIC_PREFIX)) { renderContent = content.replaceAll(HaloConst.NETEASE_MUSIC_REG_PATTERN, HaloConst.NETEASE_MUSIC_IFRAME); } + // render bilibili video short url if (content.contains(HaloConst.BILIBILI_VIDEO_PREFIX)) { renderContent = content.replaceAll(HaloConst.BILIBILI_VIDEO_REG_PATTERN, HaloConst.BILIBILI_VIDEO_IFRAME); } + // render youtube video short url if (content.contains(HaloConst.YOUTUBE_VIDEO_PREFIX)) { renderContent = content.replaceAll(HaloConst.YOUTUBE_VIDEO_REG_PATTERN, HaloConst.YOUTUBE_VIDEO_IFRAME); } + return renderContent; } @@ -84,6 +136,7 @@ public class MarkdownUtils { */ @NonNull public static String renderText(@Nullable String markdownContent) { + if (StringUtils.isBlank(markdownContent)) { return ""; }