Table of Contents

How to Create an RSS Feed for Your Blog — 3 — Automating Updates

This is the third part in a series about creating an RSS feed for your website. Click here to read the first part about why you should have an RSS feed if you don’t already have one and click here to read the second part about writing a basic feed. In this part, I’ll be talking a bit about automating updates to your feed and showing you how I do it.

The General Approach

Once you know what sort of elements you want your feed items to have (e.g. <author>, <link>, etc), you can fairly easily script updates to your feed. Unfortunately, I can’t describe exactly how you should do this since it would differ depending on what operating system you’re using and what tools you have available. If you can leverage something like a static site generator to generate your feeds for you, then this article is probably not going to be of much help to you.

To keep my feed up to date and to generate <item>s, I have a POSIX shell script that I use to publish my posts. You can view the publish script in its entirety online, but I’ve extracted just the bit which updates my feed and pasted it below:

create_rss_entry()
{
    print_info_msg "Creating RSS entry"

    # Opening tag
    rssEntry="${rssEntry}<item>"

    # Set <title>
    rssEntry="${rssEntry}
        <title>$pageTitle</title>"

    # Set <link> and <guid>
    rssEntry="${rssEntry}
        <link>$baseURL/$fileName</link>"
    rssEntry="${rssEntry}
        <guid>$baseURL/$fileName</guid>"

    # Set pubDate
    rssEntry="${rssEntry}
        <pubDate>$date</pubDate>"

    # Fill out content
    pageContent=$(cat build/blog/"$fileName"-content.html)
    rssEntry="${rssEntry}
    <description><![CDATA[$pageContent]]></description>"

    # Close the entry
    rssEntry="${rssEntry}
    </item>"
}

update_rss_feed()
{
    print_info_msg "Updating RSS feed"
    mv "$mainFeed" "$mainFeed.bak"
    head -n 8 "$mainFeed.bak" > "$mainFeed"
    echo "$rssEntry" >> "$mainFeed"
    tail -n +9 "$mainFeed.bak" >> "$mainFeed"
}

It’s actually fairly easy to generate feed entries through a simple script such as this one, and the concepts used above could be easily translated into other scripting languages like Perl or Python if you’re more comfortable using those languages.

I have the benefit that the static site generator I use outputs the HTML of just the post—no HTML <head> or <body> tags included—as an intermediate step in the process of compiling a page from Markdown to HTML. This allows me to easily include the text of the post in the <description> tags, but it would be fairly easy to extract just the HTML inside the <main> tag of your HTML page, should you not have a site generator which does something like this.

Conclusion

This concludes my simple, 3-part guide on creating an RSS feed for your own website or blog. It’s not very difficult to do and it provides numerous benefits for your readers. If the process described in this mini-series seems like too much work or too complicated, there is numerous feed generation software available online. All that matters is that you create a feed for your site.

In the future, I’ll be looking more into Atom feeds and a series about them may be coming soon.

This is my fiftieth post for the #100DaysToOffload challenge. You can learn more about this challenge over at https://100daystooffload.com.