-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (81 loc) · 2.83 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
'use strict';
const _ = require('lodash'),
handlebars = require('handlebars'),
express = require('express'),
streamPages = require('./lib/stream-pages'),
Filters = require('./lib/filters'),
Transforms = require('./lib/transforms'),
DEFAULT_XML_PRELUDE = require('./lib/constants').DEFAULT_XML_PRELUDE,
DEFAULT_XML_POSTLUDE = require('./lib/constants').DEFAULT_XML_POSTLUDE;
/**
* Generate a standard text sitemap middleware for Clay. The page URL of each page is included.
* Only published, public pages are included.
* @param {object} amphora
* @return {string}
*/
function standardText(amphora) {
const filters = Filters(amphora),
transforms = Transforms(amphora);
return (req, res) => {
res.type('text');
streamPages(amphora, res.locals.site.prefix)
.pipe(filters.published())
.pipe(filters.public())
.pipe(transforms.pages.toText(res.locals))
.pipe(res);
};
}
/**
* Generate a standard XML sitemap middleware for Clay. This includes only published, public pages.
* The "sitemap" templates of all the components on each page are rendered and included in
* the sitemap.
* @param {object} amphora
* @param {object} opts
* @param {string} opts.prelude - XML to appear before any <url> object
* @param {string} opts.postlude - XML to appear after last <url> object
* @param {object} opts.engines - Multiplex engines configuration.
* @return {function}
*/
function standardXML(amphora, opts) {
const filters = Filters(amphora),
transforms = Transforms(amphora);
opts = opts || {};
_.defaults(opts, {
prelude: DEFAULT_XML_PRELUDE,
postlude: DEFAULT_XML_POSTLUDE,
engines: {handlebars: handlebars}
});
return (req, res) => {
res.type('xml');
streamPages(amphora, res.locals.site.prefix)
.pipe(filters.published())
.pipe(filters.public())
.pipe(transforms.pages.compose(res.locals))
.pipe(transforms.pages.toXML(res.locals, opts.engines))
.pipe(transforms.strings.addBookends(opts.prelude, opts.postlude))
.pipe(res);
};
}
function middleware(amphora, xmlOpts) {
return express.Router()
.get('/sitemap.txt', standardText(amphora))
.get('/sitemap.xml', standardXML(amphora, xmlOpts));
}
/**
* Binds amphora to each method as necessary so devs don't
* have to keep passing it around on the outside.
* @param {object} amphora
* @return {object} - This lib, with amphora bound to all methods that use it
*/
module.exports = function (amphora, db) {
return {
middleware: middleware.bind(this, amphora),
standardText: standardText.bind(this, amphora),
standardXML: standardXML.bind(this, amphora),
streamPages: streamPages.bind(this, amphora, db),
filters: Filters(amphora, db),
transforms: Transforms(amphora, db),
defaultXmlPrelude: DEFAULT_XML_PRELUDE,
defaultXmlPostlude: DEFAULT_XML_POSTLUDE
};
};