Repository navigation
Replies: 7 comments 7 replies
|
The structure of the navigation object is a little beyond my knowledge. What I know could be discovered by exploring the content or doc. If that doesn't answer the question, the best would be to forward that question to the MkDocs team. |
|
It could be that the content is initialized or not according to whether that title was explicitly defined or not in the config file or the page's header; if it wasn't (typically, it's the heading of level 1) then it might be empty. |
|
Hi, I am having the same issue too. (All the Using this code: Moving the plugin up and down the plugin list does change how many get shown, so it seems to me a process gets cut short before it finishes. I'm aiming to pull a list of pages with a specific tag, but for now just getting a list of all the pages with tags is causing me grief. |
|
The same problem. I made Bad macro response. title=[blank] only in the root folder #268 including a complete test case. |
|
@lovelydinosaur Perhaps (from your expertise of MkDocs) you would have an advice to give in this case?
|
|
I ran into this too. I think the underlying issue is what @fralau said in #156, i.e., (Mk|Proper)Docs doesn’t even know the full navigation tree until all the pages are rendered (see diagram), and then MkDocs-Macros is finished and gone. (Navigation menus and the like have the full nav tree because they’re computed after all pages are rendered.) That is, given the current (Mk|ProperDocs) rendering flow, no plugin operating in the This is the workaround I used, which does a duplicate parse (in some sense a “second pass”) to populate the title: def define_env(env):
@env.macro
def entitle(nav):
"""MkDocs-Macros provides a handy `navigation` variable in its Jinja
context, which is the (Mk|Proper)Docs Navigation object [1], but many
of its fields are incomplete when the macros are being processed,
e.g. `title` [2,3]. This is because (Mk|Proper)Docs does not fully
compute page titles until they are rendered, i.e., the navigation
tree is simply not fully known until all the pages are rendered and
MkDocs-Macros is finished and gone.
Further, when the define_env() hook is called, the navigation
structure does not contain Page objects but rather just filenames.
To work around these timing problems, this macro parses page Markdown
sufficiently to populate titles and sets appropriate fields in the
Page objects [5]. One could consider the approach a fairly obnoxious
monkey-patch, but it works for us and does not require modifying the
render flow. Call it before you need to use `navigation`. This macro
produces no useful output, only side effects.
[1]: https://properdocs.org/dev-guide/themes/#navigation-objects
[2]: https://github.com/fralau/mkdocs-macros-plugin/discussions/198
[3]: https://github.com/fralau/mkdocs-macros-plugin/issues/156
[4]: https://properdocs.org/dev-guide/plugins/#events
[5]: https://github.com/ProperDocs/properdocs/blob/v1.6.7properdocs/structure/pages.py#L227"""
for i in nav:
# Populate title with quick-and-dirty parse. Don’t save the results
# into the Page object because I didn’t want to figure out how to do
# it right, and it’s the wrong time in the render pipeline anyway.
if (hasattr(i, "file") and not i._title_from_render):
LOG.info("fake parse: %s/%s" % (i.file.src_dir, i.file.src_uri))
md = markdown.Markdown()
title_prepr = mkdocs.structure.pages._ExtractTitleTreeprocessor()
title_prepr._register(md)
md.convert(i.file.content_string) # discard HTML return
i._title_from_render = title_prepr.title
# Page.title() will only use Page._title_from_render if this is
# non-None, so fool it. This will be overwritten when the proper
# value is actually read.
i.markdown = True
# Recurse if any children.
if (i.children):
entitle(i.children)
return "<!-- entitle() fake-parsing was called here -->"Once you call it with |


Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I find it strange that
{{ navigation.pages }}only returns a reduced/stripped down version of the Page object, only containing the title (Which most of the time seems to be blank) and the URL, but lacking crucial parts such as the Page meta or even the page content which MkDocs is including in the Page object.Why is that so? Can this be changed to be more in line with MkDocs'
navjinja2 variable, which contains all the Page's content?This issue makes it impossible for me to make a macro through a file that would list pages inside a specific folder, which I'm much more used to than making it in Python.
All reactions