Limit excerpts to the first paragraph of a post on the Ghost blogging platform
By default, the excerpt helper function on Ghost pulls the first 50 words of a post. For this site, I wanted to revise it so that excerpts would only contain text from the first paragraph.
By revising it, bits of code that I include near the top of the article aren't pulled into the excerpt. I'm doing something similar to generate the meta description tags too, at least until SEO is addressed in the next version of Ghost.
Below is the modified helper function I placed in the /core/server/helpers/index.js
file:
ghost.registerThemeHelper('excerpt', function (options) {
var truncateOptions = (options || {}).hash || {},
excerpt;
truncateOptions = _.pick(truncateOptions, ['words', 'characters']);
excerpt = this.html
.replace(/(<([^>]+)>)/ig,"")
.replace(/(\r\n.*|\n.*|\r.*)/gm,"")
.replace(/ +(?= )/g,"")
.replace(/\.$/, '');
if (!truncateOptions.words && !truncateOptions.characters) {
truncateOptions.words = 50;
}
return new hbs.handlebars.SafeString(
downsize(excerpt, truncateOptions)
);
});
If the first paragraph exceeds the 50 words, it will still trim it down.