package com.ruoyi.project.utils;

import org.jsoup.Jsoup;
import org.jsoup.safety.Safelist;

public class HtmlUtils {
    
    /**
     * 移除所有HTML标签，只保留纯文本
     */
    public static String removeAllTags(String html) {
        if (html == null) {
            return null;
        }
        return Jsoup.clean(html, Safelist.none());
    }
    
    /**
     * 移除所有HTML标签，但保留换行格式
     * 将<br>、<p>等转换为换行符
     */
    public static String removeTagsWithLineBreaks(String html) {
        if (html == null) {
            return null;
        }
        // 先保留换行标签
        String withBreaks = Jsoup.clean(html, 
            Safelist.none()
                .addTags("br", "p", "div")
        );
        // 将HTML换行标签转换为系统换行符
        return withBreaks
            .replaceAll("<br\\s*/?>", "\n")
            .replaceAll("<p\\b[^>]*>", "\n")
            .replaceAll("<div\\b[^>]*>", "\n")
            .replaceAll("</p>", "")
            .replaceAll("</div>", "");
    }
}