Skip to content
Permalink
Browse files

🎨 Fix #841

  • Loading branch information...
88250 committed Jan 3, 2019
1 parent ab65b88 commit 11396c5a664413cf84f83ce2cb5690aada9f203c
Showing with 24 additions and 19 deletions.
  1. +24 −19 src/main/java/org/b3log/symphony/util/Markdowns.java
@@ -64,7 +64,7 @@
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @author <a href="http://zephyr.b3log.org">Zephyr</a>
* @author <a href="http://vanessa.b3log.org">Vanessa</a>
* @version 1.11.21.9, Jan 2, 2019
* @version 1.11.21.10, Jan 3, 2019
* @since 0.2.0
*/
public final class Markdowns {
@@ -268,29 +268,28 @@ public static String toHTML(final String markdownText) {

String html = langPropsService.get("contentRenderFailedLabel");

if (MARKED_AVAILABLE) {
try {
html = toHtmlByMarked(markdownText);
if (!StringUtils.startsWith(html, "<p>")) {
html = "<p>" + html + "</p>";
}
} catch (final Exception e) {
LOGGER.log(Level.WARN, "Failed to use [marked] for markdown [md=" + StringUtils.substring(markdownText, 0, 256) + "]: " + e.getMessage());
final int count = StringUtils.countMatches(markdownText, "\n");
if (3 >= count) {
// 优化 Markdown 渲染 https://github.com/b3log/symphony/issues/841
html = toHtmlByFlexmark(markdownText);
} else {
if (MARKED_AVAILABLE) {
try {
html = toHtmlByMarked(markdownText);
} catch (final Exception e) {
LOGGER.log(Level.WARN, "Failed to use [marked] for markdown [md=" + StringUtils.substring(markdownText, 0, 256) + "]: " + e.getMessage());

com.vladsch.flexmark.util.ast.Node document = PARSER.parse(markdownText);
html = RENDERER.render(document);
if (!StringUtils.startsWith(html, "<p>")) {
html = "<p>" + html + "</p>";
html = toHtmlByFlexmark(markdownText);
}
}
} else {
com.vladsch.flexmark.util.ast.Node document = PARSER.parse(markdownText);
html = RENDERER.render(document);
if (!StringUtils.startsWith(html, "<p>")) {
html = "<p>" + html + "</p>";
} else {
html = toHtmlByFlexmark(markdownText);
}
}

if (!StringUtils.startsWith(html, "<p>")) {
html = "<p>" + html + "</p>";
}

final Whitelist whitelist = Whitelist.relaxed();
inputWhitelist(whitelist);
html = Jsoup.clean(html, whitelist);
@@ -422,6 +421,12 @@ private static String toHtmlByMarked(final String markdownText) throws Exception
return ret;
}

private static String toHtmlByFlexmark(final String markdownText) {
com.vladsch.flexmark.util.ast.Node document = PARSER.parse(markdownText);

return RENDERER.render(document);
}

/**
* Gets HTML for the specified markdown text.
*

0 comments on commit 11396c5

Please sign in to comment.
You can’t perform that action at this time.