-
I want to make a filter similar to MkDocs' Looking at the code of MkDocs, it seems to be structured like this: class TemplateContext(TypedDict):
nav: Navigation
pages: Sequence[File]
base_url: str
extra_css: Sequence[str] # Do not use, prefer `config.extra_css`.
extra_javascript: Sequence[str] # Do not use, prefer `config.extra_javascript`.
mkdocs_version: str
build_date_utc: datetime.datetime
config: MkDocsConfig
page: Page | None
@contextfilter
def url_filter(context: TemplateContext, value: str) -> str:
"""A Template filter to normalize URLs."""
return normalize_url(str(value), page=context['page'], base=context['base_url']) The issue I see right now is, that recreating the filter like this: from mkdocs.utils.templates import TemplateContext
from mkdocs.utils import normalize_url
def define_env(env):
@env.filter
def correct_url(context: TemplateContext, value: str) -> str:
return normalize_url(str(value), page=context['page'], base=context['base_url']) ...would result in a filter that requires the value to be provided, which is not ideal for me... What is a recommended aproach here? Or does the plugin offer a filter for this functionality? I'm aware the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
I would need to know more about the context. What issue do you wish to solve and when does it occur?
|
Beta Was this translation helpful? Give feedback.
Managed to get a working filter using this:
Allows me to have
{{ page.url | url }}