0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Fix URL replacement in RSS feed mucking with content

closes #3983
- removed naive regex implementation
- added cheerio to project dependencies
- used cheerio to parse RSS content
- use attribute getter/setter to replace href/src attribute urls with the resolved version
This commit is contained in:
Mo Valipour 2014-09-12 15:40:34 +01:00 committed by valipour
parent bd163ada46
commit 98d0954e32
2 changed files with 14 additions and 14 deletions

View file

@ -14,6 +14,7 @@ var moment = require('moment'),
filters = require('../../server/filters'), filters = require('../../server/filters'),
template = require('../helpers/template'), template = require('../helpers/template'),
errors = require('../errors'), errors = require('../errors'),
cheerio = require('cheerio'),
frontendControllers, frontendControllers,
staticPostPermalink, staticPostPermalink,
@ -485,23 +486,21 @@ frontendControllers = {
categories: _.pluck(post.tags, 'name'), categories: _.pluck(post.tags, 'name'),
author: post.author ? post.author.name : null author: post.author ? post.author.name : null
}, },
content = post.html; htmlContent = cheerio.load(post.html, { decodeEntities: false });
//set img src to absolute url // convert relative resource urls to absolute
content = content.replace(/src=["|'|\s]?([\w\/\?\$\.\+\-;%:@&=,_]+)["|'|\s]?/gi, function (match, p1) { ['href', 'src'].forEach(function (attributeName) {
/*jslint unparam:true*/ htmlContent('[' + attributeName + ']').each(function (ix, el) {
p1 = url.resolve(siteUrl, p1); el = htmlContent(el);
return "src='" + p1 + "' ";
var attributeValue = el.attr(attributeName);
attributeValue = url.resolve(siteUrl, attributeValue);
el.attr(attributeName, attributeValue);
});
}); });
//set a href to absolute url item.description = htmlContent.html();
content = content.replace(/href=["|'|\s]?([\w\/\?\$\.\+\-;%:@&=,_]+)["|'|\s]?/gi, function (match, p1) {
/*jslint unparam:true*/
p1 = url.resolve(siteUrl, p1);
return "href='" + p1 + "' ";
});
item.description = content;
feed.item(item); feed.item(item);
}); });
}).then(function () { }).then(function () {

View file

@ -36,6 +36,7 @@
"body-parser": "1.6.3", "body-parser": "1.6.3",
"bookshelf": "0.7.6", "bookshelf": "0.7.6",
"busboy": "0.2.3", "busboy": "0.2.3",
"cheerio": "0.17.0",
"colors": "0.6.2", "colors": "0.6.2",
"compression": "^1.0.2", "compression": "^1.0.2",
"connect": "3.0.0-rc.1", "connect": "3.0.0-rc.1",