Skip to content

Commit

Permalink
feature(material_blog): add method to identify if a page is part of M…
Browse files Browse the repository at this point in the history
…aterial blog
  • Loading branch information
Guts committed Dec 2, 2024
1 parent 9e5f2b3 commit 435aada
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
15 changes: 15 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,11 +5,13 @@
# ##################################

# standard library
from pathlib import Path
from typing import Optional

# 3rd party
from mkdocs.config.defaults import MkDocsConfig
from mkdocs.plugins import get_plugin_logger
from mkdocs.structure.pages import Page

# package
from mkdocs_rss_plugin.constants import MKDOCS_LOGGER_NAME
Expand Down Expand Up @@ -101,3 +103,16 @@ def is_blog_plugin_enabled_mkdocs(
logger.debug("Material blog plugin is enabled in Mkdocs configuration.")
self.IS_BLOG_PLUGIN_ENABLED = True
return True

def is_page_a_blog_post(self, mkdocs_page: Page) -> bool:
"""Identifies if the given page is part of Material Blog.
Args:
mkdocs_page (Page): page to identify
Returns:
bool: True if the given page is a Material Blog post.
"""
return Path(mkdocs_page.file.src_uri).is_relative_to(
self.blog_plugin_cfg.config.blog_dir
)
22 changes: 9 additions & 13 deletions mkdocs_rss_plugin/integrations/theme_material_social_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
# conditional
try:
from material import __version__ as material_version
from pymdownx.slugs import slugify

except ImportError:
material_version = None
Expand Down Expand Up @@ -261,10 +260,9 @@ def get_social_card_build_path_for_page(
mkdocs_site_dir = self.mkdocs_site_build_dir

# if page is a blog post
if self.integration_material_blog.IS_BLOG_PLUGIN_ENABLED and Path(
mkdocs_page.file.src_uri
).is_relative_to(
self.integration_material_blog.blog_plugin_cfg.config.blog_dir
if (
self.integration_material_blog.IS_BLOG_PLUGIN_ENABLED
and self.integration_material_blog.is_page_a_blog_post(mkdocs_page)
):
expected_built_card_path = Path(
f"{mkdocs_site_dir}/{self.social_cards_assets_dir}/"
Expand Down Expand Up @@ -306,10 +304,9 @@ def get_social_card_cache_path_for_page(self, mkdocs_page: Page) -> Optional[Pat
if self.IS_INSIDERS:

# if page is a blog post
if self.integration_material_blog.IS_BLOG_PLUGIN_ENABLED and Path(
mkdocs_page.file.src_uri
).is_relative_to(
self.integration_material_blog.blog_plugin_cfg.config.blog_dir
if (
self.integration_material_blog.IS_BLOG_PLUGIN_ENABLED
and self.integration_material_blog.is_page_a_blog_post(mkdocs_page)
):
expected_cached_card_path = self.social_cards_cache_dir.joinpath(
f"assets/images/social/{Path(mkdocs_page.file.dest_uri).parent}.png"
Expand Down Expand Up @@ -376,10 +373,9 @@ def get_social_card_url_for_page(
mkdocs_site_url = self.mkdocs_site_url

# if page is a blog post
if self.integration_material_blog.IS_BLOG_PLUGIN_ENABLED and Path(
mkdocs_page.file.src_uri
).is_relative_to(
self.integration_material_blog.blog_plugin_cfg.config.blog_dir
if (
self.integration_material_blog.IS_BLOG_PLUGIN_ENABLED
and self.integration_material_blog.is_page_a_blog_post(mkdocs_page)
):
page_social_card = (
f"{mkdocs_site_url}assets/images/social/"
Expand Down

0 comments on commit 435aada

Please sign in to comment.