Ghost/core/server/data/sitemap/index-generator.js
Hannah Wolfe 818085f18c Add XSL for Sitemaps
fixes #4555

- There's no easy way to declare an XSL with the node xml module, so I
  needed to move the declarations to both be strings
- Ideally the code to serve the XSL would also be inside the sitemap
  module, but I think we need to refactor a bit to get there easily
- Added the XSL from #4559, with minor amends to make the tables and urls
  display correctly
2014-12-04 09:38:09 +00:00

54 lines
1.4 KiB
JavaScript

var _ = require('lodash'),
xml = require('xml'),
moment = require('moment'),
config = require('../../config'),
utils = require('./utils'),
RESOURCES,
XMLNS_DECLS;
RESOURCES = ['pages', 'posts', 'authors', 'tags'];
XMLNS_DECLS = {
_attr: {
xmlns: 'http://www.sitemaps.org/schemas/sitemap/0.9'
}
};
function SiteMapIndexGenerator(opts) {
// Grab the other site map generators from the options
_.extend(this, _.pick(opts, RESOURCES));
}
_.extend(SiteMapIndexGenerator.prototype, {
getIndexXml: function () {
var urlElements = this.generateSiteMapUrlElements(),
data = {
// Concat the elements to the _attr declaration
sitemapindex: [XMLNS_DECLS].concat(urlElements)
};
// Return the xml
return utils.getDeclarations() + xml(data);
},
generateSiteMapUrlElements: function () {
var self = this;
return _.map(RESOURCES, function (resourceType) {
var url = config.urlFor({
relativeUrl: '/sitemap-' + resourceType + '.xml'
}, true),
lastModified = self[resourceType].lastModified;
return {
sitemap: [
{loc: url},
{lastmod: moment(lastModified).toISOString()}
]
};
});
}
});
module.exports = SiteMapIndexGenerator;