From 2fb833e4009f6875d0df01f00e7c2d4640c1e00a Mon Sep 17 00:00:00 2001 From: Laurent Franceschetti Date: Sun, 28 Jul 2024 16:20:48 +0200 Subject: [PATCH] Improved implementation for relative url (#236) - New filter is now called `relative_url`. --- mkdocs_macros/context.py | 20 ++++++++++++++++---- mkdocs_macros/macros_info.md | 2 +- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/mkdocs_macros/context.py b/mkdocs_macros/context.py index 15bc7b6..c9d961b 100644 --- a/mkdocs_macros/context.py +++ b/mkdocs_macros/context.py @@ -323,7 +323,7 @@ def render_file(filename): @env.macro def context(obj=env.variables): - "*Default mkdocs_macro* List the defined variables" + "*Default Mkdocs-Macro*: List the defined variables" try: return [(var, type(value).__name__, format_value(value)) for var, value in list_items(obj)] @@ -336,7 +336,7 @@ def context(obj=env.variables): @env.filter def pretty(var_list): """ - *Default mkdocs_macro* Prettify a dictionary or object + *Default Mkdocs-Macro*: Prettify a dictionary or object (used for environment documentation, or debugging). Note: it will work only on the product of the `context()` macro @@ -360,7 +360,7 @@ def pretty(var_list): @env.macro def macros_info(): """ - Test/debug function: + *Test/debug function*: list useful documentation on the mkdocs_macro environment. """ # NOTE: this is template @@ -383,4 +383,16 @@ def now(): # add the normal mkdocs url function - env.filter(normalize_url) \ No newline at end of file + # env.filter(normalize_url) + + @env.filter + def relative_url(path: str): + """ + *Default Mkdocs-Macro*: + convert the path of any page according to MkDoc's internal logic, + into a URL relative to the current page + (implements the `normalize_url()` function from `mkdocs.util`). + Typically used to manage custom navigation: + `{{ page.url | relative_url }}`. + """ + return normalize_url(path=path, page=env.page) \ No newline at end of file diff --git a/mkdocs_macros/macros_info.md b/mkdocs_macros/macros_info.md index 348be1c..3a921c3 100644 --- a/mkdocs_macros/macros_info.md +++ b/mkdocs_macros/macros_info.md @@ -60,6 +60,6 @@ These filters are provided as a standard by the macros plugin. ### Builtin Jinja2 Filters These filters are provided by Jinja2 as a standard. -See also the [Jinja2 documentation on builtin filters](https://jinja.palletsprojects.com/en/2.11.x/templates/#builtin-filters)). +See also the [Jinja2 documentation on builtin filters](https://jinja.palletsprojects.com/en/3.1.x/templates/#builtin-filterss). {{ context(filters_builtin) | pretty }}