forked from apostrophecms/apostrophe-site-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
189 lines (179 loc) · 6.2 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
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
187
188
189
/* jshint node:true */
var async = require('async');
var _ = require('lodash');
var fs = require('fs');
var moment = require('moment');
module.exports = factory;
function factory(options, callback) {
return new Construct(options, callback);
}
function Construct(options, callback) {
var self = this;
// Add a bunch of methods to self here, then...
self._apos = options.apos;
self._pages = options.pages;
self._apos.on('tasks:register', function(taskGroups) {
taskGroups.apostrophe.siteMap = function(site, apos, argv, callback) {
var format = self.format = argv.format || 'xml';
var indent = self.indent = !!argv.indent;
var file = self.file = argv.file || '/dev/stdout';
var out = self.out = fs.openSync(file, 'w');
var home;
var criteria = {};
var today = self.today = moment().format('YYYY-MM-DD');
var req = self.req = self._apos.getTaskReq();
// General public, not admin
req.user.permissions = {};
var excludeTypes = self.excludeTypes = options.excludeTypes || [];
var dateTime = moment();
var date = dateTime.format('YYYY-MM-DD');
var time = dateTime.format('HH:mm:ss');
criteria.$or = [
{
publicationDate: { $lt: `${date}` },
published: true
},
{
publicationDate: { $eq:`${date}` },
publicationTime: { $lte: `${time}` },
published: true
},
{
publicationDate: null,
publicationTime: null,
published: true
}
];
criteria.slug = { $ne: '/search' };
self.output = output;
if (argv['exclude-types']) {
excludeTypes = excludeTypes.concat(argv['exclude-types'].split(','));
criteria.type = { $nin: excludeTypes };
}
if (format === 'xml') {
fs.writeSync(out, '<?xml version="1.0" encoding="UTF-8"?>\n');
fs.writeSync(out, '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n');
}
var host = site.hostName;
if (!host.match(/^https?:/)) {
host = 'http://' + host;
}
self.host = host;
return async.series({
home: function(callback) {
return apos.pages.findOne({ slug: '/' }, function(err, page) {
if (err) {
return callback(err);
}
if (!page) {
return callback('no homepage');
}
home = page;
return callback(null);
});
},
pages: function(callback) {
return self._pages.getDescendants(req, home, criteria, { orphan: null, depth: 20, fields: { slug: 1, path: 1, rank: 1, level: 1 } }, function(err, children) {
home.children = children;
return callback(err);
});
},
snippets: function(callback) {
return async.eachSeries(self._pages.types, function(type, callback) {
if (!_.find(type._modules || [], function(m) {
return m.name === 'snippets';
})) {
// Not derived from snippets
return setImmediate(callback);
}
// You can ignore either the index or the instance,
// same result
if (_.contains(excludeTypes, type.name)) {
return setImmediate(callback);
}
if (_.contains(excludeTypes, type._instance)) {
return setImmediate(callback);
}
// Paginate through 100 at a time to
// avoid slamming memory
var done = false;
var skip = 0;
return async.whilst(
function() { return !done; },
function(callback) {
return type.get(req, { published: true }, { permalink: true, withJoins: false, areas: false, skip: skip, limit: 100 }, function(err, results) {
_.each(results.snippets, function(snippet) {
if (!snippet.url) {
// This one has no page to be viewed on
return;
}
snippet.url = host + snippet.url;
// Results in a reasonable priority relative
// to regular pages
snippet.level = 3;
// Future events are interesting,
// past events are boring
if (snippet.startDate) {
if (snippet.startDate > today) {
snippet.level--;
} else {
snippet.level++;
}
}
output(snippet, true);
});
if (!results.snippets.length) {
done = true;
} else {
skip += results.snippets.length;
}
return callback(null);
});
}, callback);
}, callback);
},
custom: function(callback) {
return self.custom(site, apos, argv, callback);
}
}, function(err) {
if (err) {
return callback(err);
}
output(home);
if (format === 'xml') {
fs.writeSync(out, '</urlset>\n');
}
fs.closeSync(out);
return callback(null);
});
function output(page, trustUrl) {
if (format === 'text') {
if (indent) {
var i;
for (i = 0; (i < page.level); i++) {
fs.writeSync(out, ' ');
}
}
fs.writeSync(out, page.slug);
} else {
var url = (trustUrl && page.url) || (host + (site.prefix || '') + page.slug);
fs.writeSync(out, ' <url><priority>' + (1.0 - page.level / 10) + '</priority><changefreq>daily</changefreq><loc>' + url + '</loc></url>\n');
}
_.each(page.children, function(page) {
output(page);
});
}
};
});
self.custom = function(site, apos, argv, callback) {
return setImmediate(callback);
};
if (callback) {
// Invoke the callback. This must happen on next tick or later!
return process.nextTick(function() {
return callback(null);
});
}
}
// Export the constructor so others can subclass
factory.Construct = Construct;