/** * Elenco delle pagine del sito letto dalla sitemap generata in dist/ (nessuna lista da tenere * allineata a mano), più le pagine escluse dalla sitemap (404 e conferma del modulo). */ import { existsSync, readFileSync, readdirSync } from 'node:fs'; import { resolve } from 'node:path'; export function readSitemapPaths(distDir) { const index = resolve(distDir, 'sitemap-index.xml'); if (!existsSync(index)) { throw new Error(`Sitemap non trovata in ${index}: esegui prima \`npm run build\`.`); } const locs = (xml) => [...xml.matchAll(/([^<]+)<\/loc>/g)].map((m) => m[1].trim()); const paths = []; for (const sitemapUrl of locs(readFileSync(index, 'utf8'))) { const file = resolve(distDir, new URL(sitemapUrl).pathname.replace(/^\//, '')); for (const url of locs(readFileSync(file, 'utf8'))) paths.push(new URL(url).pathname); } return [...new Set(paths)].sort(); } /** Pagine generate ma escluse dalla sitemap per scelta (noindex). */ export const EXTRA_PAGES = ['/404.html', '/contatti/grazie/']; /** Tutti i file HTML presenti in dist/ (percorsi relativi). */ export function listHtmlFiles(distDir, dir = '') { const out = []; for (const entry of readdirSync(resolve(distDir, dir), { withFileTypes: true })) { const rel = dir ? `${dir}/${entry.name}` : entry.name; if (entry.isDirectory()) out.push(...listHtmlFiles(distDir, rel)); else if (entry.name.endsWith('.html')) out.push(rel); } return out.sort(); } /** Da file HTML a percorso URL: «studio/index.html» → «/studio/». */ export function fileToPath(rel) { if (rel === 'index.html') return '/'; if (rel.endsWith('/index.html')) return `/${rel.slice(0, -'index.html'.length)}`; return `/${rel}`; }