blog/src/posts/2013-11-10-updating-ghost-t...

4.0 KiB

layout title date tags summary
post.html Updating Ghost to use Truncatise 2013-11-10 nodejs node npm javascript truncatise ghost Today I finally got around to modifying my <a href='https://ghost.org/' target='_blank'>Ghost</a> blog to make use of my new Node Module <a href='//blog.marcusnoble.co.uk/publishing-my-first-npm-module/'>Truncatise</a>.<br/><br/>This didn't go as smoothly as I hoped.

Today I finally got around to modifying my Ghost blog to make use of my new Node Module Truncatise.

This didn't go as smoothly as I hoped.

First, I discovered a bug in my module. It was incorrectly handling the combination of <p> tags with double newlines when truncating to paragraphs.

To resolve this I needed to determine whether or not to use double newline to indicate a paragraph. This was done by ignoring any newlines between paragraph tags and better regular expression matching.

I also noticed an issue with the suffix, e.g. …, when used with <p> tags, it was rending after the and of the tag and thus display on a new line. Whoops! Not what is expected. A quick little replace when not stripping HTML solved this.

So, finally I published version 0.0.2 of Truncatise to the NPM repository (as well as GitHub).

As I was now happy that the issues were resolved, I made progress on modifying the Ghost helper source code to use Truncatise instead of downsize.

Original:


coreHelpers.excerpt = function (options) {
    var truncateOptions = (options || {}).hash || {},
        excerpt;

    truncateOptions = _.pick(truncateOptions, ['words', 'characters']);

    /*jslint regexp:true */
    excerpt = String(this.html).replace(/<\/?[^>]+>/gi, '');
    excerpt = excerpt.replace(/(\r\n|\n|\r)+/gm, ' ');
    /*jslint regexp:false */

    if (!truncateOptions.words && !truncateOptions.characters) {
        truncateOptions.words = 50;
    }

    return new hbs.handlebars.SafeString(
        downsize(excerpt, truncateOptions)
    );
};

My Version:


coreHelpers.excerpt = function (options) {
    var truncateOptions = (options || {}).hash ||  {TruncateLength: 2, TruncateBy : "paragraphs", StripHTML : false, Suffix : '...'},
        excerpt;

    truncateOptions = _.pick(truncateOptions, ['TruncateBy', 'TruncateLength', 'StripHTML', 'Strict', 'Suffix']);

    excerpt = String(this.html);

    //Set default values
    if (!truncateOptions.TruncateLength) {
        truncateOptions.TruncateLength = 2;
    }
    if (!truncateOptions.TruncateBy) {
        truncateOptions.TruncateBy = "paragraphs";
    }
    if (!truncateOptions.StripHTML) {
        truncateOptions.StripHTML = false;
    }
    if (!truncateOptions.Suffix) {
        truncateOptions.Suffix = "…";
    }

    return new hbs.handlebars.SafeString(
        truncatise(excerpt, truncateOptions)
    );
};

My changes have been pushed to my fork of Ghost if anyone wishes to make use of it.

On Updating Ghost

I got into a bit of a mess when trying to update my copy of Ghost with the latest changes, mainly due to my carelessness. So I don't get into the same situation in the future, and to prevent others making my mistakes, here are my little tips to bare in mind:

  1. Back up the ./content/data/ directory before doing anything!
  2. If needed also backup your ./content/themes/ directory.
  3. Copy across the new ./core, packages.json, index.js and Gruntfile.js
  4. npm install -g grunt-cli
  5. npm install --production
  6. grunt init prod

If you get errors relating to sqlite3…

Run npm install sqlite3 --build-from-source=sqlite3

Update:

I have since moved away from Ghost and now using Jekyll