You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by xu...@apache.org on 2025/03/19 05:08:08 UTC

(opendal) branch main updated: website: Try fix external images in client route (#5806)

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

xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opendal.git


The following commit(s) were added to refs/heads/main by this push:
     new 1e8e014bc website: Try fix external images in client route (#5806)
1e8e014bc is described below

commit 1e8e014bcb36b8f6fa95d57df80f6cda1c6ce235
Author: Xuanwo <gi...@xuanwo.io>
AuthorDate: Wed Mar 19 13:08:03 2025 +0800

    website: Try fix external images in client route (#5806)
    
    Signed-off-by: Xuanwo <gi...@xuanwo.io>
---
 website/plugins/image-ssr-plugin.js | 78 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 77 insertions(+), 1 deletion(-)

diff --git a/website/plugins/image-ssr-plugin.js b/website/plugins/image-ssr-plugin.js
index a2aee0fbd..ed53a39d5 100644
--- a/website/plugins/image-ssr-plugin.js
+++ b/website/plugins/image-ssr-plugin.js
@@ -40,7 +40,8 @@ module.exports = function (context) {
 
       if (
         imageUrl.includes("img.shields.io") ||
-        imageUrl.includes("actions?query")
+        imageUrl.includes("actions?query") ||
+        imageUrl.includes("github/actions/workflow")
       ) {
         ext = ".svg";
       }
@@ -89,6 +90,78 @@ module.exports = function (context) {
     }
   }
 
+  async function processJSFiles(outDir) {
+    console.log("Processing JS files for external images...");
+
+    const jsFiles = [];
+
+    async function findJSFiles(dir) {
+      const entries = await fs.readdir(dir, { withFileTypes: true });
+
+      for (const entry of entries) {
+        const fullPath = path.join(dir, entry.name);
+        if (entry.isDirectory()) {
+          await findJSFiles(fullPath);
+        } else if (entry.name.endsWith(".js")) {
+          jsFiles.push(fullPath);
+        }
+      }
+    }
+
+    await findJSFiles(outDir);
+
+    for (const jsFile of jsFiles) {
+      const content = await fs.readFile(jsFile, "utf8");
+      let modified = false;
+      let newContent = content;
+
+      // Look for shield.io and other image URLs with a more comprehensive regex
+      const urlPatterns = [
+        /"(https?:\/\/[^"]+\.(png|jpg|jpeg|gif|svg|webp))"/g,
+        /"(https?:\/\/img\.shields\.io\/[^"]+)"/g,
+        /"(https?:\/\/github\.com\/[^"]+\/actions\/workflow[^"]+)"/g,
+        /'(https?:\/\/[^']+\.(png|jpg|jpeg|gif|svg|webp))'/g,
+        /'(https?:\/\/img\.shields\.io\/[^']+)'/g,
+        /'(https?:\/\/github\.com\/[^']+\/actions\/workflow[^']+)'/g,
+      ];
+
+      const allReplacements = [];
+
+      for (const pattern of urlPatterns) {
+        const matches = Array.from(newContent.matchAll(pattern));
+
+        for (const match of matches) {
+          const imageUrl = match[1];
+          if (!imageUrl) continue;
+
+          try {
+            const localUrl = await downloadImage(imageUrl, outDir);
+            if (localUrl !== imageUrl) {
+              allReplacements.push({
+                original: match[0],
+                replacement: match[0].replace(imageUrl, localUrl),
+              });
+              modified = true;
+            }
+          } catch (error) {
+            console.error(`Error processing URL in JS file: ${error.message}`);
+          }
+        }
+      }
+
+      // Apply replacements from longest to shortest to avoid partial replacements
+      allReplacements.sort((a, b) => b.original.length - a.original.length);
+
+      for (const { original, replacement } of allReplacements) {
+        newContent = newContent.replace(original, replacement);
+      }
+
+      if (modified) {
+        await fs.writeFile(jsFile, newContent);
+      }
+    }
+  }
+
   return {
     name: "docusaurus-ssr-image-plugin",
 
@@ -151,6 +224,9 @@ module.exports = function (context) {
         }
       }
 
+      // Process JS files to update image references in bundled JavaScript
+      await processJSFiles(outDir);
+
       console.log(`Processed ${processedImages.size} external images`);
     },
   };