Skip to content

Commit 7a02a04

Browse files
committed
Add new variables for site revision date, see #19
1 parent 22ff2e4 commit 7a02a04

File tree

5 files changed

+38
-3
lines changed

5 files changed

+38
-3
lines changed

docs/howto/override-a-theme.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,13 @@ And if you've enable creation date in the config:
120120
- `page.meta.git_creation_date_localized_raw_datetime`
121121
- `page.meta.git_creation_date_localized_raw_iso_date`
122122
- `page.meta.git_creation_date_localized_raw_iso_datetime`
123-
- `page.meta.git_creation_date_localized_raw_timeago`
123+
- `page.meta.git_creation_date_localized_raw_timeago`
124+
125+
And finally there are variants that contain the latest revision date of your entire site. This is the last commit on any file in the `docs/` directory, which is added to the meta information of every page.
126+
127+
- `page.meta.git_site_revision_date_localized`
128+
- `page.meta.git_site_revision_date_localized_raw_datetime`
129+
- `page.meta.git_site_revision_date_localized_raw_iso_date`
130+
- `page.meta.git_site_revision_date_localized_raw_date`
131+
- `page.meta.git_site_revision_date_localized_raw_iso_datetime`
132+
- `page.meta.git_site_revision_date_localized_raw_timeago`

mkdocs_git_revision_date_localized_plugin/css/timeago.css

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
/*
2-
* timeago output is dynamic, which breaks when you print a page.
3-
* This ensures fallback to type "iso_date" when printing.
2+
timeago output is dynamic, which breaks when you print a page.
3+
4+
This CSS is only included when type: timeago
5+
and ensures fallback to type "iso_date" when printing.
6+
47
*/
58

69
.git-revision-date-localized-plugin-iso_date { display: none }

mkdocs_git_revision_date_localized_plugin/plugin.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:
6161

6262
self.util = Util(config=self.config)
6363

64+
# Save last commit timestamp for entire site
65+
self.last_site_revision_timestamp = self.util.get_git_commit_timestamp(
66+
config.get('docs_dir')
67+
)
68+
6469
# Get locale settings - might be added in future mkdocs versions
6570
# see: https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/issues/24
6671
mkdocs_locale = config.get("locale", None)
@@ -222,6 +227,17 @@ def on_page_markdown(
222227
flags=re.IGNORECASE,
223228
)
224229

230+
# Finally,
231+
# Also add site last updated information, for developers
232+
site_dates = self.util.get_date_formats_for_timestamp(self.last_site_revision_timestamp)
233+
site_date = site_dates[self.config["type"]]
234+
if self.config["type"] == "timeago":
235+
site_date += site_dates["iso_date"]
236+
page.meta["git_site_revision_date_localized"] = site_date
237+
site_dates_raw = self.util.get_date_formats_for_timestamp(self.last_site_revision_timestamp, add_spans=False)
238+
for date_type, date_string in site_dates_raw.items():
239+
page.meta["git_site_revision_date_localized_raw_%s" % date_type] = date_string
240+
225241
return markdown
226242

227243
def on_post_build(self, config: Dict[str, Any], **kwargs) -> None:

mkdocs_git_revision_date_localized_plugin/util.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def get_git_commit_timestamp(
104104
# https://git-scm.com/docs/git-log#Documentation/git-log.txt---diff-filterACDMRTUXB82308203
105105
realpath = os.path.realpath(path)
106106
git = self._get_repo(realpath)
107+
107108
if is_first_commit:
108109
# diff_filter="A" will select the commit that created the file
109110
commit_timestamp = git.log(
@@ -115,6 +116,7 @@ def get_git_commit_timestamp(
115116
if commit_timestamp != "":
116117
commit_timestamp = commit_timestamp.split()[-1]
117118
else:
119+
# Latest commit touching a specific file
118120
commit_timestamp = git.log(
119121
realpath, date="short", format="%at", n=1
120122
)

tests/fixtures/basic_project/docs/overrides_material_theme/partials/source-date.html renamed to tests/fixtures/basic_project/docs/overrides_material_theme/partials/source-file.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,10 @@
3737
{% elif page.meta.revision_date %}
3838
{{ label }}: {{ page.meta.revision_date }}
3939
{% endif %}
40+
41+
{% if page.meta.git_site_revision_date_localized %}
42+
<br />Site last updated: {{ page.meta.git_site_revision_date_localized }}
43+
{% endif %}
44+
4045
</small>
4146
</div>

0 commit comments

Comments
 (0)