From 78b35a7d8358d1b4de5c50dd43574c41914da8c4 Mon Sep 17 00:00:00 2001
From: David Ashby <delta.mu.alpha@gmail.com>
Date: Tue, 18 Mar 2025 22:56:41 -0400
Subject: [PATCH] add sitemap

---
 index.js          | 40 ++++++++++++++-----
 package-lock.json | 97 +++++++++++++++++++++++++++++++++++++++++++----
 package.json      |  3 +-
 robots.txt        |  1 +
 4 files changed, 123 insertions(+), 18 deletions(-)

diff --git a/index.js b/index.js
index 7c5bd8c..5e819ef 100644
--- a/index.js
+++ b/index.js
@@ -1,13 +1,15 @@
 import { load } from "cheerio";
 import { readFile, writeFileSync, mkdirSync, cpSync, rmSync } from "fs";
 import process from "child_process";
+import { simpleSitemapAndIndex } from "sitemap";
+
 import stores from "./stores.json" with { type: "json" };
 
 function mkDir(path) {
   try {
-    return mkdirSync(path)
+    return mkdirSync(path);
   } catch (err) {
-    if (err.code !== 'EEXIST') throw err
+    if (err.code !== "EEXIST") throw err;
   }
 }
 
@@ -17,7 +19,7 @@ function writeFile(path, content) {
   } catch (err) {
     if (err) throw err;
   }
