Skip to content

Commit

Permalink
feature(material_blog): retrieve author name from Material blog autho…
Browse files Browse the repository at this point in the history
…rs file (typically .authors.yml)
  • Loading branch information
Guts committed Dec 2, 2024
1 parent 435aada commit c779b92
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
13 changes: 13 additions & 0 deletions docs/integrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)).
Expand Down
26 changes: 26 additions & 0 deletions mkdocs_rss_plugin/integrations/theme_material_blog_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# ##################################

# standard library
from functools import lru_cache
from pathlib import Path
from typing import Optional

Expand Down Expand Up @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion mkdocs_rss_plugin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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. "
Expand Down

0 comments on commit c779b92

Please sign in to comment.