UNPKG

pyseoa-ts

Version:

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

45 lines (44 loc) 1.46 kB
import { runAnalysis } from "./runAnalysis"; import { crawlWebsite } from "../crawler/crawlWebsite"; export async function runSeoAudit(url, options = {}, onProgress) { const pages = []; if (options.crawl) { // multi page mode const crawled = await crawlWebsite(url, options, onProgress); for (const [index, page] of crawled.entries()) { onProgress?.({ type: "analyze", url: page.url, pageIndex: index, stage: "analyze" }); pages.push({ url: page.url, result: runAnalysis(page.html), }); } } else { // single page mode const res = await fetch(url); const html = await res.text(); pages.push({ url, result: runAnalysis(html), }); } // Global score summary const total = pages.length; const all = pages.map(p => p.result.scoreSummary); const sum = all.reduce((acc, cur) => ({ score: acc.score + cur.totalScore, percent: acc.percent + cur.percentage }), { score: 0, percent: 0 }); const fields = Object.keys(all[0]?.breakdown || {}); const avgScore = +(sum.score / total).toFixed(2); const avgPercent = +(sum.percent / total).toFixed(2); return { pages, summary: { averageScore: avgScore, averagePercentage: avgPercent, maxPages: total, fields, } }; }