-   console.log(`${path} updated.`);
+  console.log(`${path} updated.`);
 }
 
 function slugify(str) {
@@ -41,10 +43,15 @@ function cleanWebsite(str) {
 }
 
 function metaDescription({ name, meta, description }) {
-  if (meta && (meta.length > 155 || meta.length < 145 && meta.length !== 0)) {
-    console.log(`warning: meta tag for ${name} is invalid: 145/${meta.length}/155`)
+  if (meta && (meta.length > 155 || (meta.length < 145 && meta.length !== 0))) {
+    console.log(
+      `warning: meta tag for ${name} is invalid: 145/${meta.length}/155`
+    );
   }
-  return meta || description.length > 155 ? description.slice(0, 153) + "..." : description || "A guide to and map of every independent bookstore in New York City. We have a complete list of community bookstores in NYC with locations and descriptions."
+  return meta || description.length > 155
+    ? description.slice(0, 153) + "..."
+    : description ||
+        "A guide to and map of every independent bookstore in New York City. We have a complete list of community bookstores in NYC with locations and descriptions.";
 }
 
 function GetRecentChanges() {
@@ -176,15 +183,17 @@ readFile("./index.tmpl.html", function (err, data) {
 
   rmSync("./build", { recursive: true, force: true });
 
-  mkDir("./build")
-  
-  writeFile("./build/index.html", $.html())
+  mkDir("./build");
+
+  writeFile("./build/index.html", $.html());
 
   cpSync("./site.css", "./build/site.css");
   cpSync("./robots.txt", "./build/robots.txt");
-  cpSync("./img", "./build/img", {recursive: true});
+  cpSync("./img", "./build/img", { recursive: true });
   cpSync("./stores.json", "./build/stores.json");
 
+  let pages = [{ url: `/`, changefreq: "weekly", priority: 1.0 }];
+
   stores.forEach((store) => {
     $("#selected").html(SelectedStoreTemplate(store));
     $("#info").addClass("hidden");
@@ -194,5 +203,16 @@ readFile("./index.tmpl.html", function (err, data) {
     $("meta[name='description']").attr("content", metaDescription(store));
     mkDir(`./build/${slugify(store.name)}`);
     writeFile(`./build/${slugify(store.name)}/index.html`, $.html());
+    pages.push({
+      url: `/${slugify(store.name)}/`,
+      changefreq: "monthly",
+      priority: 0.5,
+    });
+  });
+  simpleSitemapAndIndex({
+    hostname: "https://nycbookstores.org/",
+    destinationDir: `./build/`,
+    sourceData: pages,
+    gzip: false,
   });
 });
diff --git a/package-lock.json b/package-lock.json
index 5f17ae3..9328ab5 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -9,9 +9,31 @@
       "version": "1.0.0",
       "license": "BSD-3-Clause",
       "dependencies": {
-        "cheerio": "^1.0.0-rc.12"
+        "cheerio": "^1.0.0",
+        "sitemap": "^8.0.0"
       }
     },
+    "node_modules/@types/node": {
+      "version": "17.0.45",
+      "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.45.tgz",
+      "integrity": "sha512-w+tIMs3rq2afQdsPJlODhoUEKzFP1ayaoyl1CcnwtIlsVe7K7bA1NGm4s3PraqTLlXnbIN84zuBlxBWo1u9BLw==",
+      "license": "MIT"
+    },
+    "node_modules/@types/sax": {
+      "version": "1.2.7",
+      "resolved": "https://registry.npmjs.org/@types/sax/-/sax-1.2.7.tgz",
+      "integrity": "sha512-rO73L89PJxeYM3s3pPPjiPgVVcymqU490g0YO5n5By0k2Erzj6tay/4lr1CHAAU4JyOWd1rpQ8bCf6cZfHU96A==",
+      "license": "MIT",
+      "dependencies": {
+        "@types/node": "*"
+      }
+    },
+    "node_modules/arg": {
+      "version": "5.0.2",
+      "resolved": "https://registry.npmjs.org/arg/-/arg-5.0.2.tgz",
+      "integrity": "sha512-PYjyFOLKQ9y57JvQ6QLo8dAgNqswh8M1RMJYdQduT6xbWSgK36P/Z/v+p888pM69jMMfS8Xd8F6I1kQ/I9HUGg==",
+      "license": "MIT"
+    },
     "node_modules/boolbase": {
       "version": "1.0.0",
       "resolved": "https://registry.npmjs.org/boolbase/-/boolbase-1.0.0.tgz",
@@ -21,6 +43,7 @@
       "version": "1.0.0",
       "resolved": "https://registry.npmjs.org/cheerio/-/cheerio-1.0.0.tgz",
       "integrity": "sha512-quS9HgjQpdaXOvsZz82Oz7uxtXiy6UIsIQcpBj7HRw2M63Skasm9qlDocAM7jNuaxdhpPU7c4kJN+gA5MCu4ww==",
+      "license": "MIT",
       "dependencies": {
         "cheerio-select": "^2.1.0",
         "dom-serializer": "^2.0.0",
@@ -236,10 +259,36 @@
       "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
       "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
     },
+    "node_modules/sax": {
+      "version": "1.4.1",
+      "resolved": "https://registry.npmjs.org/sax/-/sax-1.4.1.tgz",
+      "integrity": "sha512-+aWOz7yVScEGoKNd4PA10LZ8sk0A/z5+nXQG5giUO5rprX9jgYsTdov9qCchZiPIZezbZH+jRut8nPodFAX4Jg==",
+      "license": "ISC"
+    },
+    "node_modules/sitemap": {
+      "version": "8.0.0",
+      "resolved": "https://registry.npmjs.org/sitemap/-/sitemap-8.0.0.tgz",
+      "integrity": "sha512-+AbdxhM9kJsHtruUF39bwS/B0Fytw6Fr1o4ZAIAEqA6cke2xcoO2GleBw9Zw7nRzILVEgz7zBM5GiTJjie1G9A==",
+      "license": "MIT",
+      "dependencies": {
+        "@types/node": "^17.0.5",
+        "@types/sax": "^1.2.1",
+        "arg": "^5.0.0",
+        "sax": "^1.2.4"
+      },
+      "bin": {
+        "sitemap": "dist/cli.js"
+      },
+      "engines": {
+        "node": ">=14.0.0",
+        "npm": ">=6.0.0"
+      }
+    },
     "node_modules/undici": {
-      "version": "6.19.7",
-      "resolved": "https://registry.npmjs.org/undici/-/undici-6.19.7.tgz",
-      "integrity": "sha512-HR3W/bMGPSr90i8AAp2C4DM3wChFdJPLrWYpIS++LxS8K+W535qftjt+4MyjNYHeWabMj1nvtmLIi7l++iq91A==",
+      "version": "6.21.2",
+      "resolved": "https://registry.npmjs.org/undici/-/undici-6.21.2.tgz",
+      "integrity": "sha512-uROZWze0R0itiAKVPsYhFov9LxrPMHLMEQFszeI2gCN6bnIIZ8twzBCJcN2LJrBBLfrP0t1FW0g+JmKVl8Vk1g==",
+      "license": "MIT",
       "engines": {
         "node": ">=18.17"
       }
@@ -265,6 +314,24 @@
     }
   },
   "dependencies": {
+    "@types/node": {
+      "version": "17.0.45",
+      "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.45.tgz",
+      "integrity": "sha512-w+tIMs3rq2afQdsPJlODhoUEKzFP1ayaoyl1CcnwtIlsVe7K7bA1NGm4s3PraqTLlXnbIN84zuBlxBWo1u9BLw=="
+    },
+    "@types/sax": {
+      "version": "1.2.7",
+      "resolved": "https://registry.npmjs.org/@types/sax/-/sax-1.2.7.tgz",
+      "integrity": "sha512-rO73L89PJxeYM3s3pPPjiPgVVcymqU490g0YO5n5By0k2Erzj6tay/4lr1CHAAU4JyOWd1rpQ8bCf6cZfHU96A==",
+      "requires": {
+        "@types/node": "*"
+      }
+    },
+    "arg": {
+      "version": "5.0.2",
+      "resolved": "https://registry.npmjs.org/arg/-/arg-5.0.2.tgz",
+      "integrity": "sha512-PYjyFOLKQ9y57JvQ6QLo8dAgNqswh8M1RMJYdQduT6xbWSgK36P/Z/v+p888pM69jMMfS8Xd8F6I1kQ/I9HUGg=="
+    },
     "boolbase": {
       "version": "1.0.0",
       "resolved": "https://registry.npmjs.org/boolbase/-/boolbase-1.0.0.tgz",
@@ -422,10 +489,26 @@
       "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
       "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
     },
+    "sax": {
+      "version": "1.4.1",
+      "resolved": "https://registry.npmjs.org/sax/-/sax-1.4.1.tgz",
+      "integrity": "sha512-+aWOz7yVScEGoKNd4PA10LZ8sk0A/z5+nXQG5giUO5rprX9jgYsTdov9qCchZiPIZezbZH+jRut8nPodFAX4Jg=="
+    },
+    "sitemap": {
+      "version": "8.0.0",
+      "resolved": "https://registry.npmjs.org/sitemap/-/sitemap-8.0.0.tgz",
+      "integrity": "sha512-+AbdxhM9kJsHtruUF39bwS/B0Fytw6Fr1o4ZAIAEqA6cke2xcoO2GleBw9Zw7nRzILVEgz7zBM5GiTJjie1G9A==",
+      "requires": {
+        "@types/node": "^17.0.5",
+        "@types/sax": "^1.2.1",
+        "arg": "^5.0.0",
+        "sax": "^1.2.4"
+      }
+    },
     "undici": {
-      "version": "6.19.7",
-      "resolved": "https://registry.npmjs.org/undici/-/undici-6.19.7.tgz",
-      "integrity": "sha512-HR3W/bMGPSr90i8AAp2C4DM3wChFdJPLrWYpIS++LxS8K+W535qftjt+4MyjNYHeWabMj1nvtmLIi7l++iq91A=="
+      "version": "6.21.2",
+      "resolved": "https://registry.npmjs.org/undici/-/undici-6.21.2.tgz",
+      "integrity": "sha512-uROZWze0R0itiAKVPsYhFov9LxrPMHLMEQFszeI2gCN6bnIIZ8twzBCJcN2LJrBBLfrP0t1FW0g+JmKVl8Vk1g=="
     },
     "whatwg-encoding": {
       "version": "3.1.1",
diff --git a/package.json b/package.json
index 8788234..21e7d1b 100644
--- a/package.json
+++ b/package.json
@@ -14,7 +14,8 @@
   },
   "homepage": "https://www.nycbookstores.org/",
   "dependencies": {
-    "cheerio": "^1.0.0-rc.12"
+    "cheerio": "^1.0.0",
+    "sitemap": "^8.0.0"
   },
   "type": "module"
 }
diff --git a/robots.txt b/robots.txt
index e69de29..6dc9126 100644
--- a/robots.txt
+++ b/robots.txt
@@ -0,0 +1 @@
+Sitemap: https://nycbookstores.org/sitemap-index.xml