in Uncategorized

Microblogging on Ghost

Over the last week, I have been working on incorporating microblogging – short, untitled posts – into my new Ghost blog. This is some short documentation on how it’s currently set up.

First, I label each micropost with an internal tag, #note. This can be used to remove the title field of microposts from the post list and individual post pages, create a separate collection, and customize the RSS feed.

Title and permalink

I am using the Attila theme. In this theme, we need to modify /partials/loop.hbs and /post.hbs to do two things with microposts: 1) not display the title and 2) use the date as the permalink.

Here is the code to not show the title:

{{^has tag="#note"}}
    <h1 class="post-title">{{{title}}}</h1>
{{/has}}

And here is the code for using the date as the permalink:

{{#has tag="#note"}}
    <a href="{{url}}">{{ date published_at format = "HH:mm"}} | {{date published_at format = "DD MMM YYYY"}}</a>
{{else}}
    <a href="{{url}}">{{date published_at format="DD MMM YYYY"}}</a>
{{/has}}

Collections

You might prefer your microposts to be separate from longer posts. In this case, modify routes.yaml according to the official documentation.

It might look something like this:

collections:
  /:
    permalink: /{slug}/
    template: index
    filter: tag:-hash-note
  /notes/:
    permalink: /notes/{slug}
    template: index
    filter: tag:hash-note

RSS feed

For the time being, I have chosen to keep all my posts in one feed. Here’s the official documentation on Custom RSS.

My routes.yaml includes:

routes:
  /rss/:
    template: custom-rss
    content_type: text/xml

And my custom-rss.hbs template looks like this:

<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        <title><![CDATA[ {{@blog.title}} ]]></title>
        <description><![CDATA[ {{@blog.description}} ]]></description>
        <link>{{@blog.url}}</link>
        <image>
            <url>{{@blog.url}}/favicon.png</url>
            <title>{{@blog.title}}</title>
            <link>{{@blog.url}}</link>
        </image>
        <lastBuildDate>{{date format="ddd, DD MMM YYYY HH:mm:ss ZZ"}}</lastBuildDate>
        <atom:link href="{{@blog.url}}" rel="self" type="application/rss+xml"/>
        <ttl>60</ttl>

        {{#get "posts" limit="all" include="tags"}}
            {{#foreach posts}}
                <item>
                    {{^has tag="#note"}}
                    <title><![CDATA[ {{title}} ]]></title>
                    {{/has}}
                    <link>{{url absolute="true"}}</link>
                    <guid isPermalink="false">{{id}}</guid>
                    <pubDate>{{date format="ddd, DD MMM YYYY HH:mm ZZ"}}</pubDate>
                    <content:encoded><![CDATA[ {{content}} ]]></content:encoded>
                </item>
            {{/foreach}}
        {{/get}}
    </channel>
</rss>

When I was customizing RSS, there were two simple mistakes that took me a while to realize. One, YAML syntax is particular – you must put /rss/: on the line immediately under routes:, and you must use two-space indentation correctly. Two, the template must be in the correct folder.

If you want to learn more about microblogging, start here.