Skip to content

Commit 9225d88

Browse files
authored
fix(material_social): use cards_dir to build cards url for page (#354)
This should fix #319 The path building is based on https://github.com/squidfunk/mkdocs-material/blob/396c493f4774fa0bafeada5d0b555d02193557e8/src/plugins/social/plugin.py#L339-L362
2 parents 4373a2a + 0325f3e commit 9225d88

File tree

3 files changed

+98
-19
lines changed

3 files changed

+98
-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,9 @@ 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(
93+
mkdocs_config=mkdocs_config
94+
)
9295
self.social_cards_assets_dir = self.get_social_cards_build_dir(
9396
mkdocs_config=mkdocs_config
9497
)
@@ -205,15 +208,15 @@ def load_cache_cards_manifest(self) -> Optional[dict]:
205208

206209
return self.CARDS_MANIFEST
207210

208-
def get_social_cards_build_dir(self, mkdocs_config: MkDocsConfig) -> Path:
209-
"""Get Social Cards folder within Mkdocs site_dir.
211+
def get_social_cards_dir(self, mkdocs_config: MkDocsConfig) -> str:
212+
"""Get Social Cards folder relative to Mkdocs site_dir.
210213
See: https://squidfunk.github.io/mkdocs-material/plugins/social/#config.cards_dir
211214
212215
Args:
213216
mkdocs_config (MkDocsConfig): Mkdocs website configuration object.
214217
215218
Returns:
216-
str: True if the theme material and the plugin social cards is enabled.
219+
str: The cards_dir if the theme material and the plugin social cards is enabled.
217220
"""
218221
social_plugin_cfg = mkdocs_config.plugins.get("material/social")
219222

@@ -222,7 +225,22 @@ def get_social_cards_build_dir(self, mkdocs_config: MkDocsConfig) -> Path:
222225
f"{social_plugin_cfg.config.cards_dir}."
223226
)
224227

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

227245
def get_social_cards_cache_dir(self, mkdocs_config: MkDocsConfig) -> Path:
228246
"""Get Social Cards folder within Mkdocs site_dir.
@@ -232,7 +250,7 @@ def get_social_cards_cache_dir(self, mkdocs_config: MkDocsConfig) -> Path:
232250
mkdocs_config (MkDocsConfig): Mkdocs website configuration object.
233251
234252
Returns:
235-
str: True if the theme material and the plugin social cards is enabled.
253+
Path: The cache dir if the theme material and the plugin social cards is enabled.
236254
"""
237255
social_plugin_cfg = mkdocs_config.plugins.get("material/social")
238256
self.social_cards_cache_dir = Path(social_plugin_cfg.config.cache_dir).resolve()
@@ -372,19 +390,12 @@ def get_social_card_url_for_page(
372390
if mkdocs_site_url is None and self.mkdocs_site_url:
373391
mkdocs_site_url = self.mkdocs_site_url
374392

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-
)
393+
# As of mkdocs-material 9.6.5, social cards are always stored in the
394+
# matching src path in the build folder, regardless of the page type.
395+
page_social_card = (
396+
f"{mkdocs_site_url}{self.social_cards_dir}/"
397+
f"{Path(mkdocs_page.file.src_uri).with_suffix('.png')}"
398+
)
399+
logger.debug(f"Use social card url: {page_social_card}")
389400

390401
return page_social_card
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
site_name: Test RSS Plugin with social cards, blog plugin and directory URL disabled
2+
site_description: Test RSS with social and blog plugins enabled but directory URLS disabled. Related to https://github.com/Guts/mkdocs-rss-plugin/issues/319.
3+
site_url: https://guts.github.io/mkdocs-rss-plugin
4+
5+
use_directory_urls: false
6+
7+
plugins:
8+
- blog:
9+
blog_dir: blog
10+
authors_profiles: true
11+
- rss:
12+
match_path: blog/posts/.*
13+
- social:
14+
enabled: true
15+
cards: true
16+
17+
theme:
18+
name: material

tests/test_integrations_material_social_cards.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from mkdocs.config import load_config
2626

2727
# package
28+
from mkdocs_rss_plugin.__about__ import __title_clean__
2829
from mkdocs_rss_plugin.integrations.theme_material_social_plugin import (
2930
IntegrationMaterialSocialCards,
3031
)
@@ -136,6 +137,55 @@ def test_plugin_config_social_cards_enabled_with_blog_plugin(self):
136137
)
137138
self.assertTrue(integration_social_cards.IS_ENABLED)
138139

140+
def test_plugin_config_social_cards_enabled_with_directory_urls_disabled(self):
141+
"""Test case described in https://github.com/Guts/mkdocs-rss-plugin/issues/319."""
142+
# default reference
143+
cfg_mkdocs = load_config(
144+
str(
145+
Path(
146+
"tests/fixtures/mkdocs_item_image_social_cards_blog_directory_url_disabled.yml"
147+
).resolve()
148+
)
149+
)
150+
151+
integration_social_cards = IntegrationMaterialSocialCards(
152+
mkdocs_config=cfg_mkdocs
153+
)
154+
self.assertTrue(integration_social_cards.IS_THEME_MATERIAL)
155+
self.assertTrue(integration_social_cards.IS_SOCIAL_PLUGIN_ENABLED)
156+
self.assertTrue(integration_social_cards.IS_SOCIAL_PLUGIN_CARDS_ENABLED)
157+
self.assertIsInstance(integration_social_cards.social_cards_dir, str)
158+
self.assertTrue(integration_social_cards.social_cards_cache_dir.is_dir())
159+
160+
with tempfile.TemporaryDirectory(
161+
prefix=f"{__title_clean__.lower()}_", delete=False
162+
) as tmpdirname:
163+
cli_result = self.build_docs_setup(
164+
testproject_path="docs",
165+
mkdocs_yml_filepath=Path(
166+
"tests/fixtures/mkdocs_item_image_social_cards_blog_directory_url_disabled.yml"
167+
),
168+
output_path=tmpdirname,
169+
strict=False,
170+
)
171+
print(tmpdirname)
172+
if cli_result.exception is not None:
173+
e = cli_result.exception
174+
logger.debug(format_exception(type(e), e, e.__traceback__))
175+
176+
self.assertEqual(cli_result.exit_code, 0)
177+
self.assertIsNone(cli_result.exception)
178+
179+
# created items
180+
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
181+
self.assertEqual(feed_parsed.bozo, 0)
182+
for feed_item in feed_parsed.entries:
183+
self.assertTrue(hasattr(feed_item, "enclosures"))
184+
185+
# updated items
186+
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_updated.xml")
187+
self.assertEqual(feed_parsed.bozo, 0)
188+
139189
def test_simple_build(self):
140190
with tempfile.TemporaryDirectory() as tmpdirname:
141191
cli_result = self.build_docs_setup(

0 commit comments

Comments
 (0)