forked from DouyinFE/semi-design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsitemap_update.js
81 lines (74 loc) Β· 2.82 KB
/
sitemap_update.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
79
80
81
const axios = require("axios");
const fastXML = require("fast-xml-parser");
const fs = require("fs/promises");
const execa = require("execa");
const xmlPath = "./sitemap.xml";
const getData = async ()=>{
const xmlRaw = await fs.readFile(xmlPath, "utf8");
const { XMLParser, XMLBuilder, XMLValidator } = fastXML;
const parser = new XMLParser({
ignoreAttributes: false,
ignoreNameSpace: false,
});
let jsObj = parser.parse(xmlRaw);
return jsObj;
};
const writeData = async (jsObj)=>{
const { XMLBuilder } = fastXML;
const builder = new XMLBuilder({
indentBy: " ",
format: true,
ignoreAttributes: false,
ignoreNameSpace: false,
});
const xmlContent = builder.build(jsObj);
await fs.writeFile(xmlPath, xmlContent);
};
const main = async ()=>{
const data = await getData();
const urlMap = {};
data['urlset'].url.forEach(item=>{
urlMap[item.loc] = item;
});
const promiseList = [];
let count = 0;
const urls = Object.keys(urlMap);
const updatedArr = [];
urls.forEach((url)=>{
const item = urlMap[url];
promiseList.push(new Promise(async (resolve, reject)=>{
try {
const res = await axios.get(url);
if (url.startsWith("https://semi.design/zh-CN") || url.startsWith("https://semi.design/en-US")) {
const lang = url.startsWith("https://semi.design/zh-CN") ? "zh-CN" : "en-US";
const mdRelativePath = url.replace(`https://semi.design/${lang}/`, "");
const mdPath = `./content/${mdRelativePath}/${lang==="zh-CN"?"index.md":"index-en-US.md"}`;
const seconds = execa.commandSync(`echo $(git log -1 --pretty="format:%ct" ${mdPath})`, { shell: true }).stdout;
item.lastmod = new Date(seconds * 1000).toISOString();
} else {
const scm = res.headers['X-Deploy-Scm-Version'] || res.headers['X-Deploy-Scm-Version'.toLowerCase()] || res.headers['X-Deploy-Scm-Version'.toUpperCase()];
if (item['scm'] && item['scm']!==scm || !item['scm']) {
item['scm'] = scm;
item.lastmod = new Date().toISOString();
}
}
count++;
console.log(`SiteMap processed ${url} ${count}/${urls.length}`);
resolve();
} catch (e) {
reject(e);
}
}).catch(e=>{
console.log("error", e, url);
}).finally(()=>{
updatedArr.push(item);
}));
});
await Promise.all(promiseList);
updatedArr.sort((itemA, itemB)=>{
return itemA.loc.localeCompare(itemB.loc);
});
data['urlset'].url = updatedArr;
await writeData(data);
};
main();