Prasanna Natarajan
Posted on October 20, 2019
In my last post, I mentioned about why Nanoc is a great choice for building your little static site if you love Ruby.
There I mentioned I have 3 separate blogs in a single site, in single domain. If you want your readers to get notified of your blog posts written in a specific blog, and if you never heard about email, and if you are an yesteryear geek, then adding RSS feeds to your blogs is a great idea.
With RSS feeds (or atom feeds), your readers can subscribe to your blog using any feed reader tool (like Feedly, Newsblur or Google Reader). Now they don't have to refresh their screen in hopes of dying to read your latest blog post. The feed reader tool will pull the latest posts automatically and they'll be able to read it as soon as a new post is published.
Anyway, with nanoc, you have to build everything from scratch. But to make things a bit easier, they have a special blogging helper module. It's in-built and comes with the necessary functions to create a blog. Once such thing is the atom_feed()
function. Just calling it from the right place with right arguments will give your site a nice rss feed.
Let's see how to create these in your nanoc blog, and especially how to do so if you have multiple blogs in same site like I do.
The first step is deciding how you want the feed url to look like? Are you ok with example.com/subscribe
, example.com/feed
or example.com/feed.xml
etc. This route is going to be defined in the Rules
file in nanoc.
Mine looks like this:
%w(tech books general).each do |blog|
# atom feeds for specific blogs
compile "/#{blog}/feed.erb" do
filter :erb
write "/#{blog}/feed.xml"
end
end
This creates 3 feed urls for my 3 blogs. The compile
method is going to look for the /tech/feed.erb
file inside the content/tech/
folder. And similarly for the other 2 blogs. It then uses the erb filter to make sense of these erb files. And finally, it makes this path available for the site at /tech/feed.xml
(Eg: https://npras.in/tech/feed.xml) Now anyone with this url can subscribe specifically to this tech blog.
Now the second part. The url should return the correct feed xml once it is visited. That's what'll make it work. We can use the atom_feed()
function that nonc gives us for this. Put this method with the right arguments in a feed.erb
file in this path: /content/tech/feed.erb
:
<%= atom_feed limit: 50, articles: sorted_articles_of_kind(:books), title: "Prasanna Natarajan's tech blog" %>
That's it. Now, when you visit that xml url, you'll have your feed returned. You can use it to subscribe to the blog from anywhere.
The sorted_articles_of_kind
is a helper method in my lib/helpers.rb
file. The function looks like this:
def sorted_articles_of_kind(kind)
sorted_articles.find_all { |a| a.identifier =~ %r{/#{kind}/} }
end
One feed to rule them all
I also have a common feed that has contents from all 3 blogs. This is for anyone who wants to know about the latest post coming across all 3 blog. https://npras.in/feed.xml.
To create this, first create a compile entry in the content/feed.erb
file. And then add it with this content:
<%= atom_feed limit: 50, articles: sorted_articles %>
Now you know how to setup rss/atom feeds for sites using nanoc. Happy blogging!
Posted on October 20, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.