pyseoa-ts
Version:
SEO analysis tools for the web, ported from pyseoa (Python) to TypeScript
29 lines (28 loc) • 708 B
JavaScript
import * as cheerio from 'cheerio';
export function analyzeH1(html) {
const $ = cheerio.load(html);
const h1Elements = $("h1");
const h1Texts = [];
h1Elements.each((_, el) => {
const text = $(el).text().trim();
if (text)
h1Texts.push(text);
});
if (h1Texts.length === 0)
return {
score: 0,
message: "Missing <H1> tag",
h1Tags: [],
};
if (h1Texts.length > 1)
return {
score: 1,
message: "Multiple <H1> tags found.",
h1Tags: h1Texts,
};
return {
score: 3,
message: "Exactly one <H1> tag found.",
h1Tags: h1Texts,
};
}