forked from IlusionDev/nextjs-sitemap-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.js
186 lines (157 loc) · 5.96 KB
/
core.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const fs = require("fs");
const dateFns = require("date-fns");
const path = require("path");
const prettifyXml = require('prettify-xml')
class SiteMapper {
constructor({
alternateUrls,
baseUrl,
ignoreIndexFiles,
ignoredPaths,
pagesDirectory,
sitemapPath,
targetDirectory,
nextConfigPath,
ignoredExtensions,
pagesConfig
}) {
this.pagesConfig = pagesConfig || {};
this.alternatesUrls = alternateUrls || {};
this.baseUrl = baseUrl;
this.ignoredPaths = ignoredPaths || [];
this.ignoreIndexFiles = ignoreIndexFiles || false;
this.ignoredExtensions = ignoredExtensions || [];
this.pagesdirectory = pagesDirectory;
this.sitemapPath = sitemapPath;
this.targetDirectory = targetDirectory;
this.nextConfigPath = nextConfigPath;
this.sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
`;
if (this.nextConfigPath) {
this.nextConfig = require(nextConfigPath);
if (typeof this.nextConfig === "function") {
this.nextConfig = this.nextConfig([], {});
}
}
}
preLaunch() {
fs.writeFileSync(
path.resolve(this.targetDirectory, "./sitemap.xml"),
this.sitemap,
{
flag: "w"
}
);
}
finish() {
const filepath = path.resolve(this.targetDirectory, "./sitemap.xml")
const xml = fs.readFileSync(filepath)
fs.writeFileSync(filepath, prettifyXml(xml + '</urlset>'))
}
removeEmptyLines(text) {
return text
.split('\n')
.filter((str = '') => Boolean(str.trim()))
.join('\n');
}
/**
*
*/
buildPathMap(dir) {
var pathMap = {};
let data = fs.readdirSync(dir);
for (let site of data) {
// Filter directories
if (site[0] === "_" || site[0] === ".") continue;
let toIgnore = false;
for (let path of this.ignoredPaths) {
if (site.includes(path)) toIgnore = true;
}
if (toIgnore) continue;
// Handle recursive paths
if (fs.lstatSync(dir + path.sep + site).isDirectory()) {
pathMap = {
...pathMap,
...this.buildPathMap(dir + path.sep + site)
};
continue;
}
// Is file
let fileExtension = site.split(".").pop();
//Ignoring file extension by user config
let toIgnoreExtension = false;
for (let extensionToIgnore of this.ignoredExtensions) {
if (extensionToIgnore === fileExtension) toIgnoreExtension = true;
}
if (toIgnoreExtension) continue;
//
let fileNameWithoutExtension = site.substring(
0,
site.length - (fileExtension.length + 1)
);
fileNameWithoutExtension =
this.ignoreIndexFiles && fileNameWithoutExtension === "index"
? ""
: fileNameWithoutExtension;
let newDir = dir.replace(this.pagesdirectory, "").replace(/\\/g, "/");
if (this.ignoreIndexFiles && newDir === "/index") {
newDir = "";
}
let pagePath = newDir + "/" + fileNameWithoutExtension;
pathMap[pagePath] = {
page: pagePath
};
}
return pathMap;
}
// Pass in a url (home is "/", and gets 1.00, every subsequent level gets 0.10 subtracted)
getPriority(url) {
return `<priority>${((100 - (this.addTrailingSlash(url).split('/').length - 2) * 10) / 100).toFixed(2)}</priority>`;
}
addTrailingSlash (path) {
if (path.charAt(path.length - 1) !== '/') {
path += '/'
}
return path
}
async sitemapMapper(dir) {
var pathMap = this.buildPathMap(dir);
const exportPathMap = this.nextConfig && this.nextConfig.exportPathMap;
if (exportPathMap) {
pathMap = await exportPathMap(pathMap, {});
// default Next.js pathmap is missing index page
pathMap['/'] = pathMap['/'] || {page: '/index'}
}
const paths = Object.keys(pathMap).sort();
const date = dateFns.format(new Date(), "YYYY-MM-DD");
for (var i = 0, len = paths.length; i < len; i++) {
let pagePath = paths[i].replace(/\/index$/, '');
let alternates = "";
let priority = this.getPriority(pagePath);
let changefreq = "";
for (let langSite in this.alternatesUrls) {
alternates += `<xhtml:link rel="alternate" hreflang="${langSite}" href="${this.alternatesUrls[langSite]}${pagePath}" />`;
}
if (this.pagesConfig && this.pagesConfig[pagePath.toLowerCase()]) {
let pageConfig = this.pagesConfig[pagePath];
priority = pageConfig.priority ? `<priority>${pageConfig.priority}</priority>` : '';
changefreq = pageConfig.changefreq ? `<changefreq>${pageConfig.changefreq}</changefreq>` : '';
}
let xmlObject = this.removeEmptyLines(
`<url><loc>${this.baseUrl}${pagePath}</loc>
${alternates}
${priority}
${changefreq}
<lastmod>${date}</lastmod>
</url>`
);
fs.writeFileSync(
path.resolve(this.targetDirectory, "./sitemap.xml"),
xmlObject,
{flag: "as"}
);
}
}
}
module.exports = SiteMapper;