Skip to content

Commit b2681ae

Browse files
committed
Add fallback to iso_date when printing with 'type: timeago', fixes #12
1 parent 9215b89 commit b2681ae

File tree

1 file changed

+25
-1
lines changed
  • mkdocs_git_revision_date_localized_plugin

1 file changed

+25
-1
lines changed

mkdocs_git_revision_date_localized_plugin/plugin.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def on_post_page(self, output_content: str, **kwargs) -> str:
112112
if self.config.get("type") != "timeago":
113113
return output_content
114114

115+
# Insert timeago.js dependencies
115116
extra_js = """
116117
<script src="https://cdnjs.cloudflare.com/ajax/libs/timeago.js/4.0.0-beta.2/timeago.min.js"></script>
117118
<script src="https://cdnjs.cloudflare.com/ajax/libs/timeago.js/4.0.0-beta.2/timeago.locales.min.js"></script>
@@ -122,7 +123,23 @@ def on_post_page(self, output_content: str, **kwargs) -> str:
122123
</script>
123124
"""
124125
idx = output_content.index("</body>")
125-
return output_content[:idx] + extra_js + output_content[idx:]
126+
output_content = output_content[:idx] + extra_js + output_content[idx:]
127+
128+
# timeago output is dynamic, which breaks when you print a page
129+
# This ensures fallback to type "iso_date"
130+
extra_css = """
131+
<style>
132+
.git-revision-date-localized-plugin-iso_date { display: none }
133+
@media print {
134+
.git-revision-date-localized-plugin-iso_date { display: inline }
135+
.git-revision-date-localized-plugin-timeago { display: none }
136+
}
137+
</style>
138+
"""
139+
idx = output_content.index("</head>")
140+
output_content = output_content[:idx] + extra_css + output_content[idx:]
141+
142+
return output_content
126143

127144
def on_page_markdown(
128145
self, markdown: str, page: Page, config: config_options.Config, files, **kwargs
@@ -154,6 +171,13 @@ def on_page_markdown(
154171
fallback_to_build_date=self.config.get("fallback_to_build_date"),
155172
)
156173
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+
157181
page.meta["git_revision_date_localized"] = revision_date
158182
return re.sub(
159183
r"\{\{\s*[page\.meta\.]*git_revision_date_localized\s*\}\}",

0 commit comments

Comments
 (0)