forked from ccported/ccported.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_sitemap.js
124 lines (115 loc) · 3.58 KB
/
generate_sitemap.js
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
const base = process.argv[2] || "https://ccported.github.io";
const toModify = process.argv[3] || "no";
const fs = require("fs");
const other_domains = ["https://ccported.onrender.com"]; // Add your alternate domains here
const absLinks = [
"/signup/",
"/login/",
"/",
"/chat/",
"/profile/",
"/roms/"
]
const { games: gamesJSON } = require("./static/games.json");
const parseXml = require("./sitemap_parser.js");
function linkExtractor(xmlString) {
const { root } = parseXml(xmlString);
const urls = root.children.filter(child => child.name === "url");
const links = urls.map(url => {
let loc;
let lastMod;
for (let child of url.children) {
if (child.name === "loc") {
loc = child;
}
if (child.name === "lastmod") {
lastMod = child;
}
}
if(lastMod){
lastMod = lastMod.children[0].content;
}
if (loc) {
loc = loc.children[0].content
}
return [loc, lastMod];
})
return links
}
function format(link, priority = 1.00, lastMod = new Date().toISOString()) {
return [base + link, priority, lastMod];
}
function generateAlternateLinks(path) {
return other_domains.map(domain =>
` <xhtml:link
rel="alternate"
href="${domain}${path}"/>`
).join('\n');
}
function xml([url, priority = 1.00, lastMod = new Date().toISOString()]) {
const path = new URL(url).pathname;
const alternateLinks = generateAlternateLinks(path);
return `
<url>
<loc>${url}</loc>
<lastmod>${lastMod}</lastmod>
<priority>${priority}</priority>
<xhtml:link
rel="alternate"
hreflang="x-default"
href="${url}"/>
${alternateLinks}
</url>`;
}
function getLinks(existing, toModify) {
let eJson = new Map();
let links = new Set();
for (let [link, lastMod] of existing) {
eJson.set(link, lastMod);
}
for(let link of absLinks){
const fullLink = base + link;
if(eJson.has(fullLink)){
if(toModify === "yes"){
links.add(format(link, 1.00, new Date().toISOString()));
}else{
links.add(format(link, 1.00, eJson.get(fullLink)));
}
}else{
links.add(format(link, 1.00, new Date().toISOString()));
}
}
for (let game of gamesJSON) {
const xlinks = game.links;
const gameLinks = xlinks.map(link => link.link);
for(let gameLink of gameLinks) {
let path = new URL(gameLink, base).pathname;
if (eJson.has(gameLink)) {
if (toModify === "yes") {
links.add(format(path, 0.8));
}else{
links.add(format(path, 0.8, eJson.get(gameLink)));
}
} else {
links.add(format(path, 0.8));
}
}
}
return links;
}
function main() {
const existing = linkExtractor(fs.readFileSync("static/sitemap.xml", "utf8"));
const links = Array.from(getLinks(existing, toModify));
const xmls = links.map(xml);
const xmlString = `<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
${xmls.join("\n")}
</urlset>`;
fs.writeFileSync("static/sitemap.xml", xmlString);
}
main();