package com.bonus.canteen.core.common.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import lombok.Generated;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.function.Function;

public class RichTextUtil {
   @Generated
   private static final Logger log = LoggerFactory.getLogger(RichTextUtil.class);

   public static String getCutFileUrl(String richText) {
      return parseXml(richText);
   }

   private static String parseXml(String richText) {
      // 1. 解析 HTML
      Document doc = Jsoup.parse(richText);

      // 2. 遍历所有 <img> 标签并修改 src
      for (Element img : doc.select("img")) {
         String originalSrc = img.attr("src");
         img.attr("src", FileUrlUtil.getFileUrl(originalSrc));
      }

      // 3. 获取处理后的 HTML
      String processedText = doc.body().html();

      return processedText;
   }

}
