From ae44083c727bd5532bc6eee44fdd73e5191f2189 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 14 Sep 2023 22:02:40 +0100 Subject: [PATCH] Add function to get page URL This is much easier to work with --- hooks/nav.py | 11 +++++++++++ theme/main.html | 2 ++ 2 files changed, 13 insertions(+) diff --git a/hooks/nav.py b/hooks/nav.py index ad63d0f..b1f6f6c 100644 --- a/hooks/nav.py +++ b/hooks/nav.py @@ -1,4 +1,5 @@ import jinja2 +from mkdocs.utils.templates import url_filter def is_list_page(page) -> bool: if not page.parent: @@ -19,7 +20,17 @@ def get_page(context, 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 diff --git a/theme/main.html b/theme/main.html index ceaef1c..c6cff26 100644 --- a/theme/main.html +++ b/theme/main.html @@ -13,6 +13,8 @@ Children: {{ page|children }} Found page: {{ tag_1_page }} +URL: {{ get_page_url("tags/tag1.md") }} + Image resized: {{ "./assets/test.jpg"|resize_image(1500) }} Globals: {{ globals.foo }}