diff --git a/docs/integrations.md b/docs/integrations.md index 5d4997f..37df14f 100644 --- a/docs/integrations.md +++ b/docs/integrations.md @@ -3,6 +3,19 @@ title: Integrations icon: octicons/plug-16 --- +## Blog plugin (from Material theme) + +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/)). + +In some cases, the RSS plugin needs to work with the Material Blog: + +- for blog posts, the structure of the path to social cards is depending on blog configuration +- retrieve the author's name from the `.authors.yml` file + +If you don't want this integration, you can disable it with the option: `use_material_blog=false`. + +> See [related section in settings](./configuration.md#use_material_blog). + ## Social Cards plugin (from Material theme) 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/)). diff --git a/mkdocs_rss_plugin/integrations/theme_material_blog_plugin.py b/mkdocs_rss_plugin/integrations/theme_material_blog_plugin.py index 9817061..951234d 100644 --- a/mkdocs_rss_plugin/integrations/theme_material_blog_plugin.py +++ b/mkdocs_rss_plugin/integrations/theme_material_blog_plugin.py @@ -5,6 +5,7 @@ # ################################## # standard library +from functools import lru_cache from pathlib import Path from typing import Optional @@ -104,6 +105,31 @@ def is_blog_plugin_enabled_mkdocs( self.IS_BLOG_PLUGIN_ENABLED = True return True + @lru_cache + def author_name_from_id(self, author_id: str) -> str: + """Return author name from author_id used in Material blog plugin (.authors.yml). + + Args: + author_id (str): author key in .authors.yml + + Returns: + str: author name or passed author_id if not found within .authors.yml + """ + if ( + self.blog_plugin_cfg.config.authors + and isinstance(self.blog_plugin_cfg, BlogPlugin) + and hasattr(self.blog_plugin_cfg, "authors") + and isinstance(self.blog_plugin_cfg.authors, dict) + ): + if author_id in self.blog_plugin_cfg.authors: + return self.blog_plugin_cfg.authors.get(author_id).get("name") + else: + logger.error( + f"Author ID '{author_id}' is not part of known authors: " + f"{self.blog_plugin_cfg.authors}. Returning author_id." + ) + return author_id + def is_page_a_blog_post(self, mkdocs_page: Page) -> bool: """Identifies if the given page is part of Material Blog. diff --git a/mkdocs_rss_plugin/util.py b/mkdocs_rss_plugin/util.py index 94b0705..a25cde0 100644 --- a/mkdocs_rss_plugin/util.py +++ b/mkdocs_rss_plugin/util.py @@ -387,7 +387,16 @@ def get_authors_from_meta(self, in_page: Page) -> Optional[tuple[str]]: if isinstance(in_page.meta.get("authors"), str): return (in_page.meta.get("authors"),) elif isinstance(in_page.meta.get("authors"), (list, tuple)): - return tuple(in_page.meta.get("authors")) + if ( + self.material_blog.IS_ENABLED + and self.material_blog.is_page_a_blog_post(in_page) + ): + return [ + self.material_blog.author_name_from_id(author_id) + for author_id in in_page.meta.get("authors") + ] + else: + return tuple(in_page.meta.get("authors")) else: logging.warning( "Type of authors value in page.meta (%s) is not valid. "