-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgenerateSitemap.js
More file actions
93 lines (85 loc) · 2.81 KB
/
Copy pathgenerateSitemap.js
File metadata and controls
93 lines (85 loc) · 2.81 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const fs = require('fs');
const path = require('path');
const markdownFolder = './_posts'; // 指定你的Markdown文件夹路径
const outputFilePath = './sitemap.xml'; // 指定生成的sitemap.xml文件路径
const siteUrlPrefix = 'https://tggsearch.github.io/'; // 指定网站URL前缀
const currentDate = new Date();
const formattedDate = currentDate.toISOString();
// 开头部分
let sitemapContent = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://tggsearch.github.io/</loc>
<lastmod>${formattedDate}</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>https://tggsearch.github.io/telegram.html</loc>
<lastmod>${formattedDate}</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://tggsearch.github.io/twitter.html</loc>
<lastmod>${formattedDate}</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://tggsearch.github.io/account.html</loc>
<lastmod>${formattedDate}</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://tggsearch.github.io/exchange.html</loc>
<lastmod>${formattedDate}</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://tggsearch.github.io/youtube.html</loc>
<lastmod>${formattedDate}</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://tggsearch.github.io/line.html</loc>
<lastmod>${formattedDate}</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://tggsearch.github.io/gmail.html</loc>
<lastmod>${formattedDate}</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
`;
// 读取Markdown文件夹中的文件
fs.readdirSync(markdownFolder).forEach(file => {
if(file.endsWith('302.md')){
return;
}
if (file.endsWith('.md')) {
const fileNameWithoutExtension = file.replace('.md', '').replace(/^\d{4}-\d{2}-\d{2}-/, ''); // 去掉日期部分
const url = `${siteUrlPrefix}docs/${fileNameWithoutExtension}.html`;
const filePath = path.join(markdownFolder, file);
const stat = fs.statSync(filePath);
const lastMod = stat.mtime.toISOString();
// 生成sitemap条目
sitemapContent += `<url>
<loc>${encodeURI(url)}</loc>
<lastmod>${lastMod}</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>`;
}
});
// 结尾部分
sitemapContent += `
</urlset>`;
// 将生成的内容写入文件
fs.writeFileSync(outputFilePath, sitemapContent, 'utf-8');
console.log(`Sitemap generated successfully at ${outputFilePath}`);