Skip to content

Commit 571701e

Browse files
committed
fix(material_social): use cards_dir to build cards url for page
1 parent c2525f5 commit 571701e

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

mkdocs_rss_plugin/integrations/theme_material_social_plugin.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ def __init__(self, mkdocs_config: MkDocsConfig, switch_force: bool = True) -> No
8989
if self.IS_ENABLED:
9090
self.mkdocs_site_url = mkdocs_config.site_url
9191
self.mkdocs_site_build_dir = mkdocs_config.site_dir
92+
self.social_cards_dir = self.get_social_cards_dir(mkdocs_config=mkdocs_config)
9293
self.social_cards_assets_dir = self.get_social_cards_build_dir(
9394
mkdocs_config=mkdocs_config
9495
)
@@ -205,15 +206,15 @@ def load_cache_cards_manifest(self) -> Optional[dict]:
205206

206207
return self.CARDS_MANIFEST
207208

208-
def get_social_cards_build_dir(self, mkdocs_config: MkDocsConfig) -> Path:
209-
"""Get Social Cards folder within Mkdocs site_dir.
209+
def get_social_cards_dir(self, mkdocs_config: MkDocsConfig) -> str:
210+
"""Get Social Cards folder relative to Mkdocs site_dir.
210211
See: https://squidfunk.github.io/mkdocs-material/plugins/social/#config.cards_dir
211212
212213
Args:
213214
mkdocs_config (MkDocsConfig): Mkdocs website configuration object.
214215
215216
Returns:
216-
str: True if the theme material and the plugin social cards is enabled.
217+
str: The cards_dir if the theme material and the plugin social cards is enabled.
217218
"""
218219
social_plugin_cfg = mkdocs_config.plugins.get("material/social")
219220

@@ -222,7 +223,22 @@ def get_social_cards_build_dir(self, mkdocs_config: MkDocsConfig) -> Path:
222223
f"{social_plugin_cfg.config.cards_dir}."
223224
)
224225

225-
return Path(social_plugin_cfg.config.cards_dir).resolve()
226+
return social_plugin_cfg.config.cards_dir
227+
228+
def get_social_cards_build_dir(self, mkdocs_config: MkDocsConfig) -> Path:
229+
"""Get Social Cards folder within Mkdocs site_dir.
230+
See: https://squidfunk.github.io/mkdocs-material/plugins/social/#config.cards_dir
231+
232+
Args:
233+
mkdocs_config (MkDocsConfig): Mkdocs website configuration object.
234+
235+
Returns:
236+
Path: Absolute path of the assets dir if the theme material and the plugin
237+
social cards is enabled.
238+
"""
239+
cards_dir = self.get_social_cards_dir(mkdocs_config=mkdocs_config)
240+
241+
return Path(cards_dir).resolve()
226242

227243
def get_social_cards_cache_dir(self, mkdocs_config: MkDocsConfig) -> Path:
228244
"""Get Social Cards folder within Mkdocs site_dir.
@@ -232,7 +248,7 @@ def get_social_cards_cache_dir(self, mkdocs_config: MkDocsConfig) -> Path:
232248
mkdocs_config (MkDocsConfig): Mkdocs website configuration object.
233249
234250
Returns:
235-
str: True if the theme material and the plugin social cards is enabled.
251+
Path: The cache dir if the theme material and the plugin social cards is enabled.
236252
"""
237253
social_plugin_cfg = mkdocs_config.plugins.get("material/social")
238254
self.social_cards_cache_dir = Path(social_plugin_cfg.config.cache_dir).resolve()
@@ -372,19 +388,14 @@ def get_social_card_url_for_page(
372388
if mkdocs_site_url is None and self.mkdocs_site_url:
373389
mkdocs_site_url = self.mkdocs_site_url
374390

375-
# if page is a blog post
376-
if (
377-
self.integration_material_blog.IS_BLOG_PLUGIN_ENABLED
378-
and self.integration_material_blog.is_page_a_blog_post(mkdocs_page)
379-
):
380-
page_social_card = (
381-
f"{mkdocs_site_url}assets/images/social/"
382-
f"{Path(mkdocs_page.file.dest_uri).parent}.png"
383-
)
384-
else:
385-
page_social_card = (
386-
f"{mkdocs_site_url}assets/images/social/"
387-
f"{Path(mkdocs_page.file.src_uri).with_suffix('.png')}"
388-
)
391+
# As of mkdocs-material 9.6.5, social cards are always stored in the
392+
# matching src path in the build folder, regardless of the page type.
393+
page_social_card = (
394+
f"{mkdocs_site_url}{self.social_cards_dir}/"
395+
f"{Path(mkdocs_page.file.src_uri).with_suffix('.png')}"
396+
)
397+
logger.debug(
398+
f"Use social card url: {page_social_card}"
399+
)
389400

390401
return page_social_card

0 commit comments

Comments
 (0)