From 433c137bc1b091fb2488f3fc863cddd3736ca831 Mon Sep 17 00:00:00 2001 From: squidfunk Date: Fri, 22 Sep 2023 18:12:48 +0200 Subject: [PATCH] Fixed blog plugin not generating paginated views as index pages --- material/plugins/blog/plugin.py | 19 +++++++++++-------- src/plugins/blog/plugin.py | 19 +++++++++++-------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/material/plugins/blog/plugin.py b/material/plugins/blog/plugin.py index 3df2e9124..ef343100b 100644 --- a/material/plugins/blog/plugin.py +++ b/material/plugins/blog/plugin.py @@ -603,17 +603,11 @@ class BlogPlugin(BasePlugin[BlogConfig]): def _generate_pages(self, view: View, config: MkDocsConfig, files: Files): yield view - # Compute base path for pagination - if the given view is an index file, - # we need to pop the file name from the base so it's not part of the URL - base, _ = posixpath.splitext(view.file.src_uri) - if view.file.name == "index": - base = posixpath.dirname(base) - # Compute pagination boundaries and create pages - pages are internally # handled as copies of a view, as they map to the same source location step = self.config.pagination_per_page for at in range(step, len(view.posts), step): - path = self._format_path_for_pagination(base, 1 + at // step) + path = self._format_path_for_pagination(view, 1 + at // step) # Create file for view, if it does not exist file = files.get_file_from_path(path) @@ -784,11 +778,20 @@ class BlogPlugin(BasePlugin[BlogConfig]): return posixpath.join(self.config.blog_dir, f"{path}.md") # Format path for pagination - def _format_path_for_pagination(self, base: str, page: int): + def _format_path_for_pagination(self, view: View, page: int): path = self.config.pagination_url_format.format( page = page ) + # Compute base path for pagination - if the given view is an index file, + # we need to pop the file name from the base so it's not part of the URL + # and we need to append `index` to the path, so the paginated view is + # also an index page - see https://t.ly/71MKF + base, _ = posixpath.splitext(view.file.src_uri) + if view.is_index: + base = posixpath.dirname(base) + path = posixpath.join(path, "index") + # Normalize path and strip slashes at the beginning and end path = posixpath.normpath(path.strip("/")) return posixpath.join(base, f"{path}.md") diff --git a/src/plugins/blog/plugin.py b/src/plugins/blog/plugin.py index 3df2e9124..ef343100b 100644 --- a/src/plugins/blog/plugin.py +++ b/src/plugins/blog/plugin.py @@ -603,17 +603,11 @@ class BlogPlugin(BasePlugin[BlogConfig]): def _generate_pages(self, view: View, config: MkDocsConfig, files: Files): yield view - # Compute base path for pagination - if the given view is an index file, - # we need to pop the file name from the base so it's not part of the URL - base, _ = posixpath.splitext(view.file.src_uri) - if view.file.name == "index": - base = posixpath.dirname(base) - # Compute pagination boundaries and create pages - pages are internally # handled as copies of a view, as they map to the same source location step = self.config.pagination_per_page for at in range(step, len(view.posts), step): - path = self._format_path_for_pagination(base, 1 + at // step) + path = self._format_path_for_pagination(view, 1 + at // step) # Create file for view, if it does not exist file = files.get_file_from_path(path) @@ -784,11 +778,20 @@ class BlogPlugin(BasePlugin[BlogConfig]): return posixpath.join(self.config.blog_dir, f"{path}.md") # Format path for pagination - def _format_path_for_pagination(self, base: str, page: int): + def _format_path_for_pagination(self, view: View, page: int): path = self.config.pagination_url_format.format( page = page ) + # Compute base path for pagination - if the given view is an index file, + # we need to pop the file name from the base so it's not part of the URL + # and we need to append `index` to the path, so the paginated view is + # also an index page - see https://t.ly/71MKF + base, _ = posixpath.splitext(view.file.src_uri) + if view.is_index: + base = posixpath.dirname(base) + path = posixpath.join(path, "index") + # Normalize path and strip slashes at the beginning and end path = posixpath.normpath(path.strip("/")) return posixpath.join(base, f"{path}.md")