From 7acd165d7a851f1c5b72b0f087eee625abc9b601 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Sun, 7 Jul 2013 18:54:50 +0100 Subject: [PATCH] Bug fix - unable to view single page - The fancyFirstChar plugin expected to always get an array of posts, and therefore broke on the single post pages - Changed the plugin to cope with single objects as well as arrays --- content/plugins/fancyFirstChar.js | 47 ++++++++++++++++++------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/content/plugins/fancyFirstChar.js b/content/plugins/fancyFirstChar.js index 157fc71054..730111ff00 100644 --- a/content/plugins/fancyFirstChar.js +++ b/content/plugins/fancyFirstChar.js @@ -1,29 +1,36 @@ +var _ = require('underscore'); + var fancyFirstChar; +function fancify(originalContent) { + var newContent, + firstCharIndex = 0; + + if (originalContent.substr(0, 1) === '<') { + firstCharIndex = originalContent.indexOf('>') + 1; + } + + newContent = originalContent.substr(0, firstCharIndex); + newContent += ''; + newContent += originalContent.substr(firstCharIndex, 1); + newContent += ''; + newContent += originalContent.substr(firstCharIndex + 1, originalContent.length - firstCharIndex - 1); + + return newContent; +} + + fancyFirstChar = { init: function (ghost) { ghost.registerFilter('prePostsRender', function (posts) { - var post, - originalContent, - newContent, - firstCharIndex = 0; - - for (post in posts) { - if (posts.hasOwnProperty(post)) { - originalContent = posts[post].content_html; - if (originalContent.substr(0, 1) === '<') { - firstCharIndex = originalContent.indexOf('>') + 1; - } - - newContent = originalContent.substr(0, firstCharIndex); - newContent += ''; - newContent += originalContent.substr(firstCharIndex, 1); - newContent += ''; - newContent += originalContent.substr(firstCharIndex + 1, originalContent.length - firstCharIndex - 1); - - posts[post].content_html = newContent; - } + if (_.isArray(posts)) { + _.each(posts, function (post) { + post.content_html = fancify(post.content_html); + }); + } else if (posts.hasOwnProperty('content_html')) { + posts.content_html = fancify(posts.content_html); } + return posts; }); },