Fixed blog plugin not generating paginated views as index pages

This commit is contained in:
squidfunk 2023-09-22 18:12:48 +02:00
parent 7d0f759cfb
commit 433c137bc1
No known key found for this signature in database
GPG Key ID: 5ED40BC4F9C436DF
2 changed files with 22 additions and 16 deletions

View File

@ -603,17 +603,11 @@ class BlogPlugin(BasePlugin[BlogConfig]):
def _generate_pages(self, view: View, config: MkDocsConfig, files: Files): def _generate_pages(self, view: View, config: MkDocsConfig, files: Files):
yield view 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 # Compute pagination boundaries and create pages - pages are internally
# handled as copies of a view, as they map to the same source location # handled as copies of a view, as they map to the same source location
step = self.config.pagination_per_page step = self.config.pagination_per_page
for at in range(step, len(view.posts), step): 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 # Create file for view, if it does not exist
file = files.get_file_from_path(path) 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") return posixpath.join(self.config.blog_dir, f"{path}.md")
# Format path for pagination # 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( path = self.config.pagination_url_format.format(
page = page 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 # Normalize path and strip slashes at the beginning and end
path = posixpath.normpath(path.strip("/")) path = posixpath.normpath(path.strip("/"))
return posixpath.join(base, f"{path}.md") return posixpath.join(base, f"{path}.md")

View File

@ -603,17 +603,11 @@ class BlogPlugin(BasePlugin[BlogConfig]):
def _generate_pages(self, view: View, config: MkDocsConfig, files: Files): def _generate_pages(self, view: View, config: MkDocsConfig, files: Files):
yield view 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 # Compute pagination boundaries and create pages - pages are internally
# handled as copies of a view, as they map to the same source location # handled as copies of a view, as they map to the same source location
step = self.config.pagination_per_page step = self.config.pagination_per_page
for at in range(step, len(view.posts), step): 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 # Create file for view, if it does not exist
file = files.get_file_from_path(path) 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") return posixpath.join(self.config.blog_dir, f"{path}.md")
# Format path for pagination # 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( path = self.config.pagination_url_format.format(
page = page 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 # Normalize path and strip slashes at the beginning and end
path = posixpath.normpath(path.strip("/")) path = posixpath.normpath(path.strip("/"))
return posixpath.join(base, f"{path}.md") return posixpath.join(base, f"{path}.md")