forked from fetchlogic/simple-sitemap
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
150 lines (120 loc) · 3.32 KB
/
index.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
#!/usr/bin/env node --harmony
var fs = require('fs');
var util = require('util');
var AjaxRequest = require('xhr2');
var async = require('async');
/**
*
* @param {string} siteURL
* @param {string} siteDistPath
* @param {boolean} dryRun
* @constructor
*/
module.exports = function Sitemap(siteURL, siteDistPath, dryRun) {
if(!siteDistPath){
siteDistPath = "/";
}
if (!dryRun) {
dryRun = false;
}
if (!/^(f|ht)tps?:\/\//i.test(siteURL)) {
siteURL = "http://" + siteURL;
}
this.pages = [];
this.sitemapCount = 1;
this.robots = [];
this.searchEngines = [
{name:"Google", URL: "www.google.com"},
{name:"Bing", URL: "www.bing.com"}
];
this.add = function(url, changeFreq, priority) {
if(!changeFreq){
changeFreq = "Daily";
}
if(!priority){
priority = "0.5";
}
this.pages.push({
url: url,
changeFreq: changeFreq,
priority: priority
});
if (this.pages.length >= 50000) {
this._flush();
}
},
// called in the end when all pages are added
this.flush = function(callback) {
var lastmod = (new Date()).toISOString();
this._flush();
var xml = [
'<?xml version="1.0" encoding="UTF-8"?>',
'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
].join('');
for (var i = 1; i < this.sitemapCount; i++) {
var filename = 'sitemap-';
if (this.sitemapCount < 10) {
filename += '0';
}
filename += i + '.xml';
xml += [
'<sitemap>',
'<loc>' + siteURL + '/' + filename + '</loc>',
'<lastmod>' + lastmod + '</lastmod>',
'</sitemap>'
].join('');
}
xml += '</sitemapindex>';
util.log('Flushing sitemap-index to disk');
fs.writeFileSync(siteDistPath + '/sitemap-index.xml', xml);
if (true === dryRun) {
if(callback){
callback();
}
return;
}
// Submit sitemapindex to the search engines
async.each(this.searchEngines, function(target, callback){
var req = new AjaxRequest();
req.onload = function(e){
if(req.status == "200"){
util.log("Submitted sitemapindex to " + target.name);
} else {
util.log("Sorry, there was a problem submitting your sitemapindex to " + target.name);
}
callback();
}
req.open("GET", "http://"+target.URL+"/ping?sitemap=" + siteURL + "/sitemap-index.xml", true);
req.send();
}, function(){
if(callback){
callback();
}
});
},
this._flush = function(callback) {
util.log('Flushing sitemap #' + this.sitemapCount + ' to disk');
var xml = [
'<?xml version="1.0" encoding="UTF-8"?>',
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
].join('');
this.pages.forEach(function(page) {
xml += [
'<url>',
'<loc>' + page.url + '</loc>',
'<changefreq>' + page.changeFreq + '</changefreq>',
'<priority>' + page.priority + '</priority>',
'</url>',
].join('');
});
xml += '</urlset>';
var filename = '/sitemap-';
if (this.sitemapCount < 10) {
filename += '0';
}
filename += this.sitemapCount + '.xml';
fs.writeFileSync(siteDistPath + filename, xml);
this.pages = [];
this.sitemapCount++;
}
};