Skip to content

Commit 62daea9

Browse files
committed
Fix: Fix detection of Jinja2, to allow title rendering (#266)
- Added `.has_j2() method for testing the presence of jinja2 - Added `.env()` method (to clarify code)
1 parent e0ba1ba commit 62daea9

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"files.exclude": {
3+
"logseq/*": true
4+
}
5+
}

journals/2025_08_10.md

Whitespace-only changes.

mkdocs_macros/plugin.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ def chatter(*args):
187187

188188
# ------------------------------------------------
189189
# These properties are available in the env object
190+
# in macros
190191
# ------------------------------------------------
191192

192193
@property
@@ -600,6 +601,20 @@ def _load_modules(self):
600601
# ----------------------------------
601602
# output elements
602603
# ----------------------------------
604+
@property
605+
def env(self) -> Environment:
606+
"""
607+
The templating environment for Jinja2.
608+
609+
It is a core component of the macros engine.
610+
It is defined in `on_config`.
611+
612+
NOTE: Do NOT confuse with the env argument in a module.
613+
"""
614+
try:
615+
return self._env
616+
except AttributeError:
617+
raise AttributeError("Jinja2 environment is not defined yet!")
603618

604619
def render(self, markdown: str, force_rendering:bool=False) -> str:
605620
"""
@@ -700,6 +715,24 @@ def render(self, markdown: str, force_rendering:bool=False) -> str:
700715

701716
else:
702717
return error_message
718+
719+
720+
def has_j2(self, s:str) -> bool:
721+
"""
722+
Defines whether a string might contain j2 code.
723+
724+
The criterion is: does it contain any start strings,
725+
such as, e.g., `{{`?
726+
727+
It takes into account the j2_..._start_string
728+
parameters of the config file.
729+
"""
730+
env = self.env
731+
CANDIDATES = [env.variable_start_string,
732+
env.block_start_string,
733+
env.comment_start_string]
734+
return any(item in s for item in CANDIDATES)
735+
703736

704737

705738
# ----------------------------------
@@ -814,7 +847,7 @@ def on_config(self, config):
814847
env_config[variable_name] = value
815848

816849
# finally build the environment:
817-
self.env = Environment(**env_config)
850+
self._env = Environment(**env_config)
818851

819852
# -------------------
820853
# Process macros
@@ -836,7 +869,11 @@ def on_config(self, config):
836869
self.env.filters.update(self.filters)
837870

838871
debug("End of environment config")
839-
872+
873+
874+
875+
876+
840877
def on_pre_build(self, *, config):
841878
"""
842879
Provide information on the variables.
@@ -932,7 +969,7 @@ def on_page_markdown(self, markdown, page:Page,
932969
# There is a bizarre issue #215 where setting the title
933970
# prevents interpretation of icons with pymdownx.emoji
934971
debug("Page title:",page.title)
935-
if "{" in page.title:
972+
if self.has_j2(page.title):
936973
page.title = self.render(markdown=page.title,
937974
force_rendering=force_rendering)
938975
debug("Page title after macro rendering:",page.title)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "mkdocs-macros-plugin"
33

44
# This version number is the REFERENCE for the rest of the project,
55
# particularly for update_pypi.sh
6-
version = "1.3.7"
6+
version = "1.3.8"
77

88
description = "Unleash the power of MkDocs with macros and variables"
99
readme = "README.md"

0 commit comments

Comments
 (0)