2121# conditional
2222try :
2323 from material import __version__ as material_version
24+ from material .plugins .blog .plugin import BlogPlugin
25+ from pymdownx .slugs import slugify
26+
2427except ImportError :
2528 material_version = None
2629
30+
2731# ############################################################################
2832# ########## Globals #############
2933# ################################
3842class IntegrationMaterialSocialCards :
3943 # attributes
4044 IS_ENABLED : bool = True
45+ IS_BLOG_PLUGIN_ENABLED : bool = True
4146 IS_SOCIAL_PLUGIN_ENABLED : bool = True
4247 IS_SOCIAL_PLUGIN_CARDS_ENABLED : bool = True
4348 IS_THEME_MATERIAL : bool = False
@@ -53,6 +58,7 @@ def __init__(self, mkdocs_config: MkDocsConfig, switch_force: bool = True) -> No
5358 it to False to disable it even if Social Cards are enabled in Mkdocs
5459 configuration. Defaults to True.
5560 """
61+ self .mkdocs_config = mkdocs_config
5662 # check if the integration can be enabled or not
5763 self .IS_SOCIAL_PLUGIN_CARDS_ENABLED = (
5864 self .is_social_plugin_and_cards_enabled_mkdocs (mkdocs_config = mkdocs_config )
@@ -85,6 +91,9 @@ def __init__(self, mkdocs_config: MkDocsConfig, switch_force: bool = True) -> No
8591 self .social_cards_cache_dir = self .get_social_cards_cache_dir (
8692 mkdocs_config = mkdocs_config
8793 )
94+ self .IS_BLOG_PLUGIN_ENABLED = self .is_blog_plugin_enabled_mkdocs (
95+ mkdocs_config = mkdocs_config
96+ )
8897 if self .is_mkdocs_theme_material_insiders ():
8998 self .load_cache_cards_manifest ()
9099
@@ -123,6 +132,37 @@ def is_mkdocs_theme_material_insiders(self) -> Optional[bool]:
123132 self .IS_INSIDERS = False
124133 return False
125134
135+ def is_blog_plugin_enabled_mkdocs (self , mkdocs_config : MkDocsConfig ) -> bool :
136+ """Check if blog plugin is installed and enabled.
137+
138+ Args:
139+ mkdocs_config (MkDocsConfig): Mkdocs website configuration object.
140+
141+ Returns:
142+ bool: True if the theme material and the plugin blog is enabled.
143+ """
144+ if not self .is_mkdocs_theme_material (mkdocs_config = mkdocs_config ):
145+ logger .debug ("Installed theme is not 'material'. Integration disabled." )
146+ return False
147+
148+ if not mkdocs_config .plugins .get ("material/blog" ):
149+ logger .debug ("Material blog plugin is not listed in configuration." )
150+ self .IS_BLOG_PLUGIN_ENABLED = False
151+ return False
152+
153+ self .blog_plugin_cfg : BlogPlugin | None = mkdocs_config .plugins .get (
154+ "material/blog"
155+ )
156+
157+ if not self .blog_plugin_cfg .config .enabled :
158+ logger .debug ("Material blog plugin is installed but disabled." )
159+ self .IS_BLOG_PLUGIN_ENABLED = False
160+ return False
161+
162+ logger .debug ("Material blog plugin is enabled in Mkdocs configuration." )
163+ self .IS_BLOG_PLUGIN_ENABLED = True
164+ return True
165+
126166 def is_social_plugin_enabled_mkdocs (self , mkdocs_config : MkDocsConfig ) -> bool :
127167 """Check if social plugin is installed and enabled.
128168
@@ -274,18 +314,29 @@ def get_social_card_build_path_for_page(
274314 if mkdocs_site_dir is None and self .mkdocs_site_build_dir :
275315 mkdocs_site_dir = self .mkdocs_site_build_dir
276316
277- expected_built_card_path = Path (
278- f"{ mkdocs_site_dir } /{ self .social_cards_assets_dir } /"
279- f"{ Path (mkdocs_page .file .src_uri ).with_suffix ('.png' )} "
280- )
317+ # if page is a blog post
318+ if self .IS_BLOG_PLUGIN_ENABLED and Path (
319+ mkdocs_page .file .src_uri
320+ ).is_relative_to (self .blog_plugin_cfg .config .blog_dir ):
321+ expected_built_card_path = Path (
322+ f"{ mkdocs_site_dir } /{ self .social_cards_assets_dir } /"
323+ f"{ Path (mkdocs_page .file .dest_uri ).parent } .png"
324+ )
325+ else :
326+ expected_built_card_path = Path (
327+ f"{ mkdocs_site_dir } /{ self .social_cards_assets_dir } /"
328+ f"{ Path (mkdocs_page .file .src_uri ).with_suffix ('.png' )} "
329+ )
281330
282331 if expected_built_card_path .is_file ():
283332 logger .debug (
284- f"Social card file found in cache folder: { expected_built_card_path } "
333+ f"Social card file found in build folder: { expected_built_card_path } "
285334 )
286335 return expected_built_card_path
287336 else :
288- logger .debug (f"Not found: { expected_built_card_path } " )
337+ logger .debug (
338+ f"Social card not found in build folder: { expected_built_card_path } "
339+ )
289340 return None
290341
291342 def get_social_card_cache_path_for_page (self , mkdocs_page : Page ) -> Optional [Path ]:
@@ -305,16 +356,28 @@ def get_social_card_cache_path_for_page(self, mkdocs_page: Page) -> Optional[Pat
305356 Path: path to the image in local cache folder if it exists
306357 """
307358 if self .IS_INSIDERS :
308- expected_cached_card_path = self .social_cards_cache_dir .joinpath (
309- f"assets/images/social/{ Path (mkdocs_page .file .src_uri ).with_suffix ('.png' )} "
310- )
359+
360+ # if page is a blog post
361+ if self .IS_BLOG_PLUGIN_ENABLED and Path (
362+ mkdocs_page .file .src_uri
363+ ).is_relative_to (self .blog_plugin_cfg .config .blog_dir ):
364+ expected_cached_card_path = self .social_cards_cache_dir .joinpath (
365+ f"assets/images/social/{ Path (mkdocs_page .file .dest_uri ).parent } .png"
366+ )
367+ else :
368+ expected_cached_card_path = self .social_cards_cache_dir .joinpath (
369+ f"assets/images/social/{ Path (mkdocs_page .file .src_uri ).with_suffix ('.png' )} "
370+ )
371+
311372 if expected_cached_card_path .is_file ():
312373 logger .debug (
313374 f"Social card file found in cache folder: { expected_cached_card_path } "
314375 )
315376 return expected_cached_card_path
316377 else :
317- logger .debug (f"Not found: { expected_cached_card_path } " )
378+ logger .debug (
379+ f"Social card not found in cache folder: { expected_cached_card_path } "
380+ )
318381
319382 else :
320383 if "description" in mkdocs_page .meta :
@@ -362,4 +425,18 @@ def get_social_card_url_for_page(
362425 if mkdocs_site_url is None and self .mkdocs_site_url :
363426 mkdocs_site_url = self .mkdocs_site_url
364427
365- return f"{ mkdocs_site_url } assets/images/social/{ Path (mkdocs_page .file .src_uri ).with_suffix ('.png' )} "
428+ # if page is a blog post
429+ if self .IS_BLOG_PLUGIN_ENABLED and Path (
430+ mkdocs_page .file .src_uri
431+ ).is_relative_to (self .blog_plugin_cfg .config .blog_dir ):
432+ page_social_card = (
433+ f"{ mkdocs_site_url } assets/images/social/"
434+ f"{ Path (mkdocs_page .file .dest_uri ).parent } .png"
435+ )
436+ else :
437+ page_social_card = (
438+ f"{ mkdocs_site_url } assets/images/social/"
439+ f"{ Path (mkdocs_page .file .src_uri ).with_suffix ('.png' )} "
440+ )
441+
442+ return page_social_card
0 commit comments