Ghost Blog and custom URL Shortening

It's been 2 years and Ghost Blogging Platform is doing well and the downloads are going up. Kudos to the team.

While the platform is doing good, we1 are miles from reaching the point where we have apps, plugins and smooth migrations/ customisation etc. And in this blog I would mention one such thing which I personally like - URL Shortener(s); specially when we share the links over social media. I have had discussions with some people on Ghost Slack on it, and thought why not do some dirty coding ;)

Oh yeah, but why dirty coding?
The Ghost Platform is written in NodeJS and I was trying to integrate different libraries like node-bitlyapi with core URL.js helper. But, due to syn-async issues and no know-how for promises, I moved to some dirty work. While I am not a developer, please take this with a pinch of salt and constructive criticism is more than welcome. Also, am all ears to learn :)

While this method is perfectly valid and working, I on the other hand have made my own URL-Shortener in nodeJS with help from Lachlan Chant article on URL Shortener with Node.JS. So, I have better control on analytics, custom links and database of my URL(s). More details available @ new blog on Personal URL Shortener

Setup Requirements

  1. A weblog running (in my case, Ghost Blog)
  2. Bitly App Account (signup here) (Or, you can use other shortening services, and their API accordingly)
  3. Linux Box (for a short shell script). My linux box is running Ubuntu, nginx, ghost blog @ Digital Ocean.

Steps I performed,

  1. I wrote a bash script to perform the URL shortening task and write the results in a file.
  2. I used jquery.ajax() to load the contents of this file (array format) in the webpage with some tricks to div id elements. I tried but could not perform with key:value pair :/
  3. I included this script in the webpage for post.hbs as <script type="text/javascript" src="{{asset "js/shorten.js"}}"></script>

Let's see the code samples now,

  • shorten.sh bash script
    This is the main script that takes the long URL(s) and converts them into shortURLs with bit.ly. I could have used window.location or other objects but that's not trustworthy so, I moved to extracting the URL(s) or slugs from database. Then call bit.ly to shorten them.
    Remember: The slugs are relative and you need to append your domain path to make them complete URL(s) before sending to bit.ly.

While this method is perfectly valid and working, I on the other hand have made my own URL-Shortener in nodeJS with help from Lachlan Chant article on URL Shortener with Node.JS. So, I have better control on analytics, custom links and database of my URL(s). More details available @ new blog on Personal URL Shortener

Now, the list of URL(s) will look like this (I'm using branded domain else, it will be bit.ly/***)

  • shorten.js script
    This is the javascript that will use ajax() to get the file with list of URL(s) generate above, and then use them in the individual post respectively. First it calls the URL for the file mentioned above, and then perform some string splits to array and finally shows the result in the beginning (as I used .before) of the class="post-date" with some fancy stuff.

Note: You can see below the post Title "*Link: qb.is/*** xfu.io/***" for the final format.

  • excerpt from post.hbs
    Finally, all this is included as the script in every page of post, but not default or header or pages, tags etc.
    <script type="text/javascript" src="{{asset "js/shorten.js"}}"></script>

Voila! Execute the bash script, and then all your posts will show short URL(s) as you formatted. You can make it a cron job, attach it with publish or further improve it etc. Fell free to comment, and revise, or make it more efficient.

Cheers & be safe.

PS: I am not a developer, and I know you can do it better, but hey, for me - it works!

  1. I use Ghost, and would consider myself part of family :)


❮❮ Target _Blank - The Infamous Issue

Ghost Blog Image Alignment ❯❯


Rishi Narang

I'm a hacker. I find inventive/ intuitive solutions to problems. I write open-source code, help out people where I can - particularly if it involves a clever technical solution. I'm based in France.