Skip to content

Commit 6dd581e

Browse files
committed
feature(social_cards): add method to determine cache folder
1 parent 377ed83 commit 6dd581e

File tree

1 file changed

+54
-14
lines changed

1 file changed

+54
-14
lines changed

mkdocs_rss_plugin/integrations/theme_material_social_plugin.py

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from pathlib import Path
99

1010
# 3rd party
11-
from mkdocs.config.config_options import Config
11+
from mkdocs.config.defaults import MkDocsConfig
1212
from mkdocs.plugins import get_plugin_logger
1313
from mkdocs.structure.pages import Page
1414

@@ -40,11 +40,11 @@ class IntegrationMaterialSocialCards:
4040
IS_THEME_MATERIAL: bool = False
4141
IS_INSIDERS: bool = False
4242

43-
def __init__(self, mkdocs_config: Config, switch_force: bool = True) -> None:
43+
def __init__(self, mkdocs_config: MkDocsConfig, switch_force: bool = True) -> None:
4444
"""Integration instanciation.
4545
4646
Args:
47-
mkdocs_config (Config): Mkdocs website configuration object.
47+
mkdocs_config (MkDocsConfig): Mkdocs website configuration object.
4848
switch_force (bool, optional): option to force integration disabling. Set
4949
it to False to disable it even if Social Cards are enabled in Mkdocs
5050
configuration. Defaults to True.
@@ -62,6 +62,7 @@ def __init__(self, mkdocs_config: Config, switch_force: bool = True) -> None:
6262
self.IS_SOCIAL_PLUGIN_CARDS_ENABLED,
6363
]
6464
)
65+
self.IS_INSIDERS = self.is_theme_material_insiders()
6566

6667
# except if the end-user wants to disable it
6768
if switch_force is False:
@@ -75,15 +76,18 @@ def __init__(self, mkdocs_config: Config, switch_force: bool = True) -> None:
7576
if self.IS_ENABLED:
7677
self.mkdocs_site_url = mkdocs_config.site_url
7778
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(
7983
mkdocs_config=mkdocs_config
8084
)
8185

82-
def is_theme_material(self, mkdocs_config: Config) -> bool:
86+
def is_theme_material(self, mkdocs_config: MkDocsConfig) -> bool:
8387
"""Check if the theme set in mkdocs.yml is material or not.
8488
8589
Args:
86-
mkdocs_config (Config): Mkdocs website configuration object.
90+
mkdocs_config (MkDocsConfig): Mkdocs website configuration object.
8791
8892
Returns:
8993
bool: True if the theme's name is 'material'. False if not.
@@ -108,11 +112,11 @@ def is_theme_material_insiders(self) -> bool | None:
108112
logger.debug("Material theme edition COMMUNITY")
109113
return False
110114

111-
def is_social_plugin_enabled_mkdocs(self, mkdocs_config: Config) -> bool:
115+
def is_social_plugin_enabled_mkdocs(self, mkdocs_config: MkDocsConfig) -> bool:
112116
"""Check if social plugin is installed and enabled.
113117
114118
Args:
115-
mkdocs_config (Config): Mkdocs website configuration object.
119+
mkdocs_config (MkDocsConfig): Mkdocs website configuration object.
116120
117121
Returns:
118122
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:
136140
self.IS_SOCIAL_PLUGIN_CARDS_ENABLED = True
137141
return True
138142

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:
140146
"""Check if social cards plugin is enabled.
141147
142148
Args:
143-
mkdocs_config (Config): Mkdocs website configuration object.
149+
mkdocs_config (MkDocsConfig): Mkdocs website configuration object.
144150
145151
Returns:
146152
bool: True if the theme material and the plugin social cards is enabled.
@@ -177,12 +183,12 @@ def is_social_plugin_enabled_page(
177183
"cards", fallback_value
178184
)
179185

180-
def get_social_cards_dir(self, mkdocs_config: Config) -> str:
186+
def get_social_cards_build_dir(self, mkdocs_config: MkDocsConfig) -> str:
181187
"""Get Social Cards folder within Mkdocs site_dir.
182188
See: https://squidfunk.github.io/mkdocs-material/plugins/social/#config.cards_dir
183189
184190
Args:
185-
mkdocs_config (Config): Mkdocs website configuration object.
191+
mkdocs_config (MkDocsConfig): Mkdocs website configuration object.
186192
187193
Returns:
188194
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:
196202

197203
return social_plugin_cfg.config.cards_dir
198204

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+
199223
def get_social_card_build_path_for_page(
200224
self, mkdocs_page: Page, mkdocs_site_dir: str | None = None
201225
) -> 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.
203227
204228
Args:
205229
mkdocs_page (Page): Mkdocs page object.
206230
mkdocs_site_dir (Optional[str], optional): Mkdocs build site dir. If None, the
207231
'class.mkdocs_site_build_dir' is used. is Defaults to None.
208232
209233
Returns:
210-
str: URL to the image once published
234+
str: path to the image once published
211235
"""
212236
if mkdocs_site_dir is None and self.mkdocs_site_build_dir:
213237
mkdocs_site_dir = self.mkdocs_site_build_dir
@@ -217,6 +241,22 @@ def get_social_card_build_path_for_page(
217241
f"{Path(mkdocs_page.file.src_uri).with_suffix('.png')}"
218242
)
219243

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+
220260
def get_social_card_url_for_page(
221261
self,
222262
mkdocs_page: Page,

0 commit comments

Comments
 (0)