|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +// Function to recursively get all files in a directory, excluding .git and .github |
| 5 | +function getAllFiles(dirPath, arrayOfFiles) { |
| 6 | + const files = fs.readdirSync(dirPath); |
| 7 | + |
| 8 | + arrayOfFiles = arrayOfFiles || []; |
| 9 | + |
| 10 | + files.forEach(function(file) { |
| 11 | + if (['.git', '.github', 'assets', 'go', 'link','static','.nojekyll','manifest.json','sw.js','ads.txt'].includes(file)) { |
| 12 | + return; |
| 13 | + } |
| 14 | + if (fs.statSync(dirPath + '/' + file).isDirectory()) { |
| 15 | + arrayOfFiles = getAllFiles(dirPath + '/' + file, arrayOfFiles); |
| 16 | + } else { |
| 17 | + arrayOfFiles.push(path.join(dirPath, '/', file)); |
| 18 | + } |
| 19 | + }); |
| 20 | + |
| 21 | + return arrayOfFiles; |
| 22 | +} |
| 23 | + |
| 24 | +// Generate sitemap.xml content |
| 25 | +function generateSitemap(files) { |
| 26 | + let xml = '<?xml version="1.0" encoding="UTF-8"?>\n'; |
| 27 | + xml += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n'; |
| 28 | + |
| 29 | + files.forEach(function(file) { |
| 30 | + const url = 'https://clashv2ray-hub.github.io/' + file.replace('./', ''); // Update URL as needed |
| 31 | + xml += `\t<url>\n\t\t<loc>${url}</loc>\n\t</url>\n`; |
| 32 | + }); |
| 33 | + |
| 34 | + xml += '</urlset>'; |
| 35 | + |
| 36 | + return xml; |
| 37 | +} |
| 38 | + |
| 39 | +// Main script |
| 40 | +const files = getAllFiles('./'); // Start from the root directory, you can change this path as needed |
| 41 | +const sitemapContent = generateSitemap(files); |
| 42 | + |
| 43 | +// Write sitemap.xml file |
| 44 | +fs.writeFileSync('sitemap.xml', sitemapContent); |
| 45 | + |
| 46 | +console.log('sitemap.xml generated successfully!'); |
0 commit comments