-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Description
Following up on squidfunk/mkdocs-material#5909 (comment), I'm suggesting to adjust the logic that MkDocs implies to create Page objects from/in files, i.e., File objects. Currently, _data_to_navigation will always create (and thus overwrite) Page on everything that doesn't look like a Link or Section. Excerpt of the comment linked:
MkDocs will always generate either a
Linkor aPagefor a file that is not excluded. As you've probably noticed, the blog plugin makes use of subclasses for pages of specific types, e.g.Post,View, etc., and relations among different concepts in the plugin are managed inside the instances of those subclasses. The problem is, that MkDocs will always generate a plain oldPageinstance, overwriting the more specific instance that has been generated by the plugin before afteron_filesand beforeon_nav. We could still solve it by splitting file generation and page instantiation, but this would make things much more complicated and non-linear, because during generation, we already assign categories to pages and vice versa, etc. We would need to split this into two stages, blowing up the code base of the plugin and also the runtime, as we're doing two passes. This is clearly not favorable from our perspective.Moving forward: I believe that this problem can be mitigated with a very small but important change in MkDocs: if a
Filealready defines a dedicatedPageinfile.page, MkDocs should just use that instead of creating a new one:return file.page if isinstance(file.page, Page) else Page(title, file, config)It's essentially a single line change in MkDocs that would make things much, much simpler for plugin developers. I've written several plugins now, and I repeatedly had to work around the oddities of this behavior.
Thus, I'm proposing to change the following line:
# Now: always create a page
return Page(title, file, config)
# Better: only create a page when one was not created before
return file.page if isinstance(file.page, Page) else Page(title, file, config)Currently, the only way to work around this limitation is to overwrite Page.__class__ on the resulting instance in on_nav, e.g., like done in section-index, which, AFAIK, does not call __init__ or initializes instance variables:
page.__class__ = CustomPage
assert isinstance(page, CustomPage)