mirror of https://github.com/halo-dev/halo
Refactor MarkdownUtils.
parent
98caa57e47
commit
0050213f49
|
@ -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'
|
||||
|
|
|
@ -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<Extension> EXTENSIONS_YAML = Collections.singleton(YamlFrontMatterExtension.create());
|
||||
|
||||
/**
|
||||
* Table extension
|
||||
* commonmark-java extension for autolinking
|
||||
*/
|
||||
private static final Set<Extension> EXTENSIONS_TABLE = Collections.singleton(TablesExtension.create());
|
||||
private static final Set<Extension> EXTENSIONS_AUTO_LINK = Collections.singleton(AutolinkExtension.create());
|
||||
|
||||
/**
|
||||
* commonmark-java extension for strikethrough
|
||||
*/
|
||||
private static final Set<Extension> EXTENSIONS_STRIKETHROUGH = Collections.singleton(StrikethroughExtension.create());
|
||||
|
||||
/**
|
||||
* commonmark-java extension for tables
|
||||
*/
|
||||
private static final Set<Extension> EXTENSIONS_TABLES = Collections.singleton(TablesExtension.create());
|
||||
|
||||
/**
|
||||
* commonmark-java extension for adding id attributes to h tags
|
||||
*/
|
||||
private static final Set<Extension> EXTENSIONS_HEADING_ANCHOR = Collections.singleton(HeadingAnchorExtension.create());
|
||||
|
||||
/**
|
||||
* commonmark-java extension for <ins> (underline)
|
||||
*/
|
||||
private static final Set<Extension> EXTENSIONS_INS = Collections.singleton(InsExtension.create());
|
||||
|
||||
/**
|
||||
* commonmark-java extension for YAML front matter
|
||||
*/
|
||||
private static final Set<Extension> 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 <a href="https://github.com/otale/tale/blob/master/src/main/java/com/tale/utils/TaleUtils.java">TaleUtils.java</a>
|
||||
*/
|
||||
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 "";
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue