Customizing Ghost: Show All Tags in UI

As you might have noticed, I have list of available tags on the right hand side. In the current Ghost Version, I don't see an api to get list of tags. Some people might do a workaround by looping through all posts, then get the unique tags. This might cause performance issues if you have a lot of posts and tags

Since I'm currently still learning NodeJS, I take this Ghost blog as an experiment tool. What I'm trying to do is to change some Ghost core codes so that it exposes list of tags to the public.

Basically, there are 3 things that I change:

  1. Add new function in core/server/controllers/frontend.js, inside frontendControllers variable

    tags: function(req, res, next){        
  2. In core/server/routes/frontend.js, add this line:
    router.get('/tags/', frontend.tags)

  3. Remove validation logic in core/server/api/tags.js inside browser function
    To be honest, I'm not 100% sure if this is an OK thing to do. I need to remove the validation because it won't allow me to get the data through frontend. Perhaps there should be a better way if I can pass the correct options parameter to this function.
    In short, I replace the logic in this function to:

    return dataProvider.Tag.findAll(options).then(function (result) {
        return {tags: result.toJSON()};

And that's pretty much it! Now if you try to load url : [your-domain]/tags, it will return list of tags.

The next step will be up to you. For me, since I don't want to load them every time I navigate to other article/page, I put them in localStorage and will be expired after 1 day.

However, you might see some orphan tags that doesn't have any blog contents. This is the current behavior in Ghost where tags won't be removed if they're not used. They mention in here that they're going to implement a tag management tool later.

Meanwhile, you can just manually delete the data from the db. But it's a bit troublesome if you use sqlite3. Thus, I do a workaround by exposing tag delete api in admin level. I'm gonna discuss it on a later post

Abdurrachman Habibi

Read more posts by this author.