You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by ur...@apache.org on 2023/03/21 11:23:18 UTC

[pulsar-site] branch add-canonical-script created (now 2f343dcd914)

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

urfree pushed a change to branch add-canonical-script
in repository https://gitbox.apache.org/repos/asf/pulsar-site.git


      at 2f343dcd914 [util] a script to batch add canonical metadata for SEO optimization

This branch includes the following new commits:

     new 2f343dcd914 [util] a script to batch add canonical metadata for SEO optimization

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[pulsar-site] 01/01: [util] a script to batch add canonical metadata for SEO optimization

Posted by ur...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

urfree pushed a commit to branch add-canonical-script
in repository https://gitbox.apache.org/repos/asf/pulsar-site.git

commit 2f343dcd9147de9d4cf09f1ffb76fc059f50f206
Author: Li Li <ur...@apache.org>
AuthorDate: Tue Mar 21 19:23:06 2023 +0800

    [util] a script to batch add canonical metadata for SEO optimization
    
    Signed-off-by: Li Li <ur...@apache.org>
---
 scripts/add-canonical.js                        | 65 +++++++++++++++++++++++++
 versioned_docs/version-2.11.x/adaptors-kafka.md |  5 ++
 2 files changed, 70 insertions(+)

diff --git a/scripts/add-canonical.js b/scripts/add-canonical.js
new file mode 100644
index 00000000000..4feb2ec9de3
--- /dev/null
+++ b/scripts/add-canonical.js
@@ -0,0 +1,65 @@
+const fs = require("fs");
+const path = require("path");
+const _ = require("lodash");
+
+function travel(dir, callback) {
+  if (fs.statSync(dir).isFile()) {
+    if (dir.endsWith(".md")) {
+      callback(dir);
+    }
+  } else {
+    fs.readdirSync(dir).forEach((file) => {
+      var pathname = path.join(dir, file);
+      if (fs.statSync(pathname).isDirectory()) {
+        travel(pathname, callback);
+      } else if (pathname.endsWith(".md")) {
+        callback(pathname);
+      }
+    });
+  }
+}
+
+function fix(mdpath) {
+  let data = fs.readFileSync(mdpath, "utf8");
+  if (/<link rel="canonical"/g.test(data)) {
+    return;
+  }
+  const idm = /^---\nid:\s*(.+)\s*/g.exec(data);
+  if (!idm || idm.length < 1) {
+    return;
+  }
+  const docId = idm[1];
+  const canonicalUrl = `https://pulsar.apache.org/docs/${docId}/`;
+  const canonicalMeta = `<head>
+    <link rel="canonical" href="${canonicalUrl}" />
+</head>`;
+  data = data.replace(
+    /(sidebar_label:.*\n---\s*\n)/gm,
+    "$1\n" + canonicalMeta + "\n\n"
+  );
+  fs.writeFileSync(mdpath, data);
+  console.log(mdpath + " fixed");
+}
+
+module.exports = (dir) => {
+  travel(dir, fix);
+};
+
+// Example: node scripts/add-canonical.js versioned_docs/version-2.11.x/adaptors-kafka.md
+if (typeof require !== "undefined" && require.main === module) {
+  const [dir] = process.argv.slice(2);
+  travel(path.join(__dirname, "..", dir), fix);
+}
+
+// Usage:
+// Example 1: Update a specify md file:
+// node scripts/add-canonical.js versioned_docs/version-2.11.x/adaptors-kafka.md
+
+// Example 2: Update files in a specify directory:
+// node scripts/add-canonical.js versioned_docs/version-2.11.x
+
+// Example 3: Update all old version docs:
+// node scripts/add-canonical.js versioned_docs
+
+// Example 4: Update all current version docs:
+// node scripts/add-canonical.js docs
\ No newline at end of file
diff --git a/versioned_docs/version-2.11.x/adaptors-kafka.md b/versioned_docs/version-2.11.x/adaptors-kafka.md
index 49761504779..fa3bf3eefbf 100644
--- a/versioned_docs/version-2.11.x/adaptors-kafka.md
+++ b/versioned_docs/version-2.11.x/adaptors-kafka.md
@@ -5,6 +5,11 @@ sidebar_label: "Kafka client wrapper"
 ---
 
 
+
+<head>
+    <link rel="canonical" href="https://pulsar.apache.org/docs/adaptors-kafka/" />
+</head>
+
 Pulsar provides an easy option for applications that are currently written using the [Apache Kafka](http://kafka.apache.org) Java client API.
 
 ## Use the Pulsar Kafka compatibility wrapper