-
Notifications
You must be signed in to change notification settings - Fork 7
/
sitemap.js
68 lines (55 loc) · 1.54 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
Object.filter = (obj, predicate) =>
Object.keys(obj)
.filter(key => predicate(obj[key]))
.reduce((res, key) => {
res[key] = obj[key]
return res
}, {})
import utils from './utilities.js'
export default class Sitemap {
constructor(settings) {
this.settings = settings
this.urls = []
this.images = [];
}
addURL(url) {
if (url['lastmod'] === null || url['lastmod'] === undefined) {
url['lastmod'] = '2099-12-31T00:00:00'
}
let urlLastMod = utils.toTimestamp(url['lastmod'])
if (this.shouldWarmup(urlLastMod) === false) {
return
}
let normalizeURL = utils.tryValidURL(url['loc'][0])
if (normalizeURL === false) {
return
}
this.urls[normalizeURL] = []
this.urls[normalizeURL].lastMod = urlLastMod
if (url['image:image']) {
url['image:image'].forEach(image => {
this.addImage(image['image:loc'][0])
})
}
}
addImage(image) {
if (this.settings.warmup_images === false) {
return
}
if (this.images.indexOf(image) === -1) {
this.images.push(image);
}
}
getURLs() {
return this.urls
}
getImages() {
return this.images
}
shouldWarmup(lastMod) {
if (this.settings.all === true) {
return true
}
return Math.round(Date.now() / 1000) - lastMod < this.settings.newer_than
}
}