This is an automated email from the ASF dual-hosted git repository.

juzhiyuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-website.git


The following commit(s) were added to refs/heads/master by this push:
     new c845c121e2c fix: blog related config, scripts (#1220)
c845c121e2c is described below

commit c845c121e2cd7b876e947addf7a72adbf3d65723
Author: Young <[email protected]>
AuthorDate: Tue Jul 19 13:00:34 2022 +0800

    fix: blog related config, scripts (#1220)
---
 blog/en/docusaurus.config.js  |  2 +-
 scripts/update-sitemap-loc.js | 78 +++++++++++++++++++++++++++----------------
 2 files changed, 51 insertions(+), 29 deletions(-)

diff --git a/blog/en/docusaurus.config.js b/blog/en/docusaurus.config.js
index cd4d287dbe3..8471ccaa9dd 100644
--- a/blog/en/docusaurus.config.js
+++ b/blog/en/docusaurus.config.js
@@ -14,7 +14,7 @@ module.exports = {
   onBrokenMarkdownLinks: 'ignore',
   noIndex: false,
   i18n: {
-    defaultLocale: 'zh',
+    defaultLocale: 'en',
     locales: ['en', 'zh'],
     localeConfigs: {
       en: {
diff --git a/scripts/update-sitemap-loc.js b/scripts/update-sitemap-loc.js
index b6d647c0086..1d10b644852 100644
--- a/scripts/update-sitemap-loc.js
+++ b/scripts/update-sitemap-loc.js
@@ -1,32 +1,54 @@
-/**
- * This script updates the sitemap.xml file with the correct location
- * See https://github.com/apache/apisix-website/issues/705
-*/
+const { js2xml, xml2js } = require('xml-js');
+const { stat, readFile, writeFile } = require('node:fs/promises');
+const Listr = require('listr');
 
-const convert = require('xml-js');
-const fs = require('fs');
+const sitemapXMLs = [
+  ['../website/build/sitemap.xml', '../blog/en/build/sitemap.xml'],
+  ['../website/build/zh/sitemap.xml', '../blog/zh/build/sitemap.xml'],
+];
 
-const SITEMAP_XML_PATH = '../website/build/sitemap.xml';
-try {
-  if (!fs.existsSync(SITEMAP_XML_PATH)) {
-    throw new Error(`${SITEMAP_XML_PATH} does not exist`);
-  }
+const tasks = new Listr([
+  {
+    title: `Check sitemap.xml files exist`,
+    task: () => Promise.all(
+      sitemapXMLs
+        .flat()
+        .map((f) => stat(f).then((stat) => (stat.isFile()
+          ? Promise.resolve()
+          : Promise.reject(new Error(`${f} is not a file`))))),
+    ),
+  },
+  {
+    title: `Merge sitemap.xml files`,
+    task: () => new Listr(
+      sitemapXMLs.map((group) => ({
+        title: `Merge ${group[0]}`,
+        task: () => Promise.all(
+          group.map((f) => readFile(f, 'utf8').then((xml) => xml2js(xml, { 
compact: true }))),
+        )
+          .then((sitemaps) => {
+            const res = sitemaps[0];
+            for (let i = 1; i < sitemaps.length; i += 1) {
+              res.urlset.url = [
+                ...res.urlset.url,
+                ...sitemaps[i].urlset.url,
+              ];
+            }
+            return res;
+          })
+          .then((sitemap) => writeFile(group[0], js2xml(sitemap, { compact: 
true }, 'utf-8'))),
+      })),
+      { concurrent: sitemapXMLs.length },
+    ),
+  },
+]);
 
-  const xml = fs.readFileSync(SITEMAP_XML_PATH, 'utf8');
-  const result = JSON.parse(convert.xml2json(xml, { compact: true }));
-  result.urlset.url = result.urlset.url.map((item) => {
-    // eslint-disable-next-line no-underscore-dangle
-    const targetLoc = item.loc._text.endsWith('/') ? item.loc._text : 
`${item.loc._text}/`;
-    return {
-      loc: {
-        _text: targetLoc,
-      },
-      changefreq: item.changefreq,
-      priority: item.priority,
-    };
+tasks
+  .run()
+  .then(() => {
+    console.log(`[Finish] Generate sitemap.xml`);
+  })
+  .catch((err) => {
+    console.error(err);
+    process.exit(1);
   });
-  fs.writeFileSync(SITEMAP_XML_PATH, convert.json2xml(result, { compact: true, 
spaces: 4 }), 'utf8');
-  console.log(`Updated ${SITEMAP_XML_PATH} successfully`);
-} catch (error) {
-  console.warn(error);
-}

Reply via email to