@@ -223,7 +223,7 @@ def on_nav(self, nav, *, config, files):
223
223
if self .config .pagination :
224
224
for view in self ._resolve_views (self .blog ):
225
225
for at in range (1 , len (view .pages )):
226
- self ._attach_at (view .pages [ at - 1 ] , view , view .pages [at ])
226
+ self ._attach_at (view .parent , view , view .pages [at ])
227
227
228
228
# Replace source file system path
229
229
view .pages [at ].file .src_uri = view .file .src_uri
@@ -245,19 +245,18 @@ def on_page_markdown(self, markdown, *, page, config, files):
245
245
return
246
246
247
247
# We set the contents of the view to its title if pagination should
248
- # not keep the content of the original view on paginaged views
248
+ # not keep the content of the original view on paginated views
249
249
if not self .config .pagination_keep_content :
250
250
view = self ._resolve_original (page )
251
251
if view in self ._resolve_views (self .blog ):
252
- assert isinstance (page , View )
253
- if page .pages .index (page ):
254
- main = page .parent
255
-
256
- # We need to use the rendered title of the original view
257
- # if the author set the title in the page's contents, or
258
- # it would be overridden with the one set in mkdocs.yml,
259
- # which would result in inconsistent headings
260
- name = main ._title_from_render or main .title
252
+
253
+ # If the current view is paginated, use the rendered title
254
+ # of the original view in case the author set the title in
255
+ # the page's contents, or it would be overridden with the
256
+ # one set in mkdocs.yml, leading to inconsistent headings
257
+ assert isinstance (view , View )
258
+ if view != page :
259
+ name = view ._title_from_render or view .title
261
260
return f"# { name } "
262
261
263
262
# Nothing more to be done for views
@@ -340,23 +339,12 @@ def on_page_context(self, context, *, page, config, nav):
340
339
if view not in self ._resolve_views (self .blog ):
341
340
return
342
341
343
- # Retrieve parent view or section
344
- assert isinstance (page , View )
345
- main = page .parent
346
-
347
- # If this page is a view, and the parent page is a view as well, we got
348
- # a paginated view and need to replace the parent with the current view.
349
- # Paginated views are always rendered at the end of the build, which is
350
- # why we can safely mutate the navigation at this point
351
- if isinstance (main , View ):
352
- page .parent = main .parent
353
-
354
- # Replace view in navigation and rewire it - the current view in the
355
- # navigation becomes the main view, thus the entire chain moves one
356
- # level up. It's essential that the rendering order is linear, or
357
- # else we might end up with a broken navigation.
358
- items = self ._resolve_siblings (main , nav )
359
- items [items .index (main )] = page
342
+ # If the current view is paginated, replace and rewire it - the current
343
+ # view temporarily becomes the main view, and is reset after rendering
344
+ assert isinstance (view , View )
345
+ if view != page :
346
+ items = self ._resolve_siblings (view , nav )
347
+ items [items .index (view )] = page
360
348
361
349
# Render excerpts and prepare pagination
362
350
posts , pagination = self ._render (page )
@@ -373,6 +361,26 @@ def pager(args: object):
373
361
context ["posts" ] = posts
374
362
context ["pagination" ] = pager if pagination else None
375
363
364
+ # After rendering a paginated view, replace the URL of the paginated view
365
+ # with the URL of the original view - since we need to replace the original
366
+ # view with a paginated view in `on_page_context` for correct resolution of
367
+ # the active state, we must fix the paginated view URLs after rendering
368
+ def on_post_page (self , output , * , page , config ):
369
+ if not self .config .enabled :
370
+ return
371
+
372
+ # Skip if page is not a view managed by this instance - this plugin has
373
+ # support for multiple instances, which is why this check is necessary
374
+ view = self ._resolve_original (page )
375
+ if view not in self ._resolve_views (self .blog ):
376
+ return
377
+
378
+ # If the current view is paginated, replace the URL of the paginated
379
+ # view with the URL of the original view - see https://t.ly/Yeh-P
380
+ assert isinstance (view , View )
381
+ if view != page :
382
+ page .file .url = view .file .url
383
+
376
384
# Remove temporary directory on shutdown
377
385
def on_shutdown (self ):
378
386
rmtree (self .temp_dir )
0 commit comments