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,9 @@ 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 = self .is_blog_plugin_enabled_mkdocs (
95
+ mkdocs_config = mkdocs_config
96
+ )
88
97
if self .is_mkdocs_theme_material_insiders ():
89
98
self .load_cache_cards_manifest ()
90
99
@@ -123,6 +132,37 @@ def is_mkdocs_theme_material_insiders(self) -> Optional[bool]:
123
132
self .IS_INSIDERS = False
124
133
return False
125
134
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
+
126
166
def is_social_plugin_enabled_mkdocs (self , mkdocs_config : MkDocsConfig ) -> bool :
127
167
"""Check if social plugin is installed and enabled.
128
168
@@ -274,18 +314,29 @@ def get_social_card_build_path_for_page(
274
314
if mkdocs_site_dir is None and self .mkdocs_site_build_dir :
275
315
mkdocs_site_dir = self .mkdocs_site_build_dir
276
316
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
+ )
281
330
282
331
if expected_built_card_path .is_file ():
283
332
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 } "
285
334
)
286
335
return expected_built_card_path
287
336
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
+ )
289
340
return None
290
341
291
342
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
305
356
Path: path to the image in local cache folder if it exists
306
357
"""
307
358
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
+
311
372
if expected_cached_card_path .is_file ():
312
373
logger .debug (
313
374
f"Social card file found in cache folder: { expected_cached_card_path } "
314
375
)
315
376
return expected_cached_card_path
316
377
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
+ )
318
381
319
382
else :
320
383
if "description" in mkdocs_page .meta :
@@ -362,4 +425,18 @@ def get_social_card_url_for_page(
362
425
if mkdocs_site_url is None and self .mkdocs_site_url :
363
426
mkdocs_site_url = self .mkdocs_site_url
364
427
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