sitemap_update.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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=>{
  40. const res = await axios.get(url);
  41. if (url.startsWith("https://semi.design/zh-CN") || url.startsWith("https://semi.design/en-US")) {
  42. const lang = url.startsWith("https://semi.design/zh-CN") ? "zh-CN" : "en-US";
  43. const mdRelativePath = url.replace(`https://semi.design/${lang}/`, "");
  44. const mdPath = `./content/${mdRelativePath}/${lang==="zh-CN"?"index.md":"index-en-US.md"}`;
  45. const seconds = execa.commandSync(`echo $(git log -1 --pretty="format:%ct" ${mdPath})`, { shell: true }).stdout;
  46. item.lastmod = new Date(seconds * 1000).toISOString();
  47. } else {
  48. const scm = res.headers['X-Deploy-Scm-Version'] || res.headers['X-Deploy-Scm-Version'.toLowerCase()] || res.headers['X-Deploy-Scm-Version'.toUpperCase()];
  49. if (item['scm'] && item['scm']!==scm || !item['scm']) {
  50. item['scm'] = scm;
  51. item.lastmod = new Date().toISOString();
  52. }
  53. }
  54. count++;
  55. console.log(`SiteMap processed ${url} ${count}/${urls.length}`);
  56. resolve();
  57. }).catch(e=>{
  58. console.log("error", e, url);
  59. }).finally(()=>{
  60. updatedArr.push(item);
  61. }));
  62. });
  63. await Promise.all(promiseList);
  64. updatedArr.sort((itemA, itemB)=>{
  65. return itemA.loc.localeCompare(itemB.loc);
  66. });
  67. data['urlset'].url = updatedArr;
  68. await writeData(data);
  69. };
  70. main();