All Posts Search with GhostHunter - Your Ghost Blog 0.X/1.X

HTML Jul 15, 2018

Hi guys,

In this post, I am going to talk about a key feature that is very important specifically to blog websites i.e Searches. It is really important to give your readers ability to search with texts allow them to find their answers quickly.

This post is specifically for Ghost blogs, one of the complete blogging platforms in market that can help you setup your blog in minutes giving full freedom for customization as it is built on handlebars very similar to html. To know more about Ghost, please visit their website

As you guys are aware, there are N numbers of libraries that can be used to achieve search feature but for this post purpose, I will be using GhostHunter. The key reason to finalize this library is:

  1. One of the most used library in Ghost world
  2. Tightly coupled with Ghost blog
  3. Easy to use and maintain

Now, there are two seperate versions for GhostHunter. One with Ghost 1.X and another one with Ghost 0.X.

With 1.X, it is pretty straight forward, GhostHunter uses Ghost APIs to get all your posts and enable search on them. you just need to enable Public API feature on your settings panel to expose your APIs for ghost hunter and it will work.

With 0.X, it is little bit tricky and that is my main reason to write this post. It is tricky because Ghost 0.X doesn't support Public APIs which makes GhostHunter implementation different as it can only look to your blog's RSS. That's right.

If you are using Ghost 0.X, there is no straight way to implement GhostHunter to look to all your posts. It can only look for RSS which only provide your top 20 - 30 posts at max.

To overcome this, you need to change one thing to make it working i.e. expose your posts with API. To do that you need to change a middleware file that checks for authentication.

Important: Keep in mind, this is the your main file which handles all authentication. Your changes should be very specific to open GET request to Post APIs.

Below are the changes that you need to do in middleware.js to expose GET request

In above code, you will see that condition (path.indexOf('v0.1/posts') === -1 || req.method !== 'GET' || scope !== 'public') only opens POST GET API and that is also specific for request sending query parameter scope=public in request. You can use this scope parameter to distingush between a request coming from GhostHunter or Ghost content section(on which all posts appears). Once you are done with all these changes, you can test your changes by hitting following url and also by removing scope parameter.

http://yourdomain/ghost/api/v0.1/posts/?limit=all&scope=public

Hope this helps! Happy Coding.

Related Tags:

HTML   Ghost   JavaScript   GhostHunter   Search   Ghost0.X