/**
* 从 HTML 内容中提取第一张图片的 URL
*/
export function useImageExtractor() {
/**
* 从 HTML 字符串中提取第一张图片的 src
* @param html HTML 字符串
* @returns 图片 URL 或 null
*/
function extractFirstImage(html: string): string | null {
if (!html || typeof html !== 'string') return null
// 创建一个临时 DOM 元素来解析 HTML
const tempDiv = document.createElement('div')
tempDiv.innerHTML = html
// 查找第一个 img 标签
const firstImg = tempDiv.querySelector('img')
if (firstImg && firstImg.src) {
return firstImg.src
}
// 如果没有找到 img 标签,尝试使用正则表达式匹配
const imgRegex = /
]+src=["']([^"']+)["']/i
const match = html.match(imgRegex)
if (match && match[1]) {
return match[1]
}
return null
}
/**
* 从 HTML 字符串中提取所有图片的 src
* @param html HTML 字符串
* @returns 图片 URL 数组
*/
function extractAllImages(html: string): string[] {
if (!html || typeof html !== 'string') return []
const tempDiv = document.createElement('div')
tempDiv.innerHTML = html
const images = tempDiv.querySelectorAll('img')
const urls: string[] = []
images.forEach((img) => {
if (img.src) {
urls.push(img.src)
}
})
return urls
}
return {
extractFirstImage,
extractAllImages,
}
}