115 lines
3.6 KiB
JavaScript
115 lines
3.6 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Sitemap generator for Hastebin
|
|
* Generates sitemap.xml with static pages and MCP discovery endpoint
|
|
*/
|
|
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
// Get base URL from environment or use default
|
|
const baseUrl = process.env.HASTEBIN_BASE_URL ||
|
|
process.env.HASTEBIN_SITEMAP_URL ||
|
|
'https://haste.nixc.us';
|
|
|
|
// Static pages to include in sitemap
|
|
const pages = [
|
|
{
|
|
loc: '/',
|
|
changefreq: 'weekly',
|
|
priority: '1.0',
|
|
description: 'Homepage - create and share code snippets'
|
|
},
|
|
{
|
|
loc: '/about',
|
|
changefreq: 'monthly',
|
|
priority: '0.8',
|
|
description: 'About page with documentation and usage instructions'
|
|
},
|
|
{
|
|
loc: '/.well-known/mcp.json',
|
|
changefreq: 'monthly',
|
|
priority: '0.7',
|
|
description: 'MCP (Model Context Protocol) discovery endpoint for AI assistants'
|
|
}
|
|
];
|
|
|
|
// Generate sitemap XML
|
|
function generateSitemap() {
|
|
const today = new Date().toISOString().split('T')[0];
|
|
|
|
let xml = '<?xml version="1.0" encoding="UTF-8"?>\n';
|
|
xml += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"\n';
|
|
xml += ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n';
|
|
xml += ' xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9\n';
|
|
xml += ' http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">\n';
|
|
xml += '\n';
|
|
xml += ' <!-- Hastebin Sitemap -->\n';
|
|
xml += ' <!-- Generated: ' + today + ' -->\n';
|
|
xml += ' <!-- Base URL: ' + baseUrl + ' -->\n';
|
|
xml += '\n';
|
|
|
|
for (const page of pages) {
|
|
xml += ' <!-- ' + page.description + ' -->\n';
|
|
xml += ' <url>\n';
|
|
xml += ' <loc>' + baseUrl + page.loc + '</loc>\n';
|
|
xml += ' <lastmod>' + today + '</lastmod>\n';
|
|
xml += ' <changefreq>' + page.changefreq + '</changefreq>\n';
|
|
xml += ' <priority>' + page.priority + '</priority>\n';
|
|
xml += ' </url>\n';
|
|
xml += '\n';
|
|
}
|
|
|
|
xml += '</urlset>\n';
|
|
|
|
return xml;
|
|
}
|
|
|
|
// Generate robots.txt content
|
|
function generateRobotsTxt() {
|
|
let robots = '# Hastebin robots.txt\n';
|
|
robots += '# Updated: ' + new Date().toISOString().split('T')[0] + '\n';
|
|
robots += '\n';
|
|
robots += 'User-agent: *\n';
|
|
robots += '\n';
|
|
robots += '# Allow static pages\n';
|
|
robots += 'Allow: /$\n';
|
|
robots += 'Allow: /about\n';
|
|
robots += '\n';
|
|
robots += '# Allow MCP discovery for AI assistants and tools\n';
|
|
robots += 'Allow: /.well-known/\n';
|
|
robots += 'Allow: /.well-known/mcp.json\n';
|
|
robots += '\n';
|
|
robots += '# Disallow individual pastes (ephemeral content)\n';
|
|
robots += 'Disallow: /raw/\n';
|
|
robots += 'Disallow: /documents/\n';
|
|
robots += '\n';
|
|
robots += '# Disallow paste URLs (random keys)\n';
|
|
robots += '# Pastes are identified by 10-character alphanumeric keys\n';
|
|
robots += 'Disallow: /*.*\n';
|
|
robots += '\n';
|
|
robots += '# Sitemap location\n';
|
|
robots += 'Sitemap: ' + baseUrl + '/sitemap.xml\n';
|
|
|
|
return robots;
|
|
}
|
|
|
|
// Main execution
|
|
const staticDir = path.join(__dirname, '..', 'static');
|
|
|
|
// Generate and write sitemap.xml
|
|
const sitemapContent = generateSitemap();
|
|
const sitemapPath = path.join(staticDir, 'sitemap.xml');
|
|
fs.writeFileSync(sitemapPath, sitemapContent, 'utf8');
|
|
console.log('Generated sitemap.xml at', sitemapPath);
|
|
console.log('Base URL:', baseUrl);
|
|
console.log('Pages included:', pages.length);
|
|
|
|
// Generate and write robots.txt
|
|
const robotsContent = generateRobotsTxt();
|
|
const robotsPath = path.join(staticDir, 'robots.txt');
|
|
fs.writeFileSync(robotsPath, robotsContent, 'utf8');
|
|
console.log('Generated robots.txt at', robotsPath);
|
|
|
|
console.log('\nSitemap generation complete!');
|