Skip to content

Commit 62d4a16

Browse files
committed
Fixed edge cases for standalone case when using blog plugin
1 parent a0cb4a7 commit 62d4a16

File tree

2 files changed

+50
-52
lines changed

2 files changed

+50
-52
lines changed

material/plugins/blog/plugin.py

+25-26
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def on_nav(self, nav, *, config, files):
161161

162162
# Attach and link views for archive
163163
title = self._translate(self.config.archive_name, config)
164-
self._attach_to(self.blog.parent, Section(title, views), nav)
164+
self._attach_to(self.blog, Section(title, views), nav)
165165

166166
# Generate and attach views for categories
167167
if self.config.categories:
@@ -170,7 +170,7 @@ def on_nav(self, nav, *, config, files):
170170

171171
# Attach and link views for categories
172172
title = self._translate(self.config.categories_name, config)
173-
self._attach_to(self.blog.parent, Section(title, views), nav)
173+
self._attach_to(self.blog, Section(title, views), nav)
174174

175175
# Paginate generated views, if enabled
176176
if self.config.pagination:
@@ -280,20 +280,19 @@ def on_page_context(self, context, *, page, config, nav):
280280
main = page.parent
281281

282282
# If this page is a view, and the parent page is a view as well, we got
283-
# a paginated view and need to update the parent view in the navigation.
284-
# Paginated views are always rendered last, which is why we can safely
285-
# mutate the navigation at this point
283+
# a paginated view and need to replace the parent with the current view.
284+
# Paginated views are always rendered at the end of the build, which is
285+
# why we can safely mutate the navigation at this point
286286
if isinstance(main, View):
287-
assert isinstance(main.parent, Section)
288-
289-
# Replace view in navigation and rewire view - the current view in
290-
# the navigation becomes the main view, thus the entire chain moves
291-
# one level up. It's essential that the rendering order is linear,
292-
# or else we might end up with a broken navigation.
293-
at = main.parent.children.index(main)
294-
main.parent.children[at] = page
295287
page.parent = main.parent
296288

289+
# Replace view in navigation and rewire it - the current view in the
290+
# navigation becomes the main view, thus the entire chain moves one
291+
# level up. It's essential that the rendering order is linear, or
292+
# else we might end up with a broken navigation.
293+
items = self._resolve_siblings(main, nav)
294+
items[items.index(main)] = page
295+
297296
# Render excerpts and perpare pagination
298297
posts, pagination = self._render(page)
299298

@@ -364,7 +363,7 @@ def _resolve(self, files: Files, config: MkDocsConfig, nav: Navigation):
364363
])
365364

366365
# Update entrypoint in navigation
367-
for items in [self._resolve_items(view.parent, nav), nav.pages]:
366+
for items in [self._resolve_siblings(view, nav), nav.pages]:
368367
items[items.index(page)] = view
369368

370369
# Return view
@@ -484,10 +483,10 @@ def _resolve_views(self, view: View):
484483
assert isinstance(page, View)
485484
yield page
486485

487-
# Resolve children of a navigation item
488-
def _resolve_items(self, item: StructureItem, nav: Navigation):
489-
if isinstance(item, Section):
490-
return item.children
486+
# Resolve siblings of a navigation item
487+
def _resolve_siblings(self, item: StructureItem, nav: Navigation):
488+
if isinstance(item.parent, Section):
489+
return item.parent.children
491490
else:
492491
return nav.items
493492

@@ -505,14 +504,14 @@ def _attach(self, parent: StructureItem, pages: list[Page]):
505504

506505
# Attach a section to the given parent section, make sure it's pages are
507506
# part of the navigation, and ensure all pages are linked correctly
508-
def _attach_to(self, parent: Section, section: Section, nav: Navigation):
509-
section.parent = parent
510-
511-
# Determine the parent section to attach the section to, which might be
512-
# the top-level navigation, if no parent section was given. Note, that
513-
# it's currently not possible to chose the position of a section, but
514-
# we might add support for this in the future.
515-
items = self._resolve_items(parent, nav)
507+
def _attach_to(self, view: View, section: Section, nav: Navigation):
508+
section.parent = view.parent
509+
510+
# Resolve siblings, which are the children of the parent section, or
511+
# the top-level list of navigation items if the view is at the root of
512+
# the project, and append the given section to it. It's currently not
513+
# possible to chose the position of a section.
514+
items = self._resolve_siblings(view, nav)
516515
items.append(section)
517516

