UNPKG

pyseoa-ts

Version:

SEO analysis tools for the web, ported from pyseoa (Python) to TypeScript

33 lines (32 loc) 963 B
import * as cheerio from 'cheerio'; const REQUIRED_TAGS = ["twitter:card", "twitter:title", "twitter:description", "twitter:image"]; export function analyzeTwitterCard(html) { const $ = cheerio.load(html); const tags = {}; for (const tag of REQUIRED_TAGS) { const content = $(`meta[name="${tag}"]`).attr("content"); if (content && content.trim()) { tags[tag] = content.trim(); } } const foundCount = Object.keys(tags).length; let score = 0; let message = "No Twitter Card tags found"; if (foundCount >= 1 && foundCount < 3) { score = 1; message = "Only some Twitter Card tags found."; } else if (foundCount === 3) { score = 2; message = "Most Twitter Card tags found."; } else if (foundCount === 4) { score = 3; message = "All key Twitter Card tags found."; } return { score, message, tags }; }