sitemap_update.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. const axios = require("axios");
  2. const fastXML = require("fast-xml-parser");
  3. const fs = require("fs/promises");
  4. const execa = require("execa");
  5. const xmlPath = "./sitemap.xml";
  6. const getData = async ()=>{
  7. const xmlRaw = await fs.readFile(xmlPath, "utf8");
  8. const { XMLParser, XMLBuilder, XMLValidator } = fastXML;
  9. const parser = new XMLParser({
  10. ignoreAttributes: false,
  11. ignoreNameSpace: false,
  12. });
  13. let jsObj = parser.parse(xmlRaw);
  14. return jsObj;
  15. };
  16. const writeData = async (jsObj)=>{
  17. const { XMLBuilder } = fastXML;
  18. const builder = new XMLBuilder({
  19. indentBy: " ",
  20. format: true,
  21. ignoreAttributes: false,
  22. ignoreNameSpace: false,
  23. });
  24. const xmlContent = builder.build(jsObj);
  25. await fs.writeFile(xmlPath, xmlContent);
  26. };
  27. const main = async ()=>{
  28. const data = await getData();
  29. const urlMap = {};
  30. data['urlset'].url.forEach(item=>{
  31. urlMap[item.loc] = item;
  32. });
  33. const promiseList = [];
  34. let count = 0;
  35. const urls = Object.keys(urlMap);
  36. const updatedArr = [];
  37. urls.forEach((url)=>{
  38. const item = urlMap[url];
  39. promiseList.push(new Promise(async (resolve, reject)=>{
  40. try {
  41. const res = await axios.get(url);
  42. if (url.startsWith("https://semi.design/zh-CN") || url.startsWith("https://semi.design/en-US")) {
  43. const lang = url.startsWith("https://semi.design/zh-CN") ? "zh-CN" : "en-US";
  44. const mdRelativePath = url.replace(`https://semi.design/${lang}/`, "");
  45. const mdPath = `./content/${mdRelativePath}/${lang==="zh-CN"?"index.md":"index-en-US.md"}`;
  46. const seconds = execa.commandSync(`echo $(git log -1 --pretty="format:%ct" ${mdPath})`, { shell: true }).stdout;
  47. item.lastmod = new Date(seconds * 1000).toISOString();
  48. } else {
  49. const scm = res.headers['X-Deploy-Scm-Version'] || res.headers['X-Deploy-Scm-Version'.toLowerCase()] || res.headers['X-Deploy-Scm-Version'.toUpperCase()];
  50. if (item['scm'] && item['scm']!==scm || !item['scm']) {
  51. item['scm'] = scm;
  52. item.lastmod = new Date().toISOString();
  53. }
  54. }
  55. count++;
  56. console.log(`SiteMap processed ${url} ${count}/${urls.length}`);
  57. resolve();
  58. } catch (e) {
  59. reject(e);
  60. }
  61. }).catch(e=>{
  62. console.log("error", e, url);
  63. }).finally(()=>{
  64. updatedArr.push(item);
  65. }));
  66. });
  67. await Promise.all(promiseList);
  68. updatedArr.sort((itemA, itemB)=>{
  69. return itemA.loc.localeCompare(itemB.loc);
  70. });
  71. data['urlset'].url = updatedArr;
  72. await writeData(data);
  73. };
  74. main();