55 lines
1.4 KiB
TypeScript
55 lines
1.4 KiB
TypeScript
'use server'
|
|
|
|
import * as cheerio from 'cheerio';
|
|
|
|
export interface LinkMetadata {
|
|
url: string;
|
|
title?: string;
|
|
description?: string;
|
|
imageUrl?: string;
|
|
siteName?: string;
|
|
}
|
|
|
|
export async function fetchLinkMetadata(url: string): Promise<LinkMetadata | null> {
|
|
try {
|
|
// Add protocol if missing
|
|
let targetUrl = url;
|
|
if (!url.startsWith('http://') && !url.startsWith('https://')) {
|
|
targetUrl = 'https://' + url;
|
|
}
|
|
|
|
const response = await fetch(targetUrl, {
|
|
headers: {
|
|
'User-Agent': 'Mozilla/5.0 (compatible; Memento/1.0; +http://localhost:3000)',
|
|
},
|
|
next: { revalidate: 3600 } // Cache for 1 hour
|
|
});
|
|
|
|
if (!response.ok) return null;
|
|
|
|
const html = await response.text();
|
|
const $ = cheerio.load(html);
|
|
|
|
const getMeta = (prop: string) =>
|
|
$(`meta[property="${prop}"]`).attr('content') ||
|
|
$(`meta[name="${prop}"]`).attr('content');
|
|
|
|
const title = getMeta('og:title') || $('title').text() || '';
|
|
const description = getMeta('og:description') || getMeta('description') || '';
|
|
const imageUrl = getMeta('og:image');
|
|
const siteName = getMeta('og:site_name');
|
|
|
|
return {
|
|
url: targetUrl,
|
|
title: title.substring(0, 100), // Truncate if too long
|
|
description: description.substring(0, 200),
|
|
imageUrl,
|
|
siteName
|
|
};
|
|
|
|
} catch (error) {
|
|
console.error('Error fetching link metadata:', error);
|
|
return null;
|
|
}
|
|
}
|