Если вам есть, что сказать сообществу профессионалов ИБ и ИТ – заведите здесь свой блог

Ghost Blog and custom URL Shortening

Аватар пользователя Narang
Автор: Наранг Ричи,
Об авторе: 
Консультант, исследователь и популярный блогер, пишущий о кибербезопасности. Известный оратор и ИБ-евангелист с более чем 10-тилетним стажем.

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/ customization 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 :)

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.

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 ajax() the URL(s) 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.
    You can see below the post Title "Link: <link URL>" 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 :)

Комментарии в Facebook

Вы сообщаете об ошибке в следующем тексте:
Нажмите кнопку «Сообщить об ошибке», чтобы отправить сообщение. Вы также можете добавить комментарий.