1
Fork 0
mkdocs-site/hooks/nav.py
Jake Howard ae44083c72
Add function to get page URL
This is much easier to work with
2023-09-14 22:02:40 +01:00

36 lines
848 B
Python

import jinja2
from mkdocs.utils.templates import url_filter
def is_list_page(page) -> bool:
if not page.parent:
return False
return page.parent.children[0] == page
def children(page):
if not is_list_page(page):
return []
return page.parent.children[1:]
@jinja2.pass_context
def get_page(context, slug):
nav = context["nav"]
for page in nav.pages:
if page.file.src_uri == slug:
return page
return None
@jinja2.pass_context
def get_page_url(context, slug):
page = get_page(context, slug)
if page is None:
return None
return url_filter(context, page.url)
def on_env(env, config, files):
env.filters["is_list_page"] = is_list_page
env.filters["children"] = children
env.globals["get_page"] = get_page
env.globals["get_page_url"] = get_page_url