-
Notifications
You must be signed in to change notification settings - Fork 5
/
sitemap.js
54 lines (34 loc) · 832 Bytes
/
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
"use strict";
/*
Program for generating a sitemap
*/
var XML = require('xml');
var fs = require('fs');
function Sitemap () {
this.header = '<?xml version="1.0" encoding="UTF-8"?>\n';
this.json = [{
urlset: [
{_attr: {xmlns: 'http://www.sitemaps.org/schemas/sitemap/0.9'}}
]
}];
return this;
}
Sitemap.prototype.add = function (post) {
this.json[0].urlset.push({
url: [
{loc: post.url},
{lastmod: (new Date(post.lastmod)).toISOString().substring(0, 10)},
{changefreq: 'weekly'}
]
});
}
Sitemap.prototype.create = function () {
try {
var map = this.header + XML(this.json, true);
fs.writeFileSync('./template/sitemap.xml', map);
console.log('Sitemap created!');
} catch (error) {
throw error;
}
}
module.exports = new Sitemap();