feat(image): 实现 Markdown 图片文件名同步
- 新增 ImageName 实体类和对应的 Mapper- 在 MarkdownFileService 中添加图片文件名同步方法 - 优化 HomePage 组件,支持实时预览 Markdown 内容 - 新增 MarkdownImageExtractor 工具类,用于提取 Markdown 中的图片文件名
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package com.test.bijihoudaun.util;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Markdown图片提取工具类
|
||||
* 用于从Markdown文本中提取图片文件名
|
||||
*/
|
||||
public class MarkdownImageExtractor {
|
||||
|
||||
/**
|
||||
* 从Markdown内容中提取图片文件名
|
||||
* 支持各种URL格式:
|
||||
* - 绝对URL: http://example.com/path/uuid.png
|
||||
* - 相对URL: /path/uuid.png
|
||||
* - 协议相对URL: //example.com/path/uuid.png
|
||||
*
|
||||
* @param markdownContent 包含图片的Markdown文本
|
||||
* @return 图片文件名列表
|
||||
*/
|
||||
public static List<String> extractImageFilenames(String markdownContent) {
|
||||
if (markdownContent == null || markdownContent.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
// 使用正则表达式匹配Markdown图片语法中的文件名
|
||||
// 模式:  其中url以UUID格式的文件名结尾
|
||||
Pattern pattern = Pattern.compile("!\\[.*?\\]\\([^)]*?([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}\\.[a-zA-Z0-9]+)\\)");
|
||||
Matcher matcher = pattern.matcher(markdownContent);
|
||||
|
||||
List<String> filenames = new ArrayList<>();
|
||||
while (matcher.find()) {
|
||||
filenames.add(matcher.group(1));
|
||||
}
|
||||
|
||||
return filenames;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查Markdown内容中是否包含指定的图片文件名
|
||||
*
|
||||
* @param markdownContent Markdown文本
|
||||
* @param filename 要查找的文件名
|
||||
* @return 是否包含该文件名
|
||||
*/
|
||||
public static boolean containsImage(String markdownContent, String filename) {
|
||||
if (markdownContent == null || filename == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return extractImageFilenames(markdownContent).contains(filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Markdown内容中提取图片URL
|
||||
*
|
||||
* @param markdownContent 包含图片的Markdown文本
|
||||
* @return 图片URL列表
|
||||
*/
|
||||
public static List<String> extractImageUrls(String markdownContent) {
|
||||
if (markdownContent == null || markdownContent.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
// 匹配Markdown图片语法中的完整URL
|
||||
Pattern pattern = Pattern.compile("!\\[.*?\\]\\(([^)]+)\\)");
|
||||
Matcher matcher = pattern.matcher(markdownContent);
|
||||
|
||||
List<String> urls = new ArrayList<>();
|
||||
while (matcher.find()) {
|
||||
urls.add(matcher.group(1));
|
||||
}
|
||||
|
||||
return urls;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user