Skip to content

Commit

Permalink
Add XSL for Sitemaps
Browse files Browse the repository at this point in the history
fixes TryGhost#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 TryGhost#4559, with minor amends to make the tables and urls
  display correctly
  • Loading branch information
ErisDS committed Dec 4, 2014
1 parent 319887c commit 818085f
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 158 deletions.
138 changes: 0 additions & 138 deletions core/client/html/sitemap-xsl.html

This file was deleted.

3 changes: 3 additions & 0 deletions core/server/config/url.js
Expand Up @@ -148,6 +148,9 @@ function urlFor(context, data, absolute) {
}

return urlPath;
} else if (context === 'sitemap-xsl') {
absolute = true;
urlPath = '/sitemap.xsl';
}
// other objects are recognised but not yet supported
} else if (_.isString(context) && _.indexOf(_.keys(knownPaths), context) !== -1) {
Expand Down
16 changes: 7 additions & 9 deletions core/server/data/sitemap/base-generator.js
@@ -1,9 +1,9 @@

var _ = require('lodash'),
xml = require('xml'),
moment = require('moment'),
api = require('../../api'),
config = require('../../config'),
var _ = require('lodash'),
xml = require('xml'),
moment = require('moment'),
api = require('../../api'),
config = require('../../config'),
utils = require('./utils'),
Promise = require('bluebird'),
CHANGE_FREQ = 'weekly',
XMLNS_DECLS;
Expand Down Expand Up @@ -87,9 +87,7 @@ _.extend(BaseSiteMapGenerator.prototype, {
};

// Return the xml
return xml(data, {
declaration: true
});
return utils.getDeclarations() + xml(data);
},

updateXmlFromNodes: function (urlElements) {
Expand Down
8 changes: 4 additions & 4 deletions core/server/data/sitemap/handler.js
@@ -1,13 +1,13 @@
var _ = require('lodash'),
utils = require('../utils'),
var _ = require('lodash'),
utils = require('../../utils'),
sitemap = require('./index');

// Responsible for handling requests for sitemap files
module.exports = function (blogApp) {
var resourceTypes = ['posts', 'authors', 'tags', 'pages'],
verifyResourceType = function (req, res, next) {
if (!_.contains(resourceTypes, req.param('resource'))) {
return res.send(404);
return res.sendStatus(404);
}

next();
Expand All @@ -19,7 +19,7 @@ module.exports = function (blogApp) {
// Redirect normal sitemap.xml requests to sitemap-index.xml
blogApp.get('/sitemap.xml', function (req, res) {
res.set({'Cache-Control': 'public, max-age=' + utils.ONE_YEAR_S});
res.redirect(301, '/sitemap-index.xml');
res.redirect(301, req.baseUrl + '/sitemap-index.xml');
});

blogApp.get('/sitemap-index.xml', function (req, res) {
Expand Down
13 changes: 6 additions & 7 deletions core/server/data/sitemap/index-generator.js
@@ -1,7 +1,8 @@
var _ = require('lodash'),
xml = require('xml'),
moment = require('moment'),
config = require('../../config'),
var _ = require('lodash'),
xml = require('xml'),
moment = require('moment'),
config = require('../../config'),
utils = require('./utils'),
RESOURCES,
XMLNS_DECLS;

Expand All @@ -27,9 +28,7 @@ _.extend(SiteMapIndexGenerator.prototype, {
};

// Return the xml
return xml(data, {
declaration: true
});
return utils.getDeclarations() + xml(data);
},

generateSiteMapUrlElements: function () {
Expand Down
13 changes: 13 additions & 0 deletions core/server/data/sitemap/utils.js
@@ -0,0 +1,13 @@
var config = require('../../config'),
utils;

utils = {
getDeclarations: function () {
var baseUrl = config.urlFor('sitemap-xsl');
baseUrl = baseUrl.replace(/^(http:|https:)/, '');
return '<?xml version="1.0" encoding="UTF-8"?>' +
'<?xml-stylesheet type="text/xsl" href="' + baseUrl + 'sitemap.xsl"?>';
}
};

module.exports = utils;
1 change: 1 addition & 0 deletions core/server/middleware/index.js
Expand Up @@ -264,6 +264,7 @@ setupMiddleware = function (blogAppInstance, adminApp) {

// Favicon
blogApp.use(serveSharedFile('favicon.ico', 'image/x-icon', utils.ONE_DAY_S));
blogApp.use(serveSharedFile('sitemap.xsl', 'text/xsl', utils.ONE_DAY_S));

// Static assets
blogApp.use('/shared', express['static'](path.join(corePath, '/shared'), {maxAge: utils.ONE_HOUR_MS}));
Expand Down

0 comments on commit 818085f

Please sign in to comment.