Skip to content

Commit d4a37e1

Browse files
committed
Move on_post_page() event below on_page_markdown()
This reflects the order of execution in MkDocs
1 parent a6aa170 commit d4a37e1

File tree

1 file changed

+45
-45
lines changed
  • mkdocs_git_revision_date_localized_plugin

1 file changed

+45
-45
lines changed

mkdocs_git_revision_date_localized_plugin/plugin.py

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,51 @@ def on_config(self, config: config_options.Config, **kwargs) -> dict:
8989

9090
return config
9191

92+
def on_page_markdown(
93+
self, markdown: str, page: Page, config: config_options.Config, files, **kwargs
94+
) -> str:
95+
"""
96+
Replace jinja2 tags in markdown and templates with the localized dates
97+
98+
The page_markdown event is called after the page's markdown is loaded
99+
from file and can be used to alter the Markdown source text.
100+
The meta- data has been stripped off and is available as page.meta
101+
at this point.
102+
103+
https://www.mkdocs.org/user-guide/plugins/#on_page_markdown
104+
105+
Args:
106+
markdown (str): Markdown source text of page as string
107+
page: mkdocs.nav.Page instance
108+
config: global configuration object
109+
site_navigation: global navigation object
110+
111+
Returns:
112+
str: Markdown source text of page as string
113+
"""
114+
115+
revision_dates = self.util.get_revision_date_for_file(
116+
path=page.file.abs_src_path,
117+
locale=self.config.get("locale", "en"),
118+
time_zone=self.config.get("time_zone", "UTC"),
119+
fallback_to_build_date=self.config.get("fallback_to_build_date"),
120+
)
121+
revision_date = revision_dates[self.config["type"]]
122+
123+
# timeago output is dynamic, which breaks when you print a page
124+
# This ensures fallback to type "iso_date"
125+
# controlled via CSS (see on_post_page() event)
126+
if self.config["type"] == "timeago":
127+
revision_date += revision_dates["iso_date"]
128+
129+
page.meta["git_revision_date_localized"] = revision_date
130+
return re.sub(
131+
r"\{\{\s*[page\.meta\.]*git_revision_date_localized\s*\}\}",
132+
revision_date,
133+
markdown,
134+
flags=re.IGNORECASE,
135+
)
136+
92137
def on_post_page(self, output_content: str, **kwargs) -> str:
93138
"""
94139
Add timeago.js as a CDN to the HTML page.
@@ -140,48 +185,3 @@ def on_post_page(self, output_content: str, **kwargs) -> str:
140185
output_content = output_content[:idx] + extra_css + output_content[idx:]
141186

142187
return output_content
143-
144-
def on_page_markdown(
145-
self, markdown: str, page: Page, config: config_options.Config, files, **kwargs
146-
) -> str:
147-
"""
148-
Replace jinja2 tags in markdown and templates with the localized dates
149-
150-
The page_markdown event is called after the page's markdown is loaded
151-
from file and can be used to alter the Markdown source text.
152-
The meta- data has been stripped off and is available as page.meta
153-
at this point.
154-
155-
https://www.mkdocs.org/user-guide/plugins/#on_page_markdown
156-
157-
Args:
158-
markdown (str): Markdown source text of page as string
159-
page: mkdocs.nav.Page instance
160-
config: global configuration object
161-
site_navigation: global navigation object
162-
163-
Returns:
164-
str: Markdown source text of page as string
165-
"""
166-
167-
revision_dates = self.util.get_revision_date_for_file(
168-
path=page.file.abs_src_path,
169-
locale=self.config.get("locale", "en"),
170-
time_zone=self.config.get("time_zone", "UTC"),
171-
fallback_to_build_date=self.config.get("fallback_to_build_date"),
172-
)
173-
revision_date = revision_dates[self.config["type"]]
174-
175-
# timeago output is dynamic, which breaks when you print a page
176-
# This ensures fallback to type "iso_date"
177-
# controlled via CSS (see on_post_page() event)
178-
if self.config["type"] == "timeago":
179-
revision_date += revision_dates["iso_date"]
180-
181-
page.meta["git_revision_date_localized"] = revision_date
182-
return re.sub(
183-
r"\{\{\s*[page\.meta\.]*git_revision_date_localized\s*\}\}",
184-
revision_date,
185-
markdown,
186-
flags=re.IGNORECASE,
187-
)

0 commit comments

Comments
 (0)