import * as path from 'path'; import { fetchHtml } from './fetch-html'; export interface WebMetaInfo { title?: string; description?: string; keyword?: string; favicon?: string; image?: string; url: string; } export const fetchWebMetaInfo = async ( webUrl: string, ): Promise => { try { const $ = await fetchHtml(webUrl); if (!$) return; const title = $('title').text(); let description = $( 'meta[name*="description"], meta[property*="og:description"]', ).attr('content'); // https://ogp.me/ // og 对 seo 的影响:https://cloud.tencent.com/developer/article/1617791 const image = $( 'meta[property*="og:image"], meta[property*="og:image:url"], meta[property*="og:image:secure_url"]', ).attr('content'); const keyword = $('meta[name*="keywords"]').attr('content'); const url = new URL(webUrl); let favicon = $('link[rel*="icon"]').attr('href'); if (favicon) { if (!/^((https?:\/\/)|(data:))/.test(favicon)) { if (favicon.startsWith('/')) { favicon = path.join(url.origin, favicon); } if (favicon.startsWith('.')) { favicon = path.join(url.origin, url.pathname, '../', favicon); } } } else { favicon = url.origin + '/favicon.ico'; } if (!description) { description = $('body').text().slice(0, 100); } return { url: webUrl, title, description, keyword, favicon, image, }; } catch (error) { console.log(error); return; } };