21
21
# conditional
22
22
try :
23
23
from material import __version__ as material_version
24
+ from material .plugins .blog .plugin import BlogPlugin
25
+ from pymdownx .slugs import slugify
26
+
24
27
except ImportError :
25
28
material_version = None
26
29
30
+
27
31
# ############################################################################
28
32
# ########## Globals #############
29
33
# ################################
38
42
class IntegrationMaterialSocialCards :
39
43
# attributes
40
44
IS_ENABLED : bool = True
45
+ IS_BLOG_PLUGIN_ENABLED : bool = True
41
46
IS_SOCIAL_PLUGIN_ENABLED : bool = True
42
47
IS_SOCIAL_PLUGIN_CARDS_ENABLED : bool = True
43
48
IS_THEME_MATERIAL : bool = False
@@ -53,6 +58,7 @@ def __init__(self, mkdocs_config: MkDocsConfig, switch_force: bool = True) -> No
53
58
it to False to disable it even if Social Cards are enabled in Mkdocs
54
59
configuration. Defaults to True.
55
60
"""
61
+ self .mkdocs_config = mkdocs_config
56
62
# check if the integration can be enabled or not
57
63
self .IS_SOCIAL_PLUGIN_CARDS_ENABLED = (
58
64
self .is_social_plugin_and_cards_enabled_mkdocs (mkdocs_config = mkdocs_config )
@@ -85,6 +91,7 @@ def __init__(self, mkdocs_config: MkDocsConfig, switch_force: bool = True) -> No
85
91
self .social_cards_cache_dir = self .get_social_cards_cache_dir (
86
92
mkdocs_config = mkdocs_config
87
93
)
94
+ self .is_blog_plugin_enabled_mkdocs (mkdocs_config = mkdocs_config )
88
95
if self .is_mkdocs_theme_material_insiders ():
89
96
self .load_cache_cards_manifest ()
90
97
@@ -123,6 +130,36 @@ def is_mkdocs_theme_material_insiders(self) -> Optional[bool]:
123
130
self .IS_INSIDERS = False
124
131
return False
125
132
133
+ def is_blog_plugin_enabled_mkdocs (self , mkdocs_config : MkDocsConfig ) -> bool :
134
+ """Check if blog plugin is installed and enabled.
135
+
136
+ Args:
137
+ mkdocs_config (MkDocsConfig): Mkdocs website configuration object.
138
+
139
+ Returns:
140
+ bool: True if the theme material and the plugin blog is enabled.
141
+ """
142
+ if not self .is_mkdocs_theme_material (mkdocs_config = mkdocs_config ):
143
+ logger .debug ("Installed theme is not 'material'. Integration disabled." )
144
+ return False
145
+
146
+ if not mkdocs_config .plugins .get ("material/blog" ):
147
+ logger .debug ("Material blog plugin is not listed in configuration." )
148
+ return False
149
+
150
+ self .blog_plugin_cfg : BlogPlugin | None = mkdocs_config .plugins .get (
151
+ "material/blog"
152
+ )
153
+
154
+ if not self .blog_plugin_cfg .config .enabled :
155
+ logger .debug ("Material blog plugin is installed but disabled." )
156
+ self .IS_BLOG_PLUGIN_ENABLED = False
157
+ return False
158
+
159
+ logger .debug ("Material blog plugin is enabled in Mkdocs configuration." )
160
+ self .IS_SOCIAL_PLUGIN_CARDS_ENABLED = True
161
+ return True
162
+
126
163
def is_social_plugin_enabled_mkdocs (self , mkdocs_config : MkDocsConfig ) -> bool :
127
164
"""Check if social plugin is installed and enabled.
128
165
@@ -274,18 +311,29 @@ def get_social_card_build_path_for_page(
274
311
if mkdocs_site_dir is None and self .mkdocs_site_build_dir :
275
312
mkdocs_site_dir = self .mkdocs_site_build_dir
276
313
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
- )
314
+ # if page is a blog post
315
+ if self .IS_BLOG_PLUGIN_ENABLED and Path (
316
+ mkdocs_page .file .src_uri
317
+ ).is_relative_to (self .blog_plugin_cfg .config .blog_dir ):
318
+ expected_built_card_path = Path (
319
+ f"{ mkdocs_site_dir } /{ self .social_cards_assets_dir } /"
320
+ f"{ Path (mkdocs_page .file .dest_uri ).parent } .png"
321
+ )
322
+ else :
323
+ expected_built_card_path = Path (
324
+ f"{ mkdocs_site_dir } /{ self .social_cards_assets_dir } /"
325
+ f"{ Path (mkdocs_page .file .src_uri ).with_suffix ('.png' )} "
326
+ )
281
327
282
328
if expected_built_card_path .is_file ():
283
329
logger .debug (
284
- f"Social card file found in cache folder: { expected_built_card_path } "
330
+ f"Social card file found in build folder: { expected_built_card_path } "
285
331
)
286
332
return expected_built_card_path
287
333
else :
288
- logger .debug (f"Not found: { expected_built_card_path } " )
334
+ logger .debug (
335
+ f"Social card not found in build folder: { expected_built_card_path } "
336
+ )
289
337
return None
290
338
291
339
def get_social_card_cache_path_for_page (self , mkdocs_page : Page ) -> Optional [Path ]:
@@ -305,16 +353,28 @@ def get_social_card_cache_path_for_page(self, mkdocs_page: Page) -> Optional[Pat
305
353
Path: path to the image in local cache folder if it exists
306
354
"""
307
355
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
- )
356
+
357
+ # if page is a blog post
358
+ if self .IS_BLOG_PLUGIN_ENABLED and Path (
359
+ mkdocs_page .file .src_uri
360
+ ).is_relative_to (self .blog_plugin_cfg .config .blog_dir ):
361
+ expected_cached_card_path = self .social_cards_cache_dir .joinpath (
362
+ f"assets/images/social/{ Path (mkdocs_page .file .dest_uri ).parent } .png"
363
+ )
364
+ else :
365
+ expected_cached_card_path = self .social_cards_cache_dir .joinpath (
366
+ f"assets/images/social/{ Path (mkdocs_page .file .src_uri ).with_suffix ('.png' )} "
367
+ )
368
+
311
369
if expected_cached_card_path .is_file ():
312
370
logger .debug (
313
371
f"Social card file found in cache folder: { expected_cached_card_path } "
314
372
)
315
373
return expected_cached_card_path
316
374
else :
317
- logger .debug (f"Not found: { expected_cached_card_path } " )
375
+ logger .debug (
376
+ f"Social card not found in cache folder: { expected_cached_card_path } "
377
+ )
318
378
319
379
else :
320
380
if "description" in mkdocs_page .meta :
@@ -362,4 +422,18 @@ def get_social_card_url_for_page(
362
422
if mkdocs_site_url is None and self .mkdocs_site_url :
363
423
mkdocs_site_url = self .mkdocs_site_url
364
424
365
- return f"{ mkdocs_site_url } assets/images/social/{ Path (mkdocs_page .file .src_uri ).with_suffix ('.png' )} "
425
+ # if page is a blog post
426
+ if self .IS_BLOG_PLUGIN_ENABLED and Path (
427
+ mkdocs_page .file .src_uri
428
+ ).is_relative_to (self .blog_plugin_cfg .config .blog_dir ):
429
+ page_social_card = (
430
+ f"{ mkdocs_site_url } assets/images/social/"
431
+ f"{ Path (mkdocs_page .file .dest_uri ).parent } .png"
432
+ )
433
+ else :
434
+ page_social_card = (
435
+ f"{ mkdocs_site_url } assets/images/social/"
436
+ f"{ Path (mkdocs_page .file .src_uri ).with_suffix ('.png' )} "
437
+ )
438
+
439
+ return page_social_card
0 commit comments