-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
172 lines (145 loc) · 4.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
'use strict';
var fs = require('fs');
var url = require('url');
var path = require('path');
var get = require('get-value');
var isObject = require('isobject');
var assign = require('assign-deep');
var toHtml = require('html-tag');
var moment = require('moment');
/**
* Expose sitemap helpers and templates
*/
var sitemap = module.exports;
var helpers = sitemap.helpers = {};
var templates = path.join.bind(path, __dirname, 'templates');
/**
* Helpers
*/
helpers._collection = require('helper-collection');
helpers._urlset = function(items, options) {
if (Array.isArray(items)) {
return items;
}
var items = get(options, 'data.root.items')
|| get(options, 'data.root.sitemap.items')
|| get(options, 'data.root.sitemap.urlset');
if (!Array.isArray(items)) {
throw new TypeError('expected sitemap.urlset to be an array');
}
return items;
};
helpers._loc = function(item, data, options) {
if (typeof data.url !== 'string') {
throw new TypeError('expected url to be a string');
}
if (typeof item.relative !== 'string') {
throw new TypeError('expected item.relative to be a string');
}
var rel = destRelative.call(this, item, options);
var res = url.resolve(data.url, rel);
return safeString(toHtml('loc', res));
};
helpers._lastmod = function(item, data) {
var date = data.lastModified || data.lastmod || item.stat && item.stat.mtime;
var res = moment(date || new Date()).format('YYYY-MM-DD');
return safeString(toHtml('lastmod', res));
};
helpers._changefreq = function(item, data) {
return safeString(toHtml('changefreq', String(data.changefreq || 'weekly')));
};
helpers._priority = function(item, data) {
return safeString(toHtml('priority', String(data.priority || '0.5')));
};
helpers._url = function(item, options) {
var data = assign({}, globalData(options), itemData(item));
var text = '\n';
text += ' ' + helpers._loc.call(this, item, data, options) + '\n';
text += ' ' + helpers._lastmod.call(this, item, data, options) + '\n';
text += ' ' + helpers._changefreq.call(this, item, data, options) + '\n';
text += ' ' + helpers._priority.call(this, item, data, options) + '\n';
text += ' ';
return safeString(toHtml('url', text));
};
helpers._filterItems = function(items) {
return items.filter(function(item) {
return item.stem !== 'sitemap' && item.data.sitemap !== false;
});
};
helpers._greaterThan = function(a, b) {
return a > b;
};
helpers.throw = function(msg) {
var err = new Error(msg);
err.path = __filename;
throw err;
};
/**
* Templates
*/
Object.defineProperty(sitemap, 'template', {
set: function() {
throw new Error('sitemap.template is a getter and cannot be defined');
},
get: function() {
return fs.readFileSync(templates('template.hbs'));
}
});
Object.defineProperty(sitemap, 'layout', {
set: function() {
throw new Error('sitemap.layout is a getter and cannot be defined');
},
get: function() {
return fs.readFileSync(templates('layout.hbs'));
}
});
Object.defineProperty(sitemap, 'partial', {
set: function() {
throw new Error('sitemap.partial is a getter and cannot be defined');
},
get: function() {
return fs.readFileSync(templates('partial.hbs'));
}
});
Object.defineProperty(sitemap, 'page', {
set: function() {
throw new Error('sitemap.page is a getter and cannot be defined');
},
get: function() {
return fs.readFileSync(templates('page.hbs'));
}
});
/**
* Utils
*/
function destRelative(item, options) {
if (typeof item.relative !== 'string') {
throw new TypeError('expected item.relative to be a string');
}
var context = this || options.data.root;
var dest = get(context, 'app.options.dest');
var dataDest = get(item, 'data.dest');
var folder = '';
if (dest && dataDest) {
folder = path.relative(dest, dataDest);
}
return path.join(folder, item.relative);
}
function globalData(options) {
var sitemap = get(options, 'data.root.sitemap');
var data = get(options, 'data.root');
return assign({}, data, sitemap);
}
function itemData(item) {
var sitemap = get(item, 'data.sitemap');
return assign({}, item.data, sitemap);
}
function safeString(str) {
function SafeString(string) {
this.string = string;
}
SafeString.prototype.toString = SafeString.prototype.toHTML = function() {
return '' + this.string;
};
return new SafeString(str);
}