Fixed several errors in blog plugin

This commit is contained in:
squidfunk 2023-08-21 23:01:37 +02:00
parent a313fa9956
commit 7099ea60a9
No known key found for this signature in database
GPG Key ID: 5ED40BC4F9C436DF
6 changed files with 30 additions and 12 deletions

View File

@ -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

View File

@ -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

View File

@ -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:

View File

@ -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

View File

@ -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

View File

@ -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: