Skip to content

Commit 535702c

Browse files
authored
update(chore): bump minimal version of Material for Mkdocs since 9.7.x is the last one (#405)
- since Material for Mkdocs turned into maintenance mode, all insiders features are available, so this PR remove code related to split feature between community/insiders editions - fix #404 - improve integrations with Material theme
2 parents b7bd81b + 7dc6646 commit 535702c

File tree

4 files changed

+54
-81
lines changed

4 files changed

+54
-81
lines changed

mkdocs_rss_plugin/integrations/theme_material_base.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
class IntegrationMaterialThemeBase:
3434
# attributes
3535
IS_THEME_MATERIAL: bool = False
36-
IS_INSIDERS: bool | None = False
3736

3837
def __init__(self, mkdocs_config: MkDocsConfig) -> None:
3938
"""Integration instantiation.
@@ -45,7 +44,6 @@ def __init__(self, mkdocs_config: MkDocsConfig) -> None:
4544
self.mkdocs_config = mkdocs_config
4645

4746
self.IS_THEME_MATERIAL = self.is_mkdocs_theme_material()
48-
self.IS_INSIDERS = self.is_mkdocs_theme_material_insiders()
4947

5048
def is_mkdocs_theme_material(
5149
self, mkdocs_config: MkDocsConfig | None = None
@@ -63,22 +61,3 @@ def is_mkdocs_theme_material(
6361

6462
self.IS_THEME_MATERIAL = mkdocs_config.theme.name == "material"
6563
return self.IS_THEME_MATERIAL
66-
67-
def is_mkdocs_theme_material_insiders(self) -> bool | None:
68-
"""Check if the material theme is community or insiders edition.
69-
70-
Returns:
71-
bool: True if the theme is Insiders edition. False if community. None if
72-
the Material theme is not installed.
73-
"""
74-
if not self.IS_THEME_MATERIAL:
75-
return None
76-
77-
if material_version is not None and "insiders" in material_version:
78-
logger.debug("Material theme edition INSIDERS")
79-
self.IS_INSIDERS = True
80-
return True
81-
else:
82-
logger.debug("Material theme edition COMMUNITY")
83-
self.IS_INSIDERS = False
84-
return False

mkdocs_rss_plugin/integrations/theme_material_blog_plugin.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@
1111
# 3rd party
1212
from mkdocs.config.defaults import MkDocsConfig
1313
from mkdocs.plugins import get_plugin_logger
14-
from mkdocs.structure.pages import Page
1514

1615
# package
1716
from mkdocs_rss_plugin.constants import MKDOCS_LOGGER_NAME
1817
from mkdocs_rss_plugin.integrations.theme_material_base import (
1918
IntegrationMaterialThemeBase,
2019
)
20+
from mkdocs_rss_plugin.models import MkdocsPageSubset
2121

2222
# conditional
2323
try:
2424
from material import __version__ as material_version
2525
from material.plugins.blog.plugin import BlogPlugin
26+
from material.plugins.blog.structure import Post
2627

2728
except ImportError:
2829
material_version = None
@@ -131,7 +132,7 @@ def author_name_from_id(self, author_id: str) -> str:
131132
)
132133
return author_id
133134

134-
def is_page_a_blog_post(self, mkdocs_page: Page) -> bool:
135+
def is_page_a_blog_post(self, mkdocs_page: Post | MkdocsPageSubset) -> bool:
135136
"""Identifies if the given page is part of Material Blog.
136137
137138
Args:
@@ -140,6 +141,18 @@ def is_page_a_blog_post(self, mkdocs_page: Page) -> bool:
140141
Returns:
141142
bool: True if the given page is a Material Blog post.
142143
"""
143-
return Path(mkdocs_page.file.src_uri).is_relative_to(
144-
self.blog_plugin_cfg.config.blog_dir
145-
)
144+
if self.IS_ENABLED and isinstance(mkdocs_page, Post):
145+
logger.info(
146+
f"page '{mkdocs_page.file.src_uri}' identified as Material Blog post."
147+
)
148+
return True
149+
elif isinstance(mkdocs_page, MkdocsPageSubset) and Path(
150+
mkdocs_page.src_uri
151+
).is_relative_to(self.blog_plugin_cfg.config.blog_dir):
152+
logger.info(
153+
f"page '{mkdocs_page.src_uri}' identified as Material Blog post "
154+
f"by src_uri matching."
155+
)
156+
return True
157+
else:
158+
return False

mkdocs_rss_plugin/integrations/theme_material_social_plugin.py

Lines changed: 34 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
# standard library
88
import json
9-
from hashlib import md5
109
from 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,

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ dev = [
5959
docs = [
6060
"mkdocs-git-committers-plugin-2>=2.4.1,<2.6",
6161
"mkdocs-git-revision-date-localized-plugin>=1.3,<1.6",
62-
"mkdocs-material[imaging]>=9.5.47,<10",
62+
"mkdocs-material[imaging]>=9.7,<9.8",
6363
"mkdocs-minify-plugin>=0.8,<0.9",
6464
"mkdocstrings-python>=1.16.2,<1.19",
6565
"termynal>=0.12.2,<0.14",
6666
]
6767
test = [
6868
"feedparser>=6.0.12,<6.1",
6969
"jsonfeed-util>=1.2,<2",
70-
"mkdocs-material[imaging]>=9.6.23",
70+
"mkdocs-material[imaging]>=9.7,<9.8",
7171
"pytest-cov>=6.9.1,<8",
7272
"validator-collection>=1.5,<1.6",
7373
]

0 commit comments

Comments
 (0)