Skip to content

Instantly share code, notes, and snippets.

@IgorHalfeld
Created November 21, 2024 04:42
Show Gist options
  • Save IgorHalfeld/ed81c6ac7903a57c73e55febab814951 to your computer and use it in GitHub Desktop.
Save IgorHalfeld/ed81c6ac7903a57c73e55febab814951 to your computer and use it in GitHub Desktop.
Get title, description and og image from a URL
import { ofetch } from 'ofetch';
import * as cheerio from 'cheerio';
interface ExtractMetaFromUrl {
url: string;
}
export const extractMetaFromUrl = async ({ url }: ExtractMetaFromUrl) => {
try {
const proxy = 'https://corsproxy.io';
const html = await ofetch(`${proxy}?${url}`);
const $ = cheerio.load(html);
const title = $('title').text();
const description = $('meta[name="description"]').attr('content');
const ogImage = $('meta[property="og:image"]').attr('content');
return {
url,
title,
description,
ogImage,
};
} catch (_) {
return { url };
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment