Created
September 28, 2024 21:42
-
-
Save someguy9/df62cca23ad49b4fc61a0d38944601df to your computer and use it in GitHub Desktop.
Getting meta title, description, and og:image using cheerio and Node.js
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 * as cheerio from 'cheerio' | |
export async function getMetadata(url) { | |
// Ensure the URL starts with http:// or https:// | |
if (!url.startsWith('http://') && !url.startsWith('https://')) { | |
url = `https://${url}`; | |
} | |
try { | |
// Fetch the HTML content of the URL | |
const response = await fetch(url) | |
if (!response.ok) { | |
throw new Error(`HTTP error! status: ${response.status}`) | |
} | |
const html = await response.text() | |
// Load the HTML content into cheerio | |
const $ = cheerio.load(html) | |
// Extract metadata | |
const metadata = { | |
title: $('title').first().text().trim() || '', | |
description: $('meta[name="description"]').attr('content') || | |
$('meta[property="og:description"]').attr('content') || '', | |
image: $('meta[property="og:image"]').attr('content') || | |
$('meta[name="twitter:image"]').attr('content') || '' | |
} | |
return metadata | |
} catch (error) { | |
console.error('Error fetching metadata:', error) | |
return { | |
title: '', | |
description: '', | |
image: '' | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment