NordicSemiconductor / asset-tracker-cloud-docs

The nRF Asset Tracker aims to provide a concrete end-to-end example for an IoT product in the asset tracker space.
https://github.com/NordicSemiconductor/asset-tracker-cloud-docs#readme
BSD 3-Clause "New" or "Revised" License
10 stars 10 forks source link

Redirect GitHub pages links to docs.nordicsemi.com #1020

Closed coderbyheart closed 5 months ago

coderbyheart commented 6 months ago

The canonical home for the docs is now https://docs.nordicsemi.com/.

Implement a redirect of all old pages, so we do not break user experience and Google listings.

Example:

https://nordicsemiconductor.github.io/asset-tracker-cloud-docs/saga/docs/aws/Index.html ➡️ https://docs.nordicsemi.com/bundle/nrf-asset-tracker-saga/page/docs/aws/Index.html

coderbyheart commented 5 months ago
import fs, { mkdirSync, writeFileSync } from "node:fs";
import path from "node:path";

const baseDir = process.cwd();

const listFiles = (dir) => {
  const files = fs.readdirSync(dir);

  files.forEach((file) => {
    const filePath = path.join(dir, file);
    const stat = fs.statSync(filePath);

    if (stat.isDirectory()) {
      listFiles(filePath); // Recursively call listFiles for subdirectories
    } else if (file.endsWith(".html")) {
      const [tag, ...rest] = filePath.replace(baseDir, "").slice(1).split("/");
      if (tag === "redirects") return;
      if ([tag, ...rest].length < 2) return;
      console.log(tag, ...rest);
      const target = `https://docs.nordicsemi.com/bundle/nrf-asset-tracker-${tag}/page/${rest.join(
        "/"
      )}`;
      console.log(target);
      try {
        mkdirSync(path.join(baseDir, "redirects", tag, ...rest.slice(0, -1)), {
          recursive: true,
        });
      } catch {}
      writeFileSync(
        path.join(baseDir, "redirects", tag, ...rest),
        [
          `<!DOCTYPE html>`,
          `<html lang="en">`,
          `<head>`,
          `<meta charset="utf-8" />`,
          `<title>nRF Asset Tracker Documentation has moved to docs.nordicsemi.com</title>`,
          `<meta http-equiv="refresh" content="0; URL=${target}" />`,
          `</head>`,
          `<body>`,
          `Moved to <a href="${target}">${target}</a>`,
          `</body>`,
          `</html>`,
        ].join(""),
        "utf-8"
      );
    }
  });
};

listFiles(baseDir);