518517
# Find last sibling that is a page, skipping sections, as we need to

src/plugins/blog/plugin.py

+25-26
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def on_nav(self, nav, *, config, files):
161161

162162
# Attach and link views for archive
163163
title = self._translate(self.config.archive_name, config)
164-
self._attach_to(self.blog.parent, Section(title, views), nav)
164+
self._attach_to(self.blog, Section(title, views), nav)
165165

166166
# Generate and attach views for categories
167167
if self.config.categories:
@@ -170,7 +170,7 @@ def on_nav(self, nav, *, config, files):
170170

171171
# Attach and link views for categories
172172
title = self._translate(self.config.categories_name, config)
173-
self._attach_to(self.blog.parent, Section(title, views), nav)
173+
self._attach_to(self.blog, Section(title, views), nav)
174174

175175
# Paginate generated views, if enabled
176176
if self.config.pagination:
@@ -280,20 +280,19 @@ def on_page_context(self, context, *, page, config, nav):
280280
main = page.parent
281281

282282
# If this page is a view, and the parent page is a view as well, we got
283-
# a paginated view and need to update the parent view in the navigation.
284-
# Paginated views are always rendered last, which is why we can safely
285-
# mutate the navigation at this point
283+
# a paginated view and need to replace the parent with the current view.
284+
# Paginated views are always rendered at the end of the build, which is
285+
# why we can safely mutate the navigation at this point
286286
if isinstance(main, View):
287-
assert isinstance(main.parent, Section)
288-
289-
# Replace view in navigation and rewire view - the current view in
290-
# the navigation becomes the main view, thus the entire chain moves
291-
# one level up. It's essential that the rendering order is linear,
292-
# or else we might end up with a broken navigation.
293-
at = main.parent.children.index(main)
294-
main.parent.children[at] = page
295287
page.parent = main.parent
296288

289+
# Replace view in navigation and rewire it - the current view in the
290+
# navigation becomes the main view, thus the entire chain moves one
291+
# level up. It's essential that the rendering order is linear, or
292+
# else we might end up with a broken navigation.
293+
items = self._resolve_siblings(main, nav)
294+
items[items.index(main)] = page
295+
297296
# Render excerpts and perpare pagination
298297
posts, pagination = self._render(page)
299298

@@ -364,7 +363,7 @@ def _resolve(self, files: Files, config: MkDocsConfig, nav: Navigation):
364363
])
365364

366365
# Update entrypoint in navigation
367-
for items in [self._resolve_items(view.parent, nav), nav.pages]:
366+
for items in [self._resolve_siblings(view, nav), nav.pages]:
368367
items[items.index(page)] = view
369368

370369
# Return view
@@ -484,10 +483,10 @@ def _resolve_views(self, view: View):
484483
assert isinstance(page, View)
485484
yield page
486485

487-
# Resolve children of a navigation item
488-
def _resolve_items(self, item: StructureItem, nav: Navigation):
489-
if isinstance(item, Section):
490-
return item.children
486+
# Resolve siblings of a navigation item
487+
def _resolve_siblings(self, item: StructureItem, nav: Navigation):
488+
if isinstance(item.parent, Section):
489+
return item.parent.children
491490
else:
492491
return nav.items
493492

@@ -505,14 +504,14 @@ def _attach(self, parent: StructureItem, pages: list[Page]):
505504

506505
# Attach a section to the given parent section, make sure it's pages are
507506
# part of the navigation, and ensure all pages are linked correctly
508-
def _attach_to(self, parent: Section, section: Section, nav: Navigation):
509-
section.parent = parent
510-
511-
# Determine the parent section to attach the section to, which might be
512-
# the top-level navigation, if no parent section was given. Note, that
513-
# it's currently not possible to chose the position of a section, but
514-
# we might add support for this in the future.
515-
items = self._resolve_items(parent, nav)
507+
def _attach_to(self, view: View, section: Section, nav: Navigation):
508+
section.parent = view.parent
509+
510+
# Resolve siblings, which are the children of the parent section, or
511+
# the top-level list of navigation items if the view is at the root of
512+
# the project, and append the given section to it. It's currently not
513+
# possible to chose the position of a section.
514+
items = self._resolve_siblings(view, nav)
516515
items.append(section)
517516

518517
# Find last sibling that is a page, skipping sections, as we need to

0 commit comments

Comments
 (0)