@@ -161,7 +161,7 @@ def on_nav(self, nav, *, config, files):
161
161
162
162
# Attach and link views for archive
163
163
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 )
165
165
166
166
# Generate and attach views for categories
167
167
if self .config .categories :
@@ -170,7 +170,7 @@ def on_nav(self, nav, *, config, files):
170
170
171
171
# Attach and link views for categories
172
172
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 )
174
174
175
175
# Paginate generated views, if enabled
176
176
if self .config .pagination :
@@ -280,20 +280,19 @@ def on_page_context(self, context, *, page, config, nav):
280
280
main = page .parent
281
281
282
282
# 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
286
286
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
295
287
page .parent = main .parent
296
288
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
+
297
296
# Render excerpts and perpare pagination
298
297
posts , pagination = self ._render (page )
299
298
@@ -364,7 +363,7 @@ def _resolve(self, files: Files, config: MkDocsConfig, nav: Navigation):
364
363
])
365
364
366
365
# 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 ]:
368
367
items [items .index (page )] = view
369
368
370
369
# Return view
@@ -484,10 +483,10 @@ def _resolve_views(self, view: View):
484
483
assert isinstance (page , View )
485
484
yield page
486
485
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
491
490
else :
492
491
return nav .items
493
492
@@ -505,14 +504,14 @@ def _attach(self, parent: StructureItem, pages: list[Page]):
505
504
506
505
# Attach a section to the given parent section, make sure it's pages are
507
506
# 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 )
516
515
items .append (section )
517
516
518
517
# Find last sibling that is a page, skipping sections, as we need to
0 commit comments