notes.jj1bdx.tokyo blog Home | Posts | RSS

Adding an RSS feed to a static site, 2022 edition

When I tried to add this blog to Feedly, it asked me to enter the RSS Feed URL. Automatically generating the RSS feed required a paid subscription of Feedly. So I decided to write some code of generating the RSS Feed by myself. I had to write some snippets of Elixir code for Lambdapad, and it worked OK in the end. It's another good way to learn Elixir!

Here's some excerpts from lambdapad.exs:

config do
    set transform: fn(config) ->
      currentdatetime = 
          DateTime.utc_now() |> 
              Map.replace(:microsecond, {0, 0}) |>
              DateTime.to_iso8601(:extended)
      Map.put(config, "builddatetime", currentdatetime)
         |> Map.put("site_root", config["blog"]["url"])
    end
end
pages "rss" do
    set from: :posts
    set template: "rss.xml"
    set uri: "/rss.xml"
    set index: true
    set var_name: "posts"
    set uri_type: :file
    set priority: :low
end

And here's the template of rss.xml:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
  <title>{{ blog.title }}</title>
  <description>{{ blog.description }}</description>
  <link>{{ site_root }}index.html</link>
  <lastBuildDate>{{ builddatetime }}</lastBuildDate>

{% for post in posts %}
  <item>
    <title>{{ post.title }}</title>
    <description>{{ post.summary }}</description>
    <link>{{ site_root }}posts/{{post.id}}</link>
    <guid>{{ site_root }}posts/{{post.id}}</guid>
    <pubDate>{{ post.date }}</pubDate>
  </item>
{% endfor %}

</channel>
</rss>

These snippets are applicable for the Lambdapad blog example.