From 3f87fe0f5bdf7ebf26b1e17dc7d5cd3b3222a45e Mon Sep 17 00:00:00 2001 From: squidfunk Date: Sun, 3 Sep 2023 09:29:40 +0200 Subject: [PATCH] Fixed linking of blog posts in navigation (temporary hack) --- material/plugins/blog/plugin.py | 20 ++++++++++++++++---- src/plugins/blog/plugin.py | 20 ++++++++++++++++---- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/material/plugins/blog/plugin.py b/material/plugins/blog/plugin.py index 08079108e..f7e9d0a50 100644 --- a/material/plugins/blog/plugin.py +++ b/material/plugins/blog/plugin.py @@ -174,10 +174,22 @@ class BlogPlugin(BasePlugin[BlogConfig]): page = self.blog.file.page self._attach_at(page.parent, page, self.blog) - # Update entrypoint in navigation (also part of the hack above) - self.blog.file.page = self.blog - for items in [self._resolve_siblings(self.blog, nav), nav.pages]: - items[items.index(page)] = self.blog + # Hack: update pages instances in navigation - this can also be removed + # once an already open pull request is merged - see https://t.ly/9C_Kz + for page in [self.blog, *self.blog.posts, *self.blog.views]: + assert isinstance(page, Page) + + # Check if the page that we generated is identical to the page that + # is associated with the file - if it is, we're good + temp = page.file.page + if not temp or temp == page: + continue + + # If not, MkDocs overwrote our page with a new instance, which we + # need to replace with the one we generated + page.file.page = page + for items in [self._resolve_siblings(page, nav), nav.pages]: + items[items.index(temp)] = page # Attach posts to entrypoint without adding them to the navigation, so # that the entrypoint is considered to be the active page for each post diff --git a/src/plugins/blog/plugin.py b/src/plugins/blog/plugin.py index 08079108e..f7e9d0a50 100644 --- a/src/plugins/blog/plugin.py +++ b/src/plugins/blog/plugin.py @@ -174,10 +174,22 @@ class BlogPlugin(BasePlugin[BlogConfig]): page = self.blog.file.page self._attach_at(page.parent, page, self.blog) - # Update entrypoint in navigation (also part of the hack above) - self.blog.file.page = self.blog - for items in [self._resolve_siblings(self.blog, nav), nav.pages]: - items[items.index(page)] = self.blog + # Hack: update pages instances in navigation - this can also be removed + # once an already open pull request is merged - see https://t.ly/9C_Kz + for page in [self.blog, *self.blog.posts, *self.blog.views]: + assert isinstance(page, Page) + + # Check if the page that we generated is identical to the page that + # is associated with the file - if it is, we're good + temp = page.file.page + if not temp or temp == page: + continue + + # If not, MkDocs overwrote our page with a new instance, which we + # need to replace with the one we generated + page.file.page = page + for items in [self._resolve_siblings(page, nav), nav.pages]: + items[items.index(temp)] = page # Attach posts to entrypoint without adding them to the navigation, so # that the entrypoint is considered to be the active page for each post