From 14134fb4b0791661c7d3dd62dceb638597c1c779 Mon Sep 17 00:00:00 2001
From: Hannah Wolfe
Date: Sun, 5 Apr 2015 21:40:13 +0100
Subject: [PATCH] Add media:content support to RSS
refs #2263, #4888
- Adds media:content element to Ghost Rt pSS feeds containing the post cover image if one is available
- Removes the prepending of the image to the `` field
- Keeps the prepending of the image in ``
---
core/server/controllers/frontend.js | 40 ++++++++++++++------
core/test/functional/routes/frontend_test.js | 14 ++++---
core/test/utils/fixtures/data-generator.js | 1 +
3 files changed, 38 insertions(+), 17 deletions(-)
diff --git a/core/server/controllers/frontend.js b/core/server/controllers/frontend.js
index ab89fa4b43..6ec5f12eea 100644
--- a/core/server/controllers/frontend.js
+++ b/core/server/controllers/frontend.js
@@ -505,7 +505,8 @@ frontendControllers = {
site_url: siteUrl,
ttl: '60',
custom_namespaces: {
- content: 'http://purl.org/rss/1.0/modules/content/'
+ content: 'http://purl.org/rss/1.0/modules/content/',
+ media: 'http://search.yahoo.com/mrss/'
}
});
@@ -525,14 +526,11 @@ frontendControllers = {
url: config.urlFor('post', {post: post, permalinks: permalinks}, true),
date: post.published_at,
categories: _.pluck(post.tags, 'name'),
- author: post.author ? post.author.name : null
+ author: post.author ? post.author.name : null,
+ custom_elements: []
},
- htmlContent = cheerio.load(post.html, {decodeEntities: false});
-
- if (post.image) {
- htmlContent('p').first().before('');
- htmlContent('img').attr('alt', post.title);
- }
+ htmlContent = cheerio.load(post.html, {decodeEntities: false}),
+ image;
// convert relative resource urls to absolute
['href', 'src'].forEach(function (attributeName) {
@@ -572,13 +570,31 @@ frontendControllers = {
});
});
- item.custom_elements = [{
+ item.description = post.meta_description || downsize(htmlContent.html(), {words: 50});
+
+ if (post.image) {
+ image = config.urlFor('image', {image: post.image}, true);
+
+ // Add a media content tag
+ item.custom_elements.push({
+ 'media:content': {
+ _attr: {
+ url: image,
+ medium: 'image'
+ }
+ }
+ });
+
+ // Also add the image to the content, because not all readers support media:content
+ htmlContent('p').first().before('');
+ htmlContent('img').attr('alt', post.title);
+ }
+
+ item.custom_elements.push({
'content:encoded': {
_cdata: htmlContent.html()
}
- }];
-
- item.description = post.meta_description || downsize(htmlContent.html(), {words: 50});
+ });
feed.item(item);
});
diff --git a/core/test/functional/routes/frontend_test.js b/core/test/functional/routes/frontend_test.js
index dedd9aedb2..1188407985 100644
--- a/core/test/functional/routes/frontend_test.js
+++ b/core/test/functional/routes/frontend_test.js
@@ -443,7 +443,14 @@ describe('Frontend Routing', function () {
}).catch(done);
});
- it('should use meta_description where available', function (done) {
+ it('should use meta_description and image where available', function (done) {
+ var post1End = 'you think :)
]]>',
+ post3Title = '',
+ post3DescStart = 'testing\n\n' +
+ '').should.be.above(0);
done();
diff --git a/core/test/utils/fixtures/data-generator.js b/core/test/utils/fixtures/data-generator.js
index 3ff05dfec3..f17d9d4cb0 100644
--- a/core/test/utils/fixtures/data-generator.js
+++ b/core/test/utils/fixtures/data-generator.js
@@ -23,6 +23,7 @@ DataGenerator.Content = {
slug: "short-and-sweet",
markdown: "## testing\n\nmctesters\n\n- test\n- line\n- items",
html: "testing
\n\nmctesters
\n\n",
+ image: "http://placekitten.com/500/200",
meta_description: "test stuff",
published_at: new Date("2015-01-03")
},