Skip to content

Commit 64966c7

Browse files
authored
feat: allow per-page description customization (#310)
Hello @Guts Thanks for the awesome project. I use this plugin heavily in my [SRE blog](https://developer-friendly.blog). I figured it would be nice to allow per page customization of the `description` field, cause at the moment, there's not a lot one can do when the description before `<!-- more -->` is short and extra detail is desired. With this change, you can have separate contents for RSS description and your typical content. An example taken from the [Material for Mkdocs Social Cards](https://squidfunk.github.io/mkdocs-material/plugins/social/#option.description): ```markdown --- date: 2024-06-24 description: >- This is the SEO description. social: cards_layout_options: description: >- This is the social cards description. rss: feed_description: >- And I want to have customized RSS description. --- ``` This idea, I reckon, can easily be extended for other similar use-cases: - #297 - #275 - #197
2 parents c62cb56 + 6506bae commit 64966c7

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

docs/configuration.md

+20
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,32 @@ Default: `true`.
173173
Used, in combination with `abstract_delimiter`, to determine each [item description element](https://www.w3schools.com/xml/rss_tag_title_link_description_item.asp):
174174

175175
- If this value is set to `-1`, then the articles' full HTML content will be filled into the description element.
176+
- If you want to customize the description per each Markdown page, refer to the example below.
176177
- Otherwise, the plugin first tries to retrieve the value of the keyword `description` from the [page metadata].
177178
- If that fails and `abstract_delimiter` is found in the page, the article content up to (but not including) the delimiter is used.
178179
- If the above has failed, then the plugin retrieves the first number of characters of the page content defined by this setting. Retrieved content is the raw markdown converted roughly into HTML.
179180

180181
Be careful: if set to `0` and there is no description, the feed's compliance is broken (an item must have a description).
181182

183+
#### Override feed description per page
184+
185+
To customize the value of the RSS description per each page and override the value of `site_description` and `plugins.rss.feed_description`, you can modify the value per each page as you see in the example below:
186+
187+
```markdown
188+
---
189+
date: 2024-06-24
190+
description: >-
191+
This is the SEO description.
192+
social:
193+
cards_layout_options:
194+
description: >-
195+
This is the social cards description.
196+
rss:
197+
feed_description: >-
198+
And I want to have customized RSS description.
199+
---
200+
```
201+
182202
`abstract_chars_count`: number of characters to use as item description.
183203

184204
Default: `150`

mkdocs_rss_plugin/util.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,10 @@ def get_description_or_abstract(
498498
Returns:
499499
str: page description to use
500500
"""
501-
description = in_page.meta.get("description")
501+
if in_page.meta.get("rss", {}).get("feed_description"):
502+
description = in_page.meta["rss"]["feed_description"]
503+
else:
504+
description = in_page.meta.get("description")
502505

503506
# If the full page is wanted (unlimited chars count)
504507
if chars_count == -1 and (in_page.content or in_page.markdown):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: Page with overridden rss feed description
3+
authors:
4+
- Guts
5+
- Tim Vink
6+
- liang2kl
7+
date: 2020-03-20 10:20
8+
update: 2022-03-30 10:20
9+
description: First test page of mkdocs-rss-plugin test suite
10+
image: "https://svgsilh.com/png-512/97849.png"
11+
rss:
12+
feed_description: This is a custom override of the feed description
13+
tags:
14+
- test
15+
---
16+
17+
# Test page with meta
18+
19+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

tests/test_build.py

+34
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,40 @@ def test_simple_build_custom_title_description(self):
679679
self.assertEqual(feed_parsed.feed.title, "My custom RSS title")
680680
self.assertEqual(feed_parsed.feed.description, "My custom RSS description")
681681

682+
def test_simple_build_override_per_page_rss_feed_description(self):
683+
"""
684+
Test per-page rss.feed_description overrides the config site_description and rss.feed_description
685+
686+
How to run this test:
687+
pytest tests/test_build.py::TestBuildRss::test_simple_build_override_per_page_rss_feed_description
688+
"""
689+
with tempfile.TemporaryDirectory() as tmpdirname:
690+
cli_result = self.build_docs_setup(
691+
testproject_path="docs",
692+
mkdocs_yml_filepath=Path(
693+
"tests/fixtures/mkdocs_custom_title_description.yml"
694+
),
695+
output_path=tmpdirname,
696+
)
697+
if cli_result.exception is not None:
698+
e = cli_result.exception
699+
logger.debug(format_exception(type(e), e, e.__traceback__))
700+
701+
self.assertEqual(cli_result.exit_code, 0)
702+
self.assertIsNone(cli_result.exception)
703+
704+
# created items
705+
feed_parsed = feedparser.parse(Path(tmpdirname) / OUTPUT_RSS_FEED_CREATED)
706+
for feed_item in feed_parsed.entries:
707+
if feed_item.title == "Page with overridden rss feed description":
708+
self.assertEqual(
709+
feed_item.description,
710+
"This is a custom override of the feed description",
711+
)
712+
break
713+
else:
714+
self.fail("Page with overridden rss feed description not found")
715+
682716
def test_rss_feed_validation(self):
683717
with tempfile.TemporaryDirectory() as tmpdirname:
684718
cli_result = self.build_docs_setup(

0 commit comments

Comments
 (0)