Skip to content

Commit

Permalink
Improved implementation for relative url (#236)
Browse files Browse the repository at this point in the history
- New filter is now called `relative_url`.
  • Loading branch information
Laurent Franceschetti committed Jul 28, 2024
1 parent 69e7cad commit 2fb833e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
20 changes: 16 additions & 4 deletions mkdocs_macros/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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
Expand All @@ -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
Expand All @@ -383,4 +383,16 @@ def now():


# add the normal mkdocs url function
env.filter(normalize_url)
# 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)
2 changes: 1 addition & 1 deletion mkdocs_macros/macros_info.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

0 comments on commit 2fb833e

Please sign in to comment.