diff --git a/core/server/data/xml/sitemap/base-generator.js b/core/server/data/xml/sitemap/base-generator.js index e9fd3a9f1d..f53fda2c27 100644 --- a/core/server/data/xml/sitemap/base-generator.js +++ b/core/server/data/xml/sitemap/base-generator.js @@ -1,10 +1,11 @@ -var _ = require('lodash'), - xml = require('xml'), - moment = require('moment'), - config = require('../../../config'), - events = require('../../../events'), - utils = require('./utils'), - Promise = require('bluebird'), +var _ = require('lodash'), + xml = require('xml'), + moment = require('moment'), + config = require('../../../config'), + events = require('../../../events'), + utils = require('./utils'), + Promise = require('bluebird'), + path = require('path'), CHANGE_FREQ = 'weekly', XMLNS_DECLS; @@ -139,9 +140,11 @@ _.extend(BaseSiteMapGenerator.prototype, { createUrlNodeFromDatum: function (datum) { var url = this.getUrlForDatum(datum), - priority = this.getPriorityForDatum(datum); + priority = this.getPriorityForDatum(datum), + node, + imgNode; - return { + node = { url: [ {loc: url}, {lastmod: moment(this.getLastModifiedForDatum(datum)).toISOString()}, @@ -149,6 +152,48 @@ _.extend(BaseSiteMapGenerator.prototype, { {priority: priority} ] }; + + imgNode = this.createImageNodeFromDatum(datum); + + if (imgNode) { + node.url.push(imgNode); + } + + return node; + }, + + createImageNodeFromDatum: function (datum) { + // Check for cover first because user has cover but the rest only have image + var image = datum.cover || datum.image, + imageUrl, + imageEl; + + if (!image) { + return; + } + + // Grab the image url + imageUrl = this.getUrlForImage(image); + + // Verify the url structure + if (!this.validateImageUrl(imageUrl)) { + return; + } + + // Create the weird xml node syntax structure that is expected + imageEl = [ + {'image:loc': imageUrl}, + {'image:caption': path.basename(imageUrl)} + ]; + + // Return the node to be added to the url xml node + return { + 'image:image': imageEl + }; + }, + + validateImageUrl: function (imageUrl) { + return !!imageUrl; }, setSiteMapContent: function (content) { diff --git a/core/server/data/xml/sitemap/page-generator.js b/core/server/data/xml/sitemap/page-generator.js index f9f7a453f3..36914ded1b 100644 --- a/core/server/data/xml/sitemap/page-generator.js +++ b/core/server/data/xml/sitemap/page-generator.js @@ -1,20 +1,15 @@ var _ = require('lodash'), - path = require('path'), api = require('../../../api'), config = require('../../../config'), BaseMapGenerator = require('./base-generator'); // A class responsible for generating a sitemap from posts and keeping it updated function PageMapGenerator(opts) { - _.extend(this, _.defaults(opts || {}, PageMapGenerator.Defaults)); + _.extend(this, opts); BaseMapGenerator.apply(this, arguments); } -PageMapGenerator.Defaults = { - // TODO? -}; - // Inherit from the base generator class _.extend(PageMapGenerator.prototype, BaseMapGenerator.prototype); @@ -54,29 +49,6 @@ _.extend(PageMapGenerator.prototype, { getPriorityForDatum: function (post) { // TODO: We could influence this with priority or meta information return post && post.name === 'home' ? 1.0 : 0.8; - }, - - createUrlNodeFromDatum: function (datum) { - var orig = BaseMapGenerator.prototype.createUrlNodeFromDatum.apply(this, arguments), - imageUrl, - imageEl; - - // Check for image and add it - if (datum.image) { - // Grab the image url - imageUrl = this.getUrlForImage(datum.image); - // Create the weird xml node syntax structure that is expected - imageEl = [ - {'image:loc': imageUrl}, - {'image:caption': path.basename(imageUrl)} - ]; - // Add the node to the url xml node - orig.url.push({ - 'image:image': imageEl - }); - } - - return orig; } }); diff --git a/core/server/data/xml/sitemap/post-generator.js b/core/server/data/xml/sitemap/post-generator.js index e4ae0f1555..6d81caed2f 100644 --- a/core/server/data/xml/sitemap/post-generator.js +++ b/core/server/data/xml/sitemap/post-generator.js @@ -1,20 +1,15 @@ var _ = require('lodash'), - path = require('path'), api = require('../../../api'), config = require('../../../config'), BaseMapGenerator = require('./base-generator'); // A class responsible for generating a sitemap from posts and keeping it updated function PostMapGenerator(opts) { - _.extend(this, _.defaults(opts || {}, PostMapGenerator.Defaults)); + _.extend(this, opts); BaseMapGenerator.apply(this, arguments); } -PostMapGenerator.Defaults = { - // TODO? -}; - // Inherit from the base generator class _.extend(PostMapGenerator.prototype, BaseMapGenerator.prototype); @@ -46,29 +41,6 @@ _.extend(PostMapGenerator.prototype, { getPriorityForDatum: function (post) { // give a slightly higher priority to featured posts return post.featured ? 0.9 : 0.8; - }, - - createUrlNodeFromDatum: function (datum) { - var orig = BaseMapGenerator.prototype.createUrlNodeFromDatum.apply(this, arguments), - imageUrl, - imageEl; - - // Check for image and add it - if (datum.image) { - // Grab the image url - imageUrl = this.getUrlForImage(datum.image); - // Create the weird xml node syntax structure that is expected - imageEl = [ - {'image:loc': imageUrl}, - {'image:caption': path.basename(imageUrl)} - ]; - // Add the node to the url xml node - orig.url.push({ - 'image:image': imageEl - }); - } - - return orig; } }); diff --git a/core/server/data/xml/sitemap/tag-generator.js b/core/server/data/xml/sitemap/tag-generator.js index 1315578fe2..cbba25d910 100644 --- a/core/server/data/xml/sitemap/tag-generator.js +++ b/core/server/data/xml/sitemap/tag-generator.js @@ -5,15 +5,11 @@ var _ = require('lodash'), // A class responsible for generating a sitemap from posts and keeping it updated function TagsMapGenerator(opts) { - _.extend(this, _.defaults(opts || {}, TagsMapGenerator.Defaults)); + _.extend(this, opts); BaseMapGenerator.apply(this, arguments); } -TagsMapGenerator.Defaults = { - // TODO? -}; - // Inherit from the base generator class _.extend(TagsMapGenerator.prototype, BaseMapGenerator.prototype); diff --git a/core/server/data/xml/sitemap/user-generator.js b/core/server/data/xml/sitemap/user-generator.js index 008cd32297..b38c273b5f 100644 --- a/core/server/data/xml/sitemap/user-generator.js +++ b/core/server/data/xml/sitemap/user-generator.js @@ -1,5 +1,4 @@ var _ = require('lodash'), - path = require('path'), api = require('../../../api'), config = require('../../../config'), validator = require('validator'), @@ -7,15 +6,11 @@ var _ = require('lodash'), // A class responsible for generating a sitemap from posts and keeping it updated function UserMapGenerator(opts) { - _.extend(this, _.defaults(opts || {}, UserMapGenerator.Defaults)); + _.extend(this, opts); BaseMapGenerator.apply(this, arguments); } -UserMapGenerator.Defaults = { - // TODO? -}; - // Inherit from the base generator class _.extend(UserMapGenerator.prototype, BaseMapGenerator.prototype); @@ -47,30 +42,9 @@ _.extend(UserMapGenerator.prototype, { return 0.6; }, - createUrlNodeFromDatum: function (datum) { - var orig = BaseMapGenerator.prototype.createUrlNodeFromDatum.apply(this, arguments), - imageUrl, - imageEl; - - // Check for image and add it - if (datum.cover) { - // Grab the image url - imageUrl = this.getUrlForImage(datum.cover); - imageUrl = imageUrl.substring(0, 2) === '//' ? 'http:' + imageUrl : imageUrl; - if (validator.isURL(imageUrl, {protocols: ['http', 'https'], require_protocol: true})) { - // Create the weird xml node syntax structure that is expected - imageEl = [ - {'image:loc': imageUrl}, - {'image:caption': path.basename(imageUrl)} - ]; - // Add the node to the url xml node - orig.url.push({ - 'image:image': imageEl - }); - } - } - - return orig; + validateImageUrl: function (imageUrl) { + return imageUrl && + validator.isURL(imageUrl, {protocols: ['http', 'https'], require_protocol: true}); } }); diff --git a/core/test/unit/sitemap_spec.js b/core/test/unit/sitemap_spec.js index 5fa92deaec..8f2c4e3cfe 100644 --- a/core/test/unit/sitemap_spec.js +++ b/core/test/unit/sitemap_spec.js @@ -472,6 +472,19 @@ describe('Sitemap', function () { generator.getPriorityForDatum({}).should.equal(0.8); }); + it('adds an image:image element if page has an image', function () { + var generator = new PostGenerator(), + urlNode = generator.createUrlNodeFromDatum(_.extend(makeFakeDatum(100), { + image: 'page-100.jpg' + })), + hasImage; + + hasImage = _.any(urlNode.url, function (node) { + return !_.isUndefined(node['image:image']); + }); + + hasImage.should.equal(true); + }); }); describe('TagGenerator', function () { @@ -480,6 +493,20 @@ describe('Sitemap', function () { generator.getPriorityForDatum({}).should.equal(0.6); }); + + it('adds an image:image element if tag has an image', function () { + var generator = new PostGenerator(), + urlNode = generator.createUrlNodeFromDatum(_.extend(makeFakeDatum(100), { + image: 'tag-100.jpg' + })), + hasImage; + + hasImage = _.any(urlNode.url, function (node) { + return !_.isUndefined(node['image:image']); + }); + + hasImage.should.equal(true); + }); }); describe('UserGenerator', function () { @@ -488,6 +515,20 @@ describe('Sitemap', function () { generator.getPriorityForDatum({}).should.equal(0.6); }); + + it('adds an image:image element if user has a cover image', function () { + var generator = new PostGenerator(), + urlNode = generator.createUrlNodeFromDatum(_.extend(makeFakeDatum(100), { + cover: 'user-100.jpg' + })), + hasImage; + + hasImage = _.any(urlNode.url, function (node) { + return !_.isUndefined(node['image:image']); + }); + + hasImage.should.equal(true); + }); }); }); });