Files
jiang13-forum/frontend/src/utils/seoText.ts

19 lines
746 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/** 从 HTML 提取纯文本摘要(供页面 description / OG */
export function excerptFromHTML(html: string, max = 160): string {
if (!html) return '';
const doc = new DOMParser().parseFromString(html, 'text/html');
const text = (doc.body.textContent || '').replace(/\s+/g, ' ').trim();
if (text.length <= max) return text;
return `${text.slice(0, Math.max(0, max - 1))}`;
}
/** 正文中第一张图片 URL */
export function firstImageFromHTML(html: string): string {
if (!html) return '';
const doc = new DOMParser().parseFromString(html, 'text/html');
const img = doc.querySelector('img[src]');
const src = img?.getAttribute('src')?.trim() || '';
if (!src || src.startsWith('data:')) return '';
return src;
}