8
8
from pathlib import Path
9
9
10
10
# 3rd party
11
- from mkdocs .config .config_options import Config
11
+ from mkdocs .config .defaults import MkDocsConfig
12
12
from mkdocs .plugins import get_plugin_logger
13
13
from mkdocs .structure .pages import Page
14
14
@@ -40,11 +40,11 @@ class IntegrationMaterialSocialCards:
40
40
IS_THEME_MATERIAL : bool = False
41
41
IS_INSIDERS : bool = False
42
42
43
- def __init__ (self , mkdocs_config : Config , switch_force : bool = True ) -> None :
43
+ def __init__ (self , mkdocs_config : MkDocsConfig , switch_force : bool = True ) -> None :
44
44
"""Integration instanciation.
45
45
46
46
Args:
47
- mkdocs_config (Config ): Mkdocs website configuration object.
47
+ mkdocs_config (MkDocsConfig ): Mkdocs website configuration object.
48
48
switch_force (bool, optional): option to force integration disabling. Set
49
49
it to False to disable it even if Social Cards are enabled in Mkdocs
50
50
configuration. Defaults to True.
@@ -62,6 +62,7 @@ def __init__(self, mkdocs_config: Config, switch_force: bool = True) -> None:
62
62
self .IS_SOCIAL_PLUGIN_CARDS_ENABLED ,
63
63
]
64
64
)
65
+ self .IS_INSIDERS = self .is_theme_material_insiders ()
65
66
66
67
# except if the end-user wants to disable it
67
68
if switch_force is False :
@@ -75,15 +76,18 @@ def __init__(self, mkdocs_config: Config, switch_force: bool = True) -> None:
75
76
if self .IS_ENABLED :
76
77
self .mkdocs_site_url = mkdocs_config .site_url
77
78
self .mkdocs_site_build_dir = mkdocs_config .site_dir
78
- self .social_cards_assets_dir = self .get_social_cards_dir (
79
+ self .social_cards_assets_dir = self .get_social_cards_build_dir (
80
+ mkdocs_config = mkdocs_config
81
+ )
82
+ self .social_cards_cache_dir = self .get_social_cards_cache_dir (
79
83
mkdocs_config = mkdocs_config
80
84
)
81
85
82
- def is_theme_material (self , mkdocs_config : Config ) -> bool :
86
+ def is_theme_material (self , mkdocs_config : MkDocsConfig ) -> bool :
83
87
"""Check if the theme set in mkdocs.yml is material or not.
84
88
85
89
Args:
86
- mkdocs_config (Config ): Mkdocs website configuration object.
90
+ mkdocs_config (MkDocsConfig ): Mkdocs website configuration object.
87
91
88
92
Returns:
89
93
bool: True if the theme's name is 'material'. False if not.
@@ -108,11 +112,11 @@ def is_theme_material_insiders(self) -> bool | None:
108
112
logger .debug ("Material theme edition COMMUNITY" )
109
113
return False
110
114
111
- def is_social_plugin_enabled_mkdocs (self , mkdocs_config : Config ) -> bool :
115
+ def is_social_plugin_enabled_mkdocs (self , mkdocs_config : MkDocsConfig ) -> bool :
112
116
"""Check if social plugin is installed and enabled.
113
117
114
118
Args:
115
- mkdocs_config (Config ): Mkdocs website configuration object.
119
+ mkdocs_config (MkDocsConfig ): Mkdocs website configuration object.
116
120
117
121
Returns:
118
122
bool: True if the theme material and the plugin social cards is enabled.
@@ -136,11 +140,13 @@ def is_social_plugin_enabled_mkdocs(self, mkdocs_config: Config) -> bool:
136
140
self .IS_SOCIAL_PLUGIN_CARDS_ENABLED = True
137
141
return True
138
142
139
- def is_social_plugin_and_cards_enabled_mkdocs (self , mkdocs_config : Config ) -> bool :
143
+ def is_social_plugin_and_cards_enabled_mkdocs (
144
+ self , mkdocs_config : MkDocsConfig
145
+ ) -> bool :
140
146
"""Check if social cards plugin is enabled.
141
147
142
148
Args:
143
- mkdocs_config (Config ): Mkdocs website configuration object.
149
+ mkdocs_config (MkDocsConfig ): Mkdocs website configuration object.
144
150
145
151
Returns:
146
152
bool: True if the theme material and the plugin social cards is enabled.
@@ -177,12 +183,12 @@ def is_social_plugin_enabled_page(
177
183
"cards" , fallback_value
178
184
)
179
185
180
- def get_social_cards_dir (self , mkdocs_config : Config ) -> str :
186
+ def get_social_cards_build_dir (self , mkdocs_config : MkDocsConfig ) -> str :
181
187
"""Get Social Cards folder within Mkdocs site_dir.
182
188
See: https://squidfunk.github.io/mkdocs-material/plugins/social/#config.cards_dir
183
189
184
190
Args:
185
- mkdocs_config (Config ): Mkdocs website configuration object.
191
+ mkdocs_config (MkDocsConfig ): Mkdocs website configuration object.
186
192
187
193
Returns:
188
194
str: True if the theme material and the plugin social cards is enabled.
@@ -196,18 +202,36 @@ def get_social_cards_dir(self, mkdocs_config: Config) -> str:
196
202
197
203
return social_plugin_cfg .config .cards_dir
198
204
205
+ def get_social_cards_cache_dir (self , mkdocs_config : MkDocsConfig ) -> str :
206
+ """Get Social Cards folder within Mkdocs site_dir.
207
+ See: https://squidfunk.github.io/mkdocs-material/plugins/social/#config.cards_dir
208
+
209
+ Args:
210
+ mkdocs_config (MkDocsConfig): Mkdocs website configuration object.
211
+
212
+ Returns:
213
+ str: True if the theme material and the plugin social cards is enabled.
214
+ """
215
+ social_plugin_cfg = mkdocs_config .plugins .get ("material/social" )
216
+
217
+ logger .debug (
218
+ "Social cards cache folder: " f"{ social_plugin_cfg .config .cache_dir } ."
219
+ )
220
+
221
+ return social_plugin_cfg .config .cache_dir
222
+
199
223
def get_social_card_build_path_for_page (
200
224
self , mkdocs_page : Page , mkdocs_site_dir : str | None = None
201
225
) -> Path :
202
- """Get social card URL for a specific page in documentation .
226
+ """Get social card path in Mkdocs build dir for a specific page.
203
227
204
228
Args:
205
229
mkdocs_page (Page): Mkdocs page object.
206
230
mkdocs_site_dir (Optional[str], optional): Mkdocs build site dir. If None, the
207
231
'class.mkdocs_site_build_dir' is used. is Defaults to None.
208
232
209
233
Returns:
210
- str: URL to the image once published
234
+ str: path to the image once published
211
235
"""
212
236
if mkdocs_site_dir is None and self .mkdocs_site_build_dir :
213
237
mkdocs_site_dir = self .mkdocs_site_build_dir
@@ -217,6 +241,22 @@ def get_social_card_build_path_for_page(
217
241
f"{ Path (mkdocs_page .file .src_uri ).with_suffix ('.png' )} "
218
242
)
219
243
244
+ def get_social_card_cache_path_for_page (self , mkdocs_page : Page ) -> Path :
245
+ """Get social card path in social plugin cache folder for a specific page.
246
+
247
+ Args:
248
+ mkdocs_page (Page): Mkdocs page object.
249
+ social_plugin_cache_dir (Optional[str], optional): Mkdocs build site dir. If None, the
250
+ 'class.mkdocs_site_build_dir' is used. is Defaults to None.
251
+
252
+ Returns:
253
+ str: path to the image once published
254
+ """
255
+ return Path (
256
+ f"{ self .social_cards_assets_dir } /"
257
+ f"{ Path (mkdocs_page .file .src_uri ).with_suffix ('.png' )} "
258
+ )
259
+
220
260
def get_social_card_url_for_page (
221
261
self ,
222
262
mkdocs_page : Page ,
0 commit comments