-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
87 lines (70 loc) · 2.09 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
'use strict';
var path = require('path');
var through = require('through2');
var isValid = require('is-valid-app');
var sitemap = require('handlebars-helper-sitemap');
var assign = require('assign-deep');
module.exports = function(options) {
return function(app) {
if (!isValid(app, 'assemble-sitemap')) return;
var files = [];
// register sitemap handlebars helpers
app.helpers(sitemap.helpers);
app.define('sitemap', function(collection, options) {
var dest = '';
var data = {};
if (Array.isArray(collection)) {
data.collections = collection;
collection = null;
}
if (typeof collection !== 'string') {
options = collection;
collection = null;
}
if (typeof options === 'string' || typeof options === 'function') {
dest = options;
options = {};
}
options = options || {};
collection = collection || options.collection;
dest = dest || options.dest;
if (options.collections) {
data.collections = options.collections;
}
var view = app.view('sitemap.xml', {
data: data,
contents: options.template || sitemap.template,
engine: 'hbs'
});
return through.obj(function(file, enc, next) {
if (file.isNull()) {
next(null, file);
return;
}
if (file.basename === 'sitemap.xml') {
next(null, file);
return;
}
files.push(file);
next(null, file);
}, function(cb) {
let data = assign({}, app.cache.data, app.data('sitemap'));
var file = files[0];
if (typeof collection === 'undefined') {
collection = data.collection || file.options.collection;
}
view.data.collection = collection;
view.base = file.base;
view.cwd = file.cwd;
view.dirname = file.dirname;
if (typeof dest === 'function') {
dest(view);
} else if (dest) {
view.dirname = path.join(file.dirname, dest);
}
this.push(view);
cb();
});
});
};
};