pyseoa-ts
Version:
SEO analysis tools for the web, ported from pyseoa (Python) to TypeScript
33 lines (32 loc) • 937 B
JavaScript
import * as cheerio from 'cheerio';
const REQUIRED_TAGS = ["og:title", "og:description", "og:image", "og:url"];
export function analyzeOpenGraph(html) {
const $ = cheerio.load(html);
const tags = {};
for (const tag of REQUIRED_TAGS) {
const content = $(`meta[property="${tag}"]`).attr("content");
if (content && content.trim()) {
tags[tag] = content.trim();
}
}
const foundCount = Object.keys(tags).length;
let score = 0;
let message = "No Open Graph tags found.";
if (foundCount >= 1 && foundCount < 3) {
score = 1;
message = "Only some Open Graph tags found.";
}
else if (foundCount === 3) {
score = 2;
message = "Most Open Graph tags found.";
}
else if (foundCount === 4) {
score = 3;
message = "All key Open Graph tags found.";
}
return {
score,
message,
tags
};
}