From 596f7f1e7f3b221ad06cbde557e06ec4980e166f Mon Sep 17 00:00:00 2001 From: Heitor Pascoal de Bittencourt Date: Sat, 29 Jul 2023 16:50:32 -0300 Subject: [PATCH] template:feeds: add extra block --- components/site/tests/site.rs | 6 ++++++ components/templates/src/builtins/atom.xml | 3 +++ components/templates/src/builtins/rss.xml | 3 +++ docs/content/documentation/templates/feeds/index.md | 7 +++++++ test_site/config.toml | 1 + test_site/content/posts/with-tag-taxonomy.md | 10 ++++++++++ test_site/templates/atom.xml | 11 +++++++++++ test_site/templates/rss.xml | 11 +++++++++++ 8 files changed, 52 insertions(+) create mode 100644 test_site/content/posts/with-tag-taxonomy.md create mode 100644 test_site/templates/atom.xml create mode 100644 test_site/templates/rss.xml diff --git a/components/site/tests/site.rs b/components/site/tests/site.rs index baca868e2..33c40cc2e 100644 --- a/components/site/tests/site.rs +++ b/components/site/tests/site.rs @@ -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", "Foo tag")); + assert!(file_contains!(public, "rss.xml", "Bar tag")); } #[test] diff --git a/components/templates/src/builtins/atom.xml b/components/templates/src/builtins/atom.xml index 153317599..6322580b6 100644 --- a/components/templates/src/builtins/atom.xml +++ b/components/templates/src/builtins/atom.xml @@ -48,6 +48,9 @@ {% else %} {{ page.content }} {% endif %} + + {%- block atom_entry_extra %} + {% endblock atom_entry_extra -%} {%- endfor %} diff --git a/components/templates/src/builtins/rss.xml b/components/templates/src/builtins/rss.xml index a684a48af..15a354a21 100644 --- a/components/templates/src/builtins/rss.xml +++ b/components/templates/src/builtins/rss.xml @@ -34,6 +34,9 @@ {{ page.permalink | escape_xml | safe }} {{ page.permalink | escape_xml | safe }} {% if page.summary %}{{ page.summary }}{% else %}{{ page.content }}{% endif %} + + {%- block rss_item_extra %} + {% endblock rss_item_extra -%} {%- endfor %} diff --git a/docs/content/documentation/templates/feeds/index.md b/docs/content/documentation/templates/feeds/index.md index e13319231..a0a530697 100644 --- a/docs/content/documentation/templates/feeds/index.md +++ b/docs/content/documentation/templates/feeds/index.md @@ -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: diff --git a/test_site/config.toml b/test_site/config.toml index e3fcbfd64..4694d39b3 100644 --- a/test_site/config.toml +++ b/test_site/config.toml @@ -7,6 +7,7 @@ theme = "sample" taxonomies = [ {name = "categories", feed = true}, {name = "podcast_authors", feed = true}, + {name = "tags", feed = true}, ] ignored_content = ["*/ignored.md"] diff --git a/test_site/content/posts/with-tag-taxonomy.md b/test_site/content/posts/with-tag-taxonomy.md new file mode 100644 index 000000000..21758e08e --- /dev/null +++ b/test_site/content/posts/with-tag-taxonomy.md @@ -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. diff --git a/test_site/templates/atom.xml b/test_site/templates/atom.xml new file mode 100644 index 000000000..e92c4271f --- /dev/null +++ b/test_site/templates/atom.xml @@ -0,0 +1,11 @@ +{% extends "atom.xml" %} + +{% block atom_entry_extra %} + +{%- if page.taxonomies and page.taxonomies.tags %} +{%- for tag in page.taxonomies.tags %} + +{% endfor -%} +{% endif -%} + +{% endblock atom_entry_extra %} diff --git a/test_site/templates/rss.xml b/test_site/templates/rss.xml new file mode 100644 index 000000000..f44464b9c --- /dev/null +++ b/test_site/templates/rss.xml @@ -0,0 +1,11 @@ +{% extends "rss.xml" %} + +{% block rss_item_extra %} + +{%- if page.taxonomies and page.taxonomies.tags %} +{%- for tag in page.taxonomies.tags %} +{{ tag }} +{% enfor -%} +{% endif -%} + +{% endblock rss_item_extra %}