-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerate-sitemap.js
49 lines (36 loc) · 1.61 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
const fs = require('fs');
const path = require('path');
// Function to recursively get all files in a directory, excluding .git and .github
function getAllFiles(dirPath, arrayOfFiles) {
const files = fs.readdirSync(dirPath);
arrayOfFiles = arrayOfFiles || [];
files.forEach(function(file) {
if (['.git', '.github', 'assets', 'uploads', 'link','static','package.json','generate-sitemap.js','package-lock.json','ads.txt'].includes(file)) {
return;
}
if (fs.statSync(dirPath + '/' + file).isDirectory()) {
arrayOfFiles = getAllFiles(dirPath + '/' + file, arrayOfFiles);
} else {
arrayOfFiles.push(path.join(dirPath, '/', file));
}
});
return arrayOfFiles;
}
// Generate sitemap.xml content
function generateSitemap(files) {
let xml = '<?xml version="1.0" encoding="UTF-8"?>\n';
xml += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n';
files.forEach(function(file) {
const url = 'https://telegeam.github.io/clashnode/' + file.replace('./', ''); // Update URL as needed
const lastmod = new Date().toISOString().split('T')[0]; // Current date as lastmod
xml += `\t<url>\n\t\t<loc>${url}</loc>\n\t\t<lastmod>${lastmod}</lastmod>\n\t\t</url>\n`;
});
xml += '</urlset>';
return xml;
}
// Main script
const files = getAllFiles('./'); // Start from the root directory, you can change this path as needed
const sitemapContent = generateSitemap(files);
// Write sitemap.xml file
fs.writeFileSync('sitemap.xml', sitemapContent);
console.log('sitemap.xml generated successfully!');