Ghost/content/plugins/fancyFirstChar.js
Hannah Wolfe 7acd165d7a 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
2013-07-07 18:54:50 +01:00

41 lines
1.1 KiB
JavaScript

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 += '<span class="fancyChar">';
newContent += originalContent.substr(firstCharIndex, 1);
newContent += '</span>';
newContent += originalContent.substr(firstCharIndex + 1, originalContent.length - firstCharIndex - 1);
return newContent;
}
fancyFirstChar = {
init: function (ghost) {
ghost.registerFilter('prePostsRender', function (posts) {
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;
});
},
activate: function () {},
deactivate: function () {}
};
module.exports = fancyFirstChar;