From 7099ea60a9f6894d95c88c2e94db72f5474f502e Mon Sep 17 00:00:00 2001 From: squidfunk Date: Mon, 21 Aug 2023 23:01:37 +0200 Subject: [PATCH] Fixed several errors in blog plugin --- material/plugins/blog/plugin.py | 13 ++++++++++--- material/plugins/blog/structure/__init__.py | 2 +- material/plugins/blog/structure/options.py | 6 ++++-- src/plugins/blog/plugin.py | 13 ++++++++++--- src/plugins/blog/structure/__init__.py | 2 +- src/plugins/blog/structure/options.py | 6 ++++-- 6 files changed, 30 insertions(+), 12 deletions(-) diff --git a/material/plugins/blog/plugin.py b/material/plugins/blog/plugin.py index 2d156390f..b8c53c821 100644 --- a/material/plugins/blog/plugin.py +++ b/material/plugins/blog/plugin.py @@ -326,7 +326,7 @@ class BlogPlugin(BasePlugin[BlogConfig]): # and must be explicitly enabled by the author. if not isinstance(post.config.draft, bool): if self.config.draft_if_future_date: - return post.config.date > datetime.now() + return post.config.date.created > datetime.now() # Post might be a draft return bool(post.config.draft) @@ -364,7 +364,7 @@ class BlogPlugin(BasePlugin[BlogConfig]): ]) # Update entrypoint in navigation - for items in [view.parent.children, nav.pages]: + for items in [self._resolve_siblings(view.parent, nav), nav.pages]: items[items.index(page)] = view # Return view @@ -484,6 +484,13 @@ class BlogPlugin(BasePlugin[BlogConfig]): assert isinstance(page, View) yield page + # Resolve siblings of a navigation item + def _resolve_siblings(self, item: StructureItem, nav: Navigation): + if isinstance(item, Section): + return item.children + else: + return nav.items + # ------------------------------------------------------------------------- # Attach a list of pages to each other and to the given parent item without @@ -505,7 +512,7 @@ class BlogPlugin(BasePlugin[BlogConfig]): # the top-level navigation, if no parent section was given. Note, that # it's currently not possible to chose the position of a section, but # we might add support for this in the future. - items = parent.children if parent else nav.items + items = self._resolve_siblings(parent, nav) items.append(section) # Find last sibling that is a page, skipping sections, as we need to diff --git a/material/plugins/blog/structure/__init__.py b/material/plugins/blog/structure/__init__.py index 6003cadd3..725c9bc66 100644 --- a/material/plugins/blog/structure/__init__.py +++ b/material/plugins/blog/structure/__init__.py @@ -245,7 +245,7 @@ def _patch(config: MkDocsConfig): config.validation = copy(config.validation) config.validation.links = copy(config.validation.links) config.mdx_configs = copy(config.mdx_configs) - config.mdx_configs["toc"] = copy(config.mdx_configs["toc"]) + config.mdx_configs["toc"] = copy(config.mdx_configs.get("toc", {})) # In order to render excerpts for posts, we need to make sure that the # table of contents extension is appropriately configured diff --git a/material/plugins/blog/structure/options.py b/material/plugins/blog/structure/options.py index 0b36d8cfb..d37779185 100644 --- a/material/plugins/blog/structure/options.py +++ b/material/plugins/blog/structure/options.py @@ -60,12 +60,14 @@ class PostDate(BaseConfigOption[DateDict]): if not isinstance(config[key_name], dict): config[key_name] = { "created": config[key_name] } - # Initialize date dictionary and convert all date values to datetime - config[key_name] = DateDict(config[key_name]) + # Convert all date values to datetime for key, value in config[key_name].items(): if isinstance(value, date): config[key_name][key] = datetime.combine(value, time()) + # Initialize date dictionary + config[key_name] = DateDict(config[key_name]) + # Ensure each date value is of type datetime def run_validation(self, value: DateDict): for key in value: diff --git a/src/plugins/blog/plugin.py b/src/plugins/blog/plugin.py index 2d156390f..b8c53c821 100644 --- a/src/plugins/blog/plugin.py +++ b/src/plugins/blog/plugin.py @@ -326,7 +326,7 @@ class BlogPlugin(BasePlugin[BlogConfig]): # and must be explicitly enabled by the author. if not isinstance(post.config.draft, bool): if self.config.draft_if_future_date: - return post.config.date > datetime.now() + return post.config.date.created > datetime.now() # Post might be a draft return bool(post.config.draft) @@ -364,7 +364,7 @@ class BlogPlugin(BasePlugin[BlogConfig]): ]) # Update entrypoint in navigation - for items in [view.parent.children, nav.pages]: + for items in [self._resolve_siblings(view.parent, nav), nav.pages]: items[items.index(page)] = view # Return view @@ -484,6 +484,13 @@ class BlogPlugin(BasePlugin[BlogConfig]): assert isinstance(page, View) yield page + # Resolve siblings of a navigation item + def _resolve_siblings(self, item: StructureItem, nav: Navigation): + if isinstance(item, Section): + return item.children + else: + return nav.items + # ------------------------------------------------------------------------- # Attach a list of pages to each other and to the given parent item without @@ -505,7 +512,7 @@ class BlogPlugin(BasePlugin[BlogConfig]): # the top-level navigation, if no parent section was given. Note, that # it's currently not possible to chose the position of a section, but # we might add support for this in the future. - items = parent.children if parent else nav.items + items = self._resolve_siblings(parent, nav) items.append(section) # Find last sibling that is a page, skipping sections, as we need to diff --git a/src/plugins/blog/structure/__init__.py b/src/plugins/blog/structure/__init__.py index 6003cadd3..725c9bc66 100644 --- a/src/plugins/blog/structure/__init__.py +++ b/src/plugins/blog/structure/__init__.py @@ -245,7 +245,7 @@ def _patch(config: MkDocsConfig): config.validation = copy(config.validation) config.validation.links = copy(config.validation.links) config.mdx_configs = copy(config.mdx_configs) - config.mdx_configs["toc"] = copy(config.mdx_configs["toc"]) + config.mdx_configs["toc"] = copy(config.mdx_configs.get("toc", {})) # In order to render excerpts for posts, we need to make sure that the # table of contents extension is appropriately configured diff --git a/src/plugins/blog/structure/options.py b/src/plugins/blog/structure/options.py index 0b36d8cfb..d37779185 100644 --- a/src/plugins/blog/structure/options.py +++ b/src/plugins/blog/structure/options.py @@ -60,12 +60,14 @@ class PostDate(BaseConfigOption[DateDict]): if not isinstance(config[key_name], dict): config[key_name] = { "created": config[key_name] } - # Initialize date dictionary and convert all date values to datetime - config[key_name] = DateDict(config[key_name]) + # Convert all date values to datetime for key, value in config[key_name].items(): if isinstance(value, date): config[key_name][key] = datetime.combine(value, time()) + # Initialize date dictionary + config[key_name] = DateDict(config[key_name]) + # Ensure each date value is of type datetime def run_validation(self, value: DateDict): for key in value: