diff --git a/mkdocs_rss_plugin/plugin.py b/mkdocs_rss_plugin/plugin.py index b672e11..c4ab87f 100644 --- a/mkdocs_rss_plugin/plugin.py +++ b/mkdocs_rss_plugin/plugin.py @@ -11,7 +11,7 @@ from email.utils import formatdate from pathlib import Path from re import compile as re_compile -from typing import List, Optional +from typing import List, Literal, Optional # 3rd party from jinja2 import Environment, FileSystemLoader, select_autoescape @@ -55,9 +55,30 @@ class GitRssPlugin(BasePlugin[RssPluginConfig]): # allow to set the plugin multiple times in the same mkdocs config supports_multiple_instances = True - def __init__(self): + def __init__(self, *args, **kwargs): """Instantiation.""" # pages storage + super().__init__(*args, **kwargs) + + self.cmd_is_serve: bool = False + + def on_startup( + self, *, command: Literal["build", "gh-deploy", "serve"], dirty: bool + ) -> None: + """The `startup` event runs once at the very beginning of an `mkdocs` invocation. + Note that for initializing variables, the __init__ method is still preferred. + For initializing per-build variables (and whenever in doubt), use the + on_config event. + + See: https://www.mkdocs.org/user-guide/plugins/#on_startup + + Args: + command: the command that MkDocs was invoked with, e.g. "serve" for `mkdocs serve`. + dirty: whether `--dirty` flag was passed. + """ + # flag used command to disable some actions if serve is used + self.cmd_is_serve = command == "serve" + self.pages_to_filter: List[PageInformation] = [] # prepare output feeds self.feed_created: dict = {} @@ -108,6 +129,7 @@ def on_config(self, config: MkDocsConfig) -> MkDocsConfig: cache_dir=self.cache_dir, use_git=self.config.use_git, integration_material_social_cards=self.integration_material_social_cards, + mkdocs_command_is_on_serve=self.cmd_is_serve, ) # check template dirs diff --git a/mkdocs_rss_plugin/util.py b/mkdocs_rss_plugin/util.py index dc95109..d34e7bc 100644 --- a/mkdocs_rss_plugin/util.py +++ b/mkdocs_rss_plugin/util.py @@ -72,12 +72,13 @@ class Util: def __init__( self, - path: str = ".", cache_dir: Path = DEFAULT_CACHE_FOLDER, - use_git: bool = True, integration_material_social_cards: Optional[ IntegrationMaterialSocialCards ] = None, + mkdocs_command_is_on_serve: bool = False, + path: str = ".", + use_git: bool = True, ): """Class hosting the plugin logic. @@ -87,6 +88,13 @@ def __init__( integration_material_social_cards (bool, optional): option to enable integration with Social Cards plugin from Material theme. Defaults to True. """ + self.mkdocs_command_is_on_serve = mkdocs_command_is_on_serve + if self.mkdocs_command_is_on_serve: + logger.debug( + "Mkdocs serve - Fetching remote images length is disabled to avoid " + "HTTP errors." + ) + if use_git: logger.debug("Git use is enabled.") try: @@ -654,6 +662,9 @@ def get_remote_image_length( Returns: int | None: image length as int or None """ + if self.mkdocs_command_is_on_serve: + return None + # first, try HEAD request to avoid downloading the image try: attempt += 1