-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateSitemap.ts
135 lines (116 loc) · 2.97 KB
/
generateSitemap.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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 generatedPagesLocations = findInDir("./public", ".html");
const generatedPages = generatedPagesLocations
.filter((page) => page !== "public/404.html" && page !== "public/index.html")
.map((page) => page.replace("public/", "").replace(".html", ""));
const pagesModifiedDates: {
[key: string]: Date;
} = {};
const templatePagesLocations = findInDir("./pages", ".html");
const thoughtsPagesLocations = findInDir("./thoughts/articles", ".md");
templatePagesLocations.forEach((pageLocation) => {
pagesModifiedDates[
SITE_ROOT +
pageLocation
.replace("pages/", "")
.replace(".html", "")
.replace("index", "")
] = fs.statSync(pageLocation).mtime;
});
thoughtsPagesLocations.forEach((pageLocation) => {
pagesModifiedDates[
SITE_ROOT + pageLocation.replace("articles/", "").replace(".md", "")
] = fs.statSync(pageLocation).mtime;
});
console.log(
`Found ${generatedPages.length} public pages (not including index or 404)`
);
const generateUrlElement = (loc: string) => {
let changefreq = loc === SITE_ROOT ? "monthly" : "weekly";
let prio = loc === SITE_ROOT ? "1.0" : "0.8";
if (loc.indexOf("thoughts/") > -1) {
// is an article/subpage
prio = "1";
changefreq = "yearly";
}
if (loc.indexOf("changelog") > 1) {
// it's the changelog
prio = "0.1";
changefreq = "weekly";
}
console.log(loc, prio, changefreq);
return {
type: "element",
name: "url",
elements: [
{
type: "element",
name: "loc",
elements: [
{
type: "text",
text: loc,
},
],
},
{
type: "element",
name: "lastmod",
elements: [
{
type: "text",
text: pagesModifiedDates[loc].toISOString().split("T")[0],
},
],
},
{
type: "element",
name: "changefreq",
elements: [
{
type: "text",
text: changefreq,
},
],
},
{
type: "element",
name: "priority",
elements: [
{
type: "text",
text: prio,
},
],
},
],
};
};
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");