const express = require('express'); const router = express.Router(); const contentService = require('../services/content'); router.get('/sitemap.xml', (req, res) => { const siteUrl = res.locals.site.url; const posts = contentService.getAllPosts(); const pages = Array.from(contentService.pagesBySlug.values()); let xml = '\n'; xml += '\n'; // Static / Top-level pages const staticRoutes = ['/', '/blogs', '/search']; for (const route of staticRoutes) { xml += ' \n'; xml += ` ${siteUrl}${route}\n`; xml += ` daily\n`; xml += ` ${route === '/' ? '1.0' : '0.8'}\n`; xml += ' \n'; } // Standalone pages for (const page of pages) { if (!page.slug || page.slug === '/') continue; const cleanSlug = page.slug.startsWith('/') ? page.slug : `/${page.slug}`; if (cleanSlug === '/guanli' || cleanSlug.startsWith('/guanli/') || cleanSlug === '/admin' || cleanSlug.startsWith('/admin/')) { continue; } xml += ' \n'; xml += ` ${siteUrl}${cleanSlug}\n`; xml += ` weekly\n`; xml += ` 0.7\n`; xml += ' \n'; } // Blog posts (including translations) for (const post of posts) { if (!post.slug || post.slug.startsWith('guanli') || post.slug.startsWith('admin')) continue; xml += ' \n'; xml += ` ${siteUrl}/${post.slug}\n`; xml += ` ${post.date.toISOString().split('T')[0]}\n`; xml += ` monthly\n`; xml += ` 0.9\n`; xml += ' \n'; // Bilingual URLs if (post.availableLanguages) { for (const lang of post.availableLanguages) { if (lang !== post.lang && lang !== 'default') { xml += ' \n'; xml += ` ${siteUrl}/${post.slug}/${lang}\n`; xml += ` ${post.date.toISOString().split('T')[0]}\n`; xml += ` monthly\n`; xml += ` 0.8\n`; xml += ' \n'; } } } } xml += ''; res.header('Content-Type', 'application/xml; charset=utf-8'); res.send(xml); }); router.get('/robots.txt', (req, res) => { const siteUrl = res.locals.site.url; const content = `User-agent: *\nDisallow: /admin/\nAllow: /\n\nSitemap: ${siteUrl}/sitemap.xml\n`; res.header('Content-Type', 'text/plain; charset=utf-8'); res.send(content); }); module.exports = router;