Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 30 additions & 19 deletions mkdocs_rss_plugin/integrations/theme_material_social_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ def __init__(self, mkdocs_config: MkDocsConfig, switch_force: bool = True) -> No
if self.IS_ENABLED:
self.mkdocs_site_url = mkdocs_config.site_url
self.mkdocs_site_build_dir = mkdocs_config.site_dir
self.social_cards_dir = self.get_social_cards_dir(
mkdocs_config=mkdocs_config
)
self.social_cards_assets_dir = self.get_social_cards_build_dir(
mkdocs_config=mkdocs_config
)
Expand Down Expand Up @@ -205,15 +208,15 @@ def load_cache_cards_manifest(self) -> Optional[dict]:

return self.CARDS_MANIFEST

def get_social_cards_build_dir(self, mkdocs_config: MkDocsConfig) -> Path:
"""Get Social Cards folder within Mkdocs site_dir.
def get_social_cards_dir(self, mkdocs_config: MkDocsConfig) -> str:
"""Get Social Cards folder relative to Mkdocs site_dir.
See: https://squidfunk.github.io/mkdocs-material/plugins/social/#config.cards_dir

Args:
mkdocs_config (MkDocsConfig): Mkdocs website configuration object.

Returns:
str: True if the theme material and the plugin social cards is enabled.
str: The cards_dir if the theme material and the plugin social cards is enabled.
"""
social_plugin_cfg = mkdocs_config.plugins.get("material/social")

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

return Path(social_plugin_cfg.config.cards_dir).resolve()
return social_plugin_cfg.config.cards_dir

def get_social_cards_build_dir(self, mkdocs_config: MkDocsConfig) -> Path:
"""Get Social Cards folder within Mkdocs site_dir.
See: https://squidfunk.github.io/mkdocs-material/plugins/social/#config.cards_dir

Args:
mkdocs_config (MkDocsConfig): Mkdocs website configuration object.

Returns:
Path: Absolute path of the assets dir if the theme material and the plugin
social cards is enabled.
"""
cards_dir = self.get_social_cards_dir(mkdocs_config=mkdocs_config)

return Path(cards_dir).resolve()

def get_social_cards_cache_dir(self, mkdocs_config: MkDocsConfig) -> Path:
"""Get Social Cards folder within Mkdocs site_dir.
Expand All @@ -232,7 +250,7 @@ def get_social_cards_cache_dir(self, mkdocs_config: MkDocsConfig) -> Path:
mkdocs_config (MkDocsConfig): Mkdocs website configuration object.

Returns:
str: True if the theme material and the plugin social cards is enabled.
Path: The cache dir if the theme material and the plugin social cards is enabled.
"""
social_plugin_cfg = mkdocs_config.plugins.get("material/social")
self.social_cards_cache_dir = Path(social_plugin_cfg.config.cache_dir).resolve()
Expand Down Expand Up @@ -372,19 +390,12 @@ def get_social_card_url_for_page(
if mkdocs_site_url is None and self.mkdocs_site_url:
mkdocs_site_url = self.mkdocs_site_url

# if page is a blog post
if (
self.integration_material_blog.IS_BLOG_PLUGIN_ENABLED
and self.integration_material_blog.is_page_a_blog_post(mkdocs_page)
):
page_social_card = (
f"{mkdocs_site_url}assets/images/social/"
f"{Path(mkdocs_page.file.dest_uri).parent}.png"
)
else:
page_social_card = (
f"{mkdocs_site_url}assets/images/social/"
f"{Path(mkdocs_page.file.src_uri).with_suffix('.png')}"
)
Comment on lines -375 to -388
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally this introduced a bug with blog posts social cards.

# As of mkdocs-material 9.6.5, social cards are always stored in the
# matching src path in the build folder, regardless of the page type.
Comment on lines +393 to +394
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion is wrong. To reproduce:

mkdocs build -f ./tests/fixtures/mkdocs_item_image_social_cards_blog.yml --verbose

page_social_card = (
f"{mkdocs_site_url}{self.social_cards_dir}/"
f"{Path(mkdocs_page.file.src_uri).with_suffix('.png')}"
)
logger.debug(f"Use social card url: {page_social_card}")

return page_social_card
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
site_name: Test RSS Plugin with social cards, blog plugin and directory URL disabled
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.
site_url: https://guts.github.io/mkdocs-rss-plugin

use_directory_urls: false

plugins:
- blog:
blog_dir: blog
authors_profiles: true
- rss:
match_path: blog/posts/.*
- social:
enabled: true
cards: true

theme:
name: material
50 changes: 50 additions & 0 deletions tests/test_integrations_material_social_cards.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from mkdocs.config import load_config

# package
from mkdocs_rss_plugin.__about__ import __title_clean__
from mkdocs_rss_plugin.integrations.theme_material_social_plugin import (
IntegrationMaterialSocialCards,
)
Expand Down Expand Up @@ -136,6 +137,55 @@ def test_plugin_config_social_cards_enabled_with_blog_plugin(self):
)
self.assertTrue(integration_social_cards.IS_ENABLED)

def test_plugin_config_social_cards_enabled_with_directory_urls_disabled(self):
"""Test case described in https://github.com/Guts/mkdocs-rss-plugin/issues/319."""
# default reference
cfg_mkdocs = load_config(
str(
Path(
"tests/fixtures/mkdocs_item_image_social_cards_blog_directory_url_disabled.yml"
).resolve()
)
)

integration_social_cards = IntegrationMaterialSocialCards(
mkdocs_config=cfg_mkdocs
)
self.assertTrue(integration_social_cards.IS_THEME_MATERIAL)
self.assertTrue(integration_social_cards.IS_SOCIAL_PLUGIN_ENABLED)
self.assertTrue(integration_social_cards.IS_SOCIAL_PLUGIN_CARDS_ENABLED)
self.assertIsInstance(integration_social_cards.social_cards_dir, str)
self.assertTrue(integration_social_cards.social_cards_cache_dir.is_dir())

with tempfile.TemporaryDirectory(
prefix=f"{__title_clean__.lower()}_", delete=False
) as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path(
"tests/fixtures/mkdocs_item_image_social_cards_blog_directory_url_disabled.yml"
),
output_path=tmpdirname,
strict=False,
)
print(tmpdirname)
if cli_result.exception is not None:
e = cli_result.exception
logger.debug(format_exception(type(e), e, e.__traceback__))

self.assertEqual(cli_result.exit_code, 0)
self.assertIsNone(cli_result.exception)

# created items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
self.assertEqual(feed_parsed.bozo, 0)
for feed_item in feed_parsed.entries:
self.assertTrue(hasattr(feed_item, "enclosures"))

# updated items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_updated.xml")
self.assertEqual(feed_parsed.bozo, 0)

def test_simple_build(self):
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
Expand Down
Loading