Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Menu not rendered when using macros inside H1 #144

Closed
BoxedBrain opened this issue Jul 26, 2022 · 10 comments
Closed

Menu not rendered when using macros inside H1 #144

BoxedBrain opened this issue Jul 26, 2022 · 10 comments
Labels
enhancement New feature or request fixed A fix has been submitted info required Further information is requested

Comments

@BoxedBrain
Copy link

BoxedBrain commented Jul 26, 2022

Hi,

I am using macros inside some H1 tags.
e.g. # Configuration of {{feature-x.name}}

The page title works as expected, but inside the menu it's displayed raw.
Is it somehow possible to solve that using macros and without specifying the menu entry manually?

Thanks!

@github-actions
Copy link

Welcome to this project and thank you!' first issue

@fralau
Copy link
Owner

fralau commented Jul 27, 2022

It's interesting... I had not thought about that and nobody tried so far, obviously. Yes, one could want to have a page with a variable title. 🤔

But could tell me how come you had this use case? (what's the general logic of having pages with variable titles, so that I could see if there is not another way?)

That's my idea of how it could work, if we tried to fix this: exploit the on_nav() event; and run through all the page titles, and attempt to render them (convert from text + jinja2 to raw text.

@fralau fralau added the info required Further information is requested label Jul 27, 2022
@BoxedBrain
Copy link
Author

@fralau we are currently discussing the renaming of some product features. Therefore, I thought about replacing them with a variable to do changes quickly and easier everywhere. Also, we use some of them in our headings, and that's how I came across this issue.

@fralau
Copy link
Owner

fralau commented Jul 28, 2022

@BoxedBrain I can see how it makes sense... From a functional viewpoint, if we push the requirement of "translating macros" to its logical conclusion, then it might also apply to header text, which goes into the building of menus.

However, as I mentioned, it's a completely different thing "under the hood", which obeys to a different logic: we would need to develop something completely new, in a new direction.

Why not?... 🤔 But it's not something I would invest my time in, without thinking first.

@fralau fralau added enhancement New feature or request and removed info required Further information is requested labels Jul 28, 2022
@fralau
Copy link
Owner

fralau commented Apr 23, 2023

What do we want to do with this issue?

@fralau
Copy link
Owner

fralau commented Jul 2, 2023

Considering the list of events provided by mkdocs' documentation:

We should use the on_nav() event to, somehow, list and render all macros in the titles. That event (already used in the code), is executed after on_config(), hence the renderer (MacrosPlugin.render()) should be available for work.

@fralau
Copy link
Owner

fralau commented Jul 2, 2023

@BoxedBrain Here is the result of my experiments: I made the change for the navigation on my dev environment and it does work.

However, it won't work if the macro is directly in the Header 1 of the page (problem of the chicken and the egg, which I haven't been able to solve yet).

What works, is to write the variable title (with the macro) in the nav section of the mkdocs.yaml file, e.g. :

nav:
    - Home: index.md
    - Environment for {{ unit_price}}: environment.md
    - Second:
        - Also for {{ unit_price}}: other.md
    - Not interpreted: literal.md

It means you cannot use meta variables in the page. But if your feature-x variable has been defined programmatically in a module (I suppose it is) then that could work for you?

Here is the changed code in on_nav:

from mkdocs.structure.nav import Section

        # Render also the navigation items, so that macros are interpreted
        # also in navigation
        # solution to issue #144
        def render_nav(nav):
            for nav_item in nav:
                try:
                    nav_item.title = self.render(nav_item.title)
                except AttributeError:
                    # not title in pre-page navigation, do nothing
                    pass
                if isinstance(nav_item, Section): 
                    # for second, third level titles
                    render_nav(nav_item.children)
        render_nav(nav)

@fralau fralau added the info required Further information is requested label Jul 2, 2023
fralau pushed a commit that referenced this issue Jul 2, 2023
  - from Yaml (macros authorized in nav)
  - from page variables themselves
@fralau fralau added the fixed A fix has been submitted label Jul 2, 2023
@fralau
Copy link
Owner

fralau commented Jul 6, 2023

Note: I did not use the code above. More simply, I made the program render the title of the page (interpret the macros), just after rendering the macros of the page itself. Since navigation is woven into the HTML page after all pages have been rendered from mkdocs to HTML, it works.

@BoxedBrain
Copy link
Author

Hi @fralau, thanks for your effort! We moved away from using macros in h1.
But it looks like it is relevant for @sverdoux / #171

@fralau
Copy link
Owner

fralau commented Jul 12, 2023

This update was pushed in mkdocs-macros 1.0.2 on pypi.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fixed A fix has been submitted info required Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants