Skip to content

Commit

Permalink
feat: Added sitemap generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ripixel committed Jun 6, 2020
1 parent 9d1e386 commit 86c79e6
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 29 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
"main": "index.js",
"scripts": {
"dev": "nodemon",
"build": "mkdir -p ./public && npm run build:css && npm run build:favicons && npm run build:pages && npm run build:images",
"build": "mkdir -p ./public && npm run build:css && npm run build:favicons && npm run build:pages && npm run build:images && npm run build:sitemap",
"build:dev": "mkdir -p ./public && npm run build:css && npm run build:pages && npm run build:images",
"build:pages": "ts-node ./scripts/generatePages",
"build:css": "cleancss -o ./public/styles.min.css ./assets/styles/*.css",
"build:favicons": "favicons-generate",
"build:images": "cp -R ./assets/images/. ./public/"
"build:images": "cp -R ./assets/images/. ./public/",
"build:sitemap": "ts-node ./scripts/generateSitemap"
},
"repository": {
"type": "git",
Expand All @@ -29,6 +30,7 @@
"firebase-tools": "^8.2.0",
"nodemon": "^2.0.3",
"ts-node": "^8.10.1",
"typescript": "^3.8.3"
"typescript": "^3.8.3",
"xml-js": "^1.6.11"
}
}
29 changes: 29 additions & 0 deletions scripts/findInDir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as fs from "fs";
import * as path from "path";

export const findInDir = (startPath: string, filter: string): string[] => {
console.log(
`Finding files in directory '${startPath}' containing '${filter}'`
);
if (!fs.existsSync(startPath)) {
console.error("no dir " + startPath);
return [];
}

let foundFiles: string[] = [];
const files = fs.readdirSync(startPath);
for (let i = 0; i < files.length; i++) {
const filename = path.join(startPath, files[i]);
const stat = fs.lstatSync(filename);
if (stat.isDirectory()) {
foundFiles = [...foundFiles, ...findInDir(filename, filter)]; //recurse
} else if (filename.indexOf(filter) >= 0) {
foundFiles.push(filename);
}
}

console.log(`Found ${foundFiles.length} files`);
return foundFiles;
};

export default findInDir;
28 changes: 2 additions & 26 deletions scripts/generatePages.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,8 @@
import * as fs from "fs";
import * as path from "path";

console.log("/// Beginning generation of pages");

const findInDir = (startPath: string, filter: string): string[] => {
console.log(
`Finding files in directory '${startPath}' containing '${filter}'`
);
if (!fs.existsSync(startPath)) {
console.error("no dir " + startPath);
return [];
}
import findInDir from "./findInDir";

let foundFiles: string[] = [];
const files = fs.readdirSync(startPath);
for (let i = 0; i < files.length; i++) {
const filename = path.join(startPath, files[i]);
const stat = fs.lstatSync(filename);
if (stat.isDirectory()) {
foundFiles = [...foundFiles, ...findInDir(filename, filter)]; //recurse
} else if (filename.indexOf(filter) >= 0) {
foundFiles.push(filename);
}
}

console.log(`Found ${foundFiles.length} files`);
return foundFiles;
};
console.log("/// Beginning generation of pages");

const templates = findInDir("./templates", ".html");
const pages = findInDir("./pages", ".html");
Expand Down
94 changes: 94 additions & 0 deletions scripts/generateSitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import * as fs from "fs";
import { js2xml } from "xml-js";

import findInDir from "./findInDir";

const SITE_ROOT = "https://www.ripixel.co.uk/";

console.log("/// Beginning sitemap generation");

const generatedPages = findInDir("./public", ".html")
.filter((page) => page !== "public/404.html" && page !== "public/index.html")
.map((page) => page.replace("public/", "").replace(".html", ""));

console.log(
`Found ${generatedPages.length} public pages (not including index or 404)`
);

const generateUrlElement = (loc: string) => {
return {
type: "element",
name: "url",
elements: [
{
type: "element",
name: "loc",
elements: [
{
type: "text",
text: loc,
},
],
},
{
type: "element",
name: "lastmod",
elements: [
{
type: "text",
text: new Date().toISOString().split("T")[0],
},
],
},
{
type: "element",
name: "changefreq",
elements: [
{
type: "text",
text: loc === SITE_ROOT ? "monthly" : "weekly",
},
],
},
{
type: "element",
name: "priority",
elements: [
{
type: "text",
text: loc === SITE_ROOT ? "1.0" : "0.5",
},
],
},
],
};
};

const sitemapJs = {
declaration: {
attributes: {
version: "1.0",
encoding: "utf-8",
},
},
elements: [
{
type: "element",
name: "urlset",
attributes: {
xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9",
},
elements: [generateUrlElement(SITE_ROOT)],
},
],
};

generatedPages.forEach((page) => {
sitemapJs.elements[0].elements.push(generateUrlElement(SITE_ROOT + page));
});

const sitemapXml = js2xml(sitemapJs, { spaces: 4 });

fs.writeFileSync("./public/sitemap.xml", sitemapXml, "utf8");

console.log("/// Finished sitemap generation");

0 comments on commit 86c79e6

Please sign in to comment.