add sitemap
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
2025-03-18 22:56:41 -04:00
parent 1115a7afdd
commit 78b35a7d83
4 changed files with 123 additions and 18 deletions

View File

@@ -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,
});
});