Skip to content

Commit 2fb833e

Browse files
author
Laurent Franceschetti
committed
Improved implementation for relative url (#236)
- New filter is now called `relative_url`.
1 parent 69e7cad commit 2fb833e

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

mkdocs_macros/context.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def render_file(filename):
323323

324324
@env.macro
325325
def context(obj=env.variables):
326-
"*Default mkdocs_macro* List the defined variables"
326+
"*Default Mkdocs-Macro*: List the defined variables"
327327
try:
328328
return [(var, type(value).__name__, format_value(value))
329329
for var, value in list_items(obj)]
@@ -336,7 +336,7 @@ def context(obj=env.variables):
336336
@env.filter
337337
def pretty(var_list):
338338
"""
339-
*Default mkdocs_macro* Prettify a dictionary or object
339+
*Default Mkdocs-Macro*: Prettify a dictionary or object
340340
(used for environment documentation, or debugging).
341341
342342
Note: it will work only on the product of the `context()` macro
@@ -360,7 +360,7 @@ def pretty(var_list):
360360
@env.macro
361361
def macros_info():
362362
"""
363-
Test/debug function:
363+
*Test/debug function*:
364364
list useful documentation on the mkdocs_macro environment.
365365
"""
366366
# NOTE: this is template
@@ -383,4 +383,16 @@ def now():
383383

384384

385385
# add the normal mkdocs url function
386-
env.filter(normalize_url)
386+
# env.filter(normalize_url)
387+
388+
@env.filter
389+
def relative_url(path: str):
390+
"""
391+
*Default Mkdocs-Macro*:
392+
convert the path of any page according to MkDoc's internal logic,
393+
into a URL relative to the current page
394+
(implements the `normalize_url()` function from `mkdocs.util`).
395+
Typically used to manage custom navigation:
396+
`{{ page.url | relative_url }}`.
397+
"""
398+
return normalize_url(path=path, page=env.page)

mkdocs_macros/macros_info.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ These filters are provided as a standard by the macros plugin.
6060
### Builtin Jinja2 Filters
6161
These filters are provided by Jinja2 as a standard.
6262

63-
See also the [Jinja2 documentation on builtin filters](https://jinja.palletsprojects.com/en/2.11.x/templates/#builtin-filters)).
63+
See also the [Jinja2 documentation on builtin filters](https://jinja.palletsprojects.com/en/3.1.x/templates/#builtin-filterss).
6464

6565
{{ context(filters_builtin) | pretty }}

0 commit comments

Comments
 (0)