Skip to content

Commit c779b92

Browse files
committed
feature(material_blog): retrieve author name from Material blog authors file (typically .authors.yml)
1 parent 435aada commit c779b92

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

docs/integrations.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@ title: Integrations
33
icon: octicons/plug-16
44
---
55

6+
## Blog plugin (from Material theme)
7+
8+
Since version 1.17, the plugin integrates with the [Blog plugin (shipped with Material theme)](https://squidfunk.github.io/mkdocs-material/plugins/blog/) (see also [the tutorial about blog + RSS plugins](https://squidfunk.github.io/mkdocs-material/tutorials/blogs/engage/)).
9+
10+
In some cases, the RSS plugin needs to work with the Material Blog:
11+
12+
- for blog posts, the structure of the path to social cards is depending on blog configuration
13+
- retrieve the author's name from the `.authors.yml` file
14+
15+
If you don't want this integration, you can disable it with the option: `use_material_blog=false`.
16+
17+
> See [related section in settings](./configuration.md#use_material_blog).
18+
619
## Social Cards plugin (from Material theme)
720

821
Since version 1.10, the plugin integrates with the [Social Cards plugin (shipped with Material theme)](https://squidfunk.github.io/mkdocs-material/setup/setting-up-social-cards/) (see also [the full plugin documentation here](https://squidfunk.github.io/mkdocs-material/plugins/social/)).

mkdocs_rss_plugin/integrations/theme_material_blog_plugin.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# ##################################
66

77
# standard library
8+
from functools import lru_cache
89
from pathlib import Path
910
from typing import Optional
1011

@@ -104,6 +105,31 @@ def is_blog_plugin_enabled_mkdocs(
104105
self.IS_BLOG_PLUGIN_ENABLED = True
105106
return True
106107

108+
@lru_cache
109+
def author_name_from_id(self, author_id: str) -> str:
110+
"""Return author name from author_id used in Material blog plugin (.authors.yml).
111+
112+
Args:
113+
author_id (str): author key in .authors.yml
114+
115+
Returns:
116+
str: author name or passed author_id if not found within .authors.yml
117+
"""
118+
if (
119+
self.blog_plugin_cfg.config.authors
120+
and isinstance(self.blog_plugin_cfg, BlogPlugin)
121+
and hasattr(self.blog_plugin_cfg, "authors")
122+
and isinstance(self.blog_plugin_cfg.authors, dict)
123+
):
124+
if author_id in self.blog_plugin_cfg.authors:
125+
return self.blog_plugin_cfg.authors.get(author_id).get("name")
126+
else:
127+
logger.error(
128+
f"Author ID '{author_id}' is not part of known authors: "
129+
f"{self.blog_plugin_cfg.authors}. Returning author_id."
130+
)
131+
return author_id
132+
107133
def is_page_a_blog_post(self, mkdocs_page: Page) -> bool:
108134
"""Identifies if the given page is part of Material Blog.
109135

mkdocs_rss_plugin/util.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,16 @@ def get_authors_from_meta(self, in_page: Page) -> Optional[tuple[str]]:
387387
if isinstance(in_page.meta.get("authors"), str):
388388
return (in_page.meta.get("authors"),)
389389
elif isinstance(in_page.meta.get("authors"), (list, tuple)):
390-
return tuple(in_page.meta.get("authors"))
390+
if (
391+
self.material_blog.IS_ENABLED
392+
and self.material_blog.is_page_a_blog_post(in_page)
393+
):
394+
return [
395+
self.material_blog.author_name_from_id(author_id)
396+
for author_id in in_page.meta.get("authors")
397+
]
398+
else:
399+
return tuple(in_page.meta.get("authors"))
391400
else:
392401
logging.warning(
393402
"Type of authors value in page.meta (%s) is not valid. "

0 commit comments

Comments
 (0)