66
77# standard library
88import json
9- from hashlib import md5
109from pathlib import Path
1110
1211# 3rd party
@@ -98,8 +97,7 @@ def __init__(self, mkdocs_config: MkDocsConfig, switch_force: bool = True) -> No
9897 mkdocs_config = mkdocs_config
9998 )
10099
101- if self .is_mkdocs_theme_material_insiders ():
102- self .load_cache_cards_manifest ()
100+ self .load_cache_cards_manifest ()
103101
104102 # store some attributes used to compute social card hash
105103 self .site_name = mkdocs_config .site_name
@@ -252,10 +250,26 @@ def get_social_cards_cache_dir(self, mkdocs_config: MkDocsConfig) -> Path:
252250 Path: The cache dir if the theme material and the plugin social cards is enabled.
253251 """
254252 social_plugin_cfg = mkdocs_config .plugins .get ("material/social" )
255- self .social_cards_cache_dir = Path (social_plugin_cfg .config .cache_dir ).resolve ()
253+
254+ if (
255+ Path (social_plugin_cfg .config .cache_dir )
256+ .resolve ()
257+ .is_relative_to (Path (mkdocs_config .config_file_path ).parent .resolve ())
258+ ):
259+ self .social_cards_cache_dir = Path (
260+ social_plugin_cfg .config .cache_dir
261+ ).resolve ()
262+ else :
263+ self .social_cards_cache_dir = (
264+ Path (mkdocs_config .config_file_path )
265+ .parent .resolve ()
266+ .joinpath (social_plugin_cfg .config .cache_dir )
267+ )
256268
257269 logger .debug (
258- "Material Social cards cache folder: " f"{ self .social_cards_cache_dir } ."
270+ "Material Social cards cache folder: "
271+ f"{ self .social_cards_cache_dir } . "
272+ f"Already exists: { self .social_cards_cache_dir .is_dir ()} "
259273 )
260274
261275 return self .social_cards_cache_dir
@@ -307,61 +321,27 @@ def get_social_card_cache_path_for_page(
307321 ) -> Path | None :
308322 """Get social card path in social plugin cache folder for a specific page.
309323
310- Note:
311- As we write this code (June 2024), the cache mechanism in Insiders edition
312- has stores images directly with the corresponding Page's path and name and
313- keep a correspondance matrix with hashes in a manifest.json;
314- the cache mechanism in Community edition uses the hash as file names without
315- any exposed matching criteria.
324+ The cache mechanism in stores images directly with the
325+ corresponding Page's path and name and keep a correspondance matrix with hashes
326+ in a manifest.json.
316327
317328 Args:
318329 mkdocs_page: Mkdocs page object.
319330
320331 Returns:
321332 path to the image in local cache folder if it exists
322333 """
323- if self .IS_INSIDERS :
324-
325- # if page is a blog post
326- if (
327- self .integration_material_blog .IS_BLOG_PLUGIN_ENABLED
328- and self .integration_material_blog .is_page_a_blog_post (mkdocs_page )
329- ):
330- expected_cached_card_path = self .social_cards_cache_dir .joinpath (
331- f"assets/images/social/{ Path (mkdocs_page .dest_uri ).parent } .png"
332- )
333- else :
334- expected_cached_card_path = self .social_cards_cache_dir .joinpath (
335- f"assets/images/social/{ Path (mkdocs_page .src_uri ).with_suffix ('.png' )} "
336- )
337-
338- if expected_cached_card_path .is_file ():
339- logger .debug (
340- f"Social card file found in cache folder: { expected_cached_card_path } "
341- )
342- return expected_cached_card_path
343- else :
344- logger .debug (
345- f"Social card not found in cache folder: { expected_cached_card_path } "
346- )
347-
348- else :
349- if "description" in mkdocs_page .meta :
350- description = mkdocs_page .meta ["description" ]
351- else :
352- description = self .site_description
353-
354- page_hash = md5 (
355- "" .join (
356- [
357- self .site_name ,
358- str (mkdocs_page .meta .get ("title" , mkdocs_page .title )),
359- description ,
360- ]
361- ).encode ("utf-8" )
334+ # if page is a blog post
335+ if (
336+ self .integration_material_blog .IS_BLOG_PLUGIN_ENABLED
337+ and self .integration_material_blog .is_page_a_blog_post (mkdocs_page )
338+ ):
339+ expected_cached_card_path = self .social_cards_cache_dir .joinpath (
340+ f"assets/images/social/{ Path (mkdocs_page .dest_uri ).parent } .png"
362341 )
342+ else :
363343 expected_cached_card_path = self .social_cards_cache_dir .joinpath (
364- f"{ page_hash . hexdigest () } . png"
344+ f"assets/images/social/ { Path ( mkdocs_page . src_uri ). with_suffix ( '. png' ) } "
365345 )
366346
367347 if expected_cached_card_path .is_file ():
@@ -370,8 +350,9 @@ def get_social_card_cache_path_for_page(
370350 )
371351 return expected_cached_card_path
372352 else :
373- logger .debug (f"Not found: { expected_cached_card_path } " )
374- return None
353+ logger .debug (
354+ f"Social card not found in cache folder: { expected_cached_card_path } "
355+ )
375356
376357 def get_social_card_url_for_page (
377358 self ,
0 commit comments