truncate long opengraph description to 250 chars in mobile and stream view

This commit is contained in:
Faldrian 2014-11-09 15:55:35 +01:00
parent 850c98cbf7
commit 29f55f42b6
6 changed files with 49 additions and 7 deletions

View file

@ -3,10 +3,10 @@
(function(){ (function(){
app.helpers.openGraph = { app.helpers.openGraph = {
html : function (open_graph_cache) { html : function (open_graph_cache) {
if (!open_graph_cache) { return "" } if (!open_graph_cache) { return ""; }
return '<img src="' + open_graph_cache.image + '" />' return '<img src="' + open_graph_cache.image + '" />';
} },
} };
})(); })();
// @license-end // @license-end

View file

@ -0,0 +1,10 @@
(function() {
app.helpers.truncate = function(passedString, length) {
if (passedString.length > length) {
var lastBlank = passedString.lastIndexOf(' ', length);
var trimstring = passedString.substring(0, Math.min(length, lastBlank));
return new Handlebars.SafeString(trimstring + " ...");
}
return new Handlebars.SafeString(passedString);
};
})();

View file

@ -116,7 +116,25 @@ app.views.OEmbed = app.views.Base.extend({
}); });
app.views.OpenGraph = app.views.Base.extend({ app.views.OpenGraph = app.views.Base.extend({
templateName : "opengraph" templateName : "opengraph",
initialize: function() {
this.truncateDescription();
},
truncateDescription: function() {
// truncate opengraph description to 250 for stream view
if(this.model.has('open_graph_cache')) {
var ogdesc = this.model.get('open_graph_cache');
ogdesc.description = app.helpers.truncate(ogdesc.description, 250);
}
}
});
app.views.SPVOpenGraph = app.views.OpenGraph.extend({
truncateDescription: function () {
// override with nothing
}
}); });
// @license-end // @license-end

View file

@ -16,7 +16,7 @@ app.views.SinglePostContent = app.views.Base.extend({
initialize : function() { initialize : function() {
this.singlePostActionsView = new app.views.SinglePostActions({model: this.model}); this.singlePostActionsView = new app.views.SinglePostActions({model: this.model});
this.oEmbedView = new app.views.OEmbed({model : this.model}); this.oEmbedView = new app.views.OEmbed({model : this.model});
this.openGraphView = new app.views.OpenGraph({model : this.model}); this.openGraphView = new app.views.SPVOpenGraph({model : this.model});
this.postContentView = new app.views.ExpandedStatusMessage({model: this.model}); this.postContentView = new app.views.ExpandedStatusMessage({model: this.model});
this.pollView = new app.views.Poll({ model: this.model }); this.pollView = new app.views.Poll({ model: this.model });
}, },

View file

@ -45,7 +45,7 @@ module OpenGraphHelper
" <div>" + " <div>" +
" <img src=\"#{cache.image}\" />" + " <img src=\"#{cache.image}\" />" +
" <strong>#{cache.title}</strong>" + " <strong>#{cache.title}</strong>" +
" <p>#{cache.description}</p>" + " <p>#{truncate(cache.description, length: 250, separator: ' ')}</p>" +
" </div>" + " </div>" +
"</a>" "</a>"
end end

View file

@ -42,6 +42,14 @@ describe("app.views.StreamPost", function(){
"ob_type": "article" "ob_type": "article"
}; };
var open_graph_cache_extralong = {
"url": "http://example.com/articles/123",
"title": "Example title",
"description": Array(62).join("Test description"), // 992 chars
"image": "http://example.com/thumb.jpg",
"ob_type": "article"
};
beforeEach(function(){ beforeEach(function(){
loginAs({name: "alice", avatar : {small : "http://avatar.com/photo.jpg"}}); loginAs({name: "alice", avatar : {small : "http://avatar.com/photo.jpg"}});
@ -114,6 +122,12 @@ describe("app.views.StreamPost", function(){
var view = new app.views.StreamPost({model : this.statusMessage}).render(); var view = new app.views.StreamPost({model : this.statusMessage}).render();
expect(view.$el.html()).not.toContain(open_graph_cache.title) expect(view.$el.html()).not.toContain(open_graph_cache.title)
}) })
it("truncates long opengraph descriptions in stream view to be 250 chars or less", function() {
this.statusMessage.set({"open_graph_cache" : open_graph_cache_extralong});
var view = new app.views.StreamPost({model : this.statusMessage}).render();
expect(view.$el.find('.og-description').html().length).toBeLessThan(251);
});
}) })
context("user not signed in", function(){ context("user not signed in", function(){