-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_sitemap.js
78 lines (65 loc) · 2.05 KB
/
generate_sitemap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const fs = require('fs');
const path = require('path');
const BASE_URL = 'https://hashnote-usyc.netlify.app';
// Expanded list of file types to include
const INCLUDED_EXTENSIONS = [
'.html',
'.pdf',
'.txt',
'.sol',
'.ts',
'.csv',
'.xml',
'.png',
'.json'
];
function getFiles(dir, files = []) {
try {
const entries = fs.readdirSync(dir);
for (const entry of entries) {
const entryPath = path.join(dir, entry);
// Skip node_modules and .git
if (entry === 'node_modules' || entry === '.git') continue;
if (fs.statSync(entryPath).isDirectory()) {
getFiles(entryPath, files);
} else {
const ext = path.extname(entry).toLowerCase();
if (INCLUDED_EXTENSIONS.includes(ext)) {
files.push(entryPath);
}
}
}
return files;
} catch (error) {
console.error('Error reading directory:', error);
return files;
}
}
try {
const files = getFiles('.');
const today = new Date().toISOString().split('T')[0];
let sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>${BASE_URL}</loc>
<lastmod>${today}</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>`;
files.forEach(file => {
const relativePath = file.replace(/\\/g, '/').replace('./', '');
const priority = file.includes('usyc') || file.includes('hashnote') ? '0.9' : '0.8';
sitemap += `
<url>
<loc>${BASE_URL}/${relativePath}</loc>
<lastmod>${today}</lastmod>
<changefreq>weekly</changefreq>
<priority>${priority}</priority>
</url>`;
});
sitemap += '\n</urlset>';
fs.writeFileSync('sitemap.xml', sitemap);
console.log('Sitemap generated successfully!');
} catch (error) {
console.error('Error generating sitemap:', error);
}