Skip to content

Commit

Permalink
Handle macros in page titles (#144)
Browse files Browse the repository at this point in the history
  - from Yaml (macros authorized in nav)
  - from page variables themselves
  • Loading branch information
Laurent Franceschetti committed Jul 2, 2023
1 parent c750bea commit e2fded0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
26 changes: 25 additions & 1 deletion mkdocs_macros/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from mkdocs.config.config_options import Type as PluginType
from mkdocs.plugins import BasePlugin
from mkdocs.structure.pages import Page
from mkdocs.structure.nav import Section
from mkdocs.utils import get_markdown_title

from mkdocs_macros.errors import format_error
from mkdocs_macros.context import define_env
Expand Down Expand Up @@ -488,7 +490,11 @@ def render(self, markdown: str):
# copy the page variables and update with the meta data
# in the YAML header:
page_variables = copy(self.variables)
meta_variables = self.variables['page'].meta
try:
meta_variables = self.variables['page'].meta
except KeyError as e:
# this is a premature rendering, no meta variables in the page
meta_variables = {}
# Warning this is ternary logique (True, False, None: nothing said)
ignore_macros = None
render_macros = None
Expand Down Expand Up @@ -655,6 +661,20 @@ def on_nav(self, nav, config, files):
Capture the nav and files objects so they can be used by
templates.
"""
# 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)
# nav has useful properties like 'pages' and 'items'
# see: https://github.com/mkdocs/mkdocs/blob/master/mkdocs/structure/nav.py
self.variables['navigation'] = nav
Expand Down Expand Up @@ -718,6 +738,10 @@ def on_page_markdown(self, markdown, page, config,
markdown=self.markdown,
# page=page,
)
# HACK: convert macros in the title from render (if exists)
# to answer 144
page.title = self.render(page.title)

# execute the post-macro functions in the various modules
for func in self.post_macro_functions:
func(self)
Expand Down
2 changes: 2 additions & 0 deletions test/module/docs/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ bar:
bingo: Hello
---

# Environment

{{ macros_info() }}


Expand Down
4 changes: 2 additions & 2 deletions test/module/docs/other.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Other Page
## Pages (simple)
# Other Page {{ unit_price}}
## Pages (simple) for {{ unit_price}}:
{{ context(navigation) | pretty }}

# Items
Expand Down
2 changes: 1 addition & 1 deletion test/module/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ copyright: (C) Laurent Franceschetti 2020

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

0 comments on commit e2fded0

Please sign in to comment.