Created
November 21, 2024 04:42
-
-
Save IgorHalfeld/ed81c6ac7903a57c73e55febab814951 to your computer and use it in GitHub Desktop.
Get title, description and og image from a URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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