Lius
7 天以前 0a19d4923b0a048aee0cda91c37f25bc6e140d54
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package org.jeecg.common.util;
 
import org.apache.commons.lang3.StringUtils;
import org.pegdown.PegDownProcessor;
import org.springframework.web.util.HtmlUtils;
 
/**
 * HTML 工具类
 * @author: jeecg-boot
 * @date: 2022/3/30 14:43
 */
@SuppressWarnings("AlibabaClassNamingShouldBeCamel")
public class HTMLUtils {
 
    /**
     * 获取HTML内的文本,不包含标签
     *
     * @param html HTML 代码
     */
    public static String getInnerText(String html) {
        if (StringUtils.isNotBlank(html)) {
            //去掉 html 的标签
            String content = html.replaceAll("</?[^>]+>", "");
            // 将多个空格合并成一个空格
            content = content.replaceAll("(&nbsp;)+", "&nbsp;");
            // 反向转义字符
            content = HtmlUtils.htmlUnescape(content);
            return content.trim();
        }
        return "";
    }
 
    /**
     * 将Markdown解析成Html
     * @param markdownContent
     * @return
     */
    public static String parseMarkdown(String markdownContent) {
        PegDownProcessor pdp = new PegDownProcessor();
        return pdp.markdownToHtml(markdownContent);
    }
 
}