build.gradle
<aside>
implementation 'com.vladsch.flexmark:flexmark-all:0.64.8'
</aside>
CommonUtil
<aside>
package lh.h.config;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Node;
import org.springframework.stereotype.Component;
@Component
public class CommonUtil {
//Markdwon을 분석
private final Parser parser = Parser.builder().build();
//Markdown을 HTML로 변환
private final HtmlRenderer renderer = HtmlRenderer.builder().build();
public String markdown(String markdown) {
//Markdown을 AST로 변환
Node document = parser.parse(markdown);
//AST를 HTML로 변환
String html = renderer.render(document);
// 코드 블록을 <pre> 없이 <code>로 변환
html = html.replace("<pre><code>", "<code>").replace("</code></pre>", "</code>");
return html;
}
}
</aside>
html
<aside>
<div class="markdown-body" data-theme="light" th:utext="${@commonUtil.markdown(board.content)}">
</aside>
css
<aside>
https://github.com/sindresorhus/github-markdown-css
</aside>