-
-
Notifications
You must be signed in to change notification settings - Fork 30
fix(material_social): use cards_dir to build cards url for page #354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| ) | ||
|
|
@@ -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") | ||
|
|
||
|
|
@@ -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. | ||
|
|
@@ -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() | ||
|
|
@@ -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')}" | ||
| ) | ||
| # 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
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
18 changes: 18 additions & 0 deletions
18
tests/fixtures/mkdocs_item_image_social_cards_blog_directory_url_disabled.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.