import 'dotenv/config'; import { getAllSlugs } from './data.js'; import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); async function generateSitemap() { const slugs = await getAllSlugs(); const siteUrl = process.env.PUBLIC_SITE_URL || 'https://yourdomain.com'; const urls = slugs.map(slugArr => { if (slugArr.length === 1 && slugArr[0] === '/') { return '/'; } return '/' + slugArr.join('/'); }); let uniqueUrls = Array.from(new Set(urls)); if (!uniqueUrls.includes('/')) { uniqueUrls.unshift('/'); } else { uniqueUrls = uniqueUrls.filter(url => url !== '/'); uniqueUrls.unshift('/'); } const sitemap = `\n\n${uniqueUrls.map(url => `\n \n ${siteUrl}${url}\n weekly\n 0.7\n `).join('')}\n`; const sitemapPath = path.resolve(__dirname, '../public/sitemap.xml'); fs.writeFileSync(sitemapPath, sitemap, 'utf8'); console.log('Sitemap generated!'); } generateSitemap();