Skip to content

Commit

Permalink
template:feeds: add extra block
Browse files Browse the repository at this point in the history
  • Loading branch information
heitorPB committed Jul 29, 2023
1 parent f962370 commit 596f7f1
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 0 deletions.
6 changes: 6 additions & 0 deletions components/site/tests/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,12 @@ fn can_build_feeds() {
assert!(file_contains!(public, "posts/tutorials/programming/atom.xml", "Foo Doe"));
assert!(file_contains!(public, "posts/tutorials/programming/atom.xml", "Bar Doe"));
assert!(file_contains!(public, "posts/tutorials/programming/atom.xml", "Baz Doe"));

// Test feeds with extended block
assert!(file_contains!(public, "atom.xml", "<category term=\"Foo tag\"/"));
assert!(file_contains!(public, "atom.xml", "<category term=\"Bar tag\"/"));
assert!(file_contains!(public, "rss.xml", "<category>Foo tag</category>"));
assert!(file_contains!(public, "rss.xml", "<category>Bar tag</category>"));
}

#[test]
Expand Down
3 changes: 3 additions & 0 deletions components/templates/src/builtins/atom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
{% else %}
<content type="html" xml:base="{{ page.permalink | escape_xml | safe }}">{{ page.content }}</content>
{% endif %}

{%- block atom_entry_extra %}
{% endblock atom_entry_extra -%}
</entry>
{%- endfor %}
</feed>
3 changes: 3 additions & 0 deletions components/templates/src/builtins/rss.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
<link>{{ page.permalink | escape_xml | safe }}</link>
<guid>{{ page.permalink | escape_xml | safe }}</guid>
<description xml:base="{{ page.permalink | escape_xml | safe }}">{% if page.summary %}{{ page.summary }}{% else %}{{ page.content }}{% endif %}</description>

{%- block rss_item_extra %}
{% endblock rss_item_extra -%}
</item>
{%- endfor %}
</channel>
Expand Down
7 changes: 7 additions & 0 deletions docs/content/documentation/templates/feeds/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ for `atom.xml` (in the preferred Atom 1.0 format), and `rss.xml` (in the RSS
2.0 format). If you choose a different filename (e.g. `feed.xml`), you will
need to provide a template yourself.

Alternatively, instead of providing an entire template for the feed you can
extend them by [overriding a
block](@/documentation/themes/extending-a-theme.md#overriding-a-block). The
Atom template provides a block named `atom_entry_extra`, while the rss template
has the block `rss_item_extra`. These blocks allow you to extend the content of
each item that goes into the feed.

**Only pages with a date will be available.**

The feed template gets five variables:
Expand Down
1 change: 1 addition & 0 deletions test_site/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ theme = "sample"
taxonomies = [
{name = "categories", feed = true},
{name = "podcast_authors", feed = true},
{name = "tags", feed = true},
]

ignored_content = ["*/ignored.md"]
Expand Down
10 changes: 10 additions & 0 deletions test_site/content/posts/with-tag-taxonomy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
+++
title = "A post with tags"
description = ""
date = 2023-07-29

[taxonomies]
tags = ["foo tag", "bar tag"]
+++

This post has a `tags` taxonomy.
11 changes: 11 additions & 0 deletions test_site/templates/atom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends "atom.xml" %}

{% block atom_entry_extra %}

{%- if page.taxonomies and page.taxonomies.tags %}
{%- for tag in page.taxonomies.tags %}
<category term="{{ tag }}/">
{% endfor -%}
{% endif -%}

{% endblock atom_entry_extra %}
11 changes: 11 additions & 0 deletions test_site/templates/rss.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends "rss.xml" %}

{% block rss_item_extra %}

{%- if page.taxonomies and page.taxonomies.tags %}
{%- for tag in page.taxonomies.tags %}
<category>{{ tag }}</category>
{% enfor -%}
{% endif -%}

{% endblock rss_item_extra %}

0 comments on commit 596f7f1

Please sign in to comment.