Skip to content

Commit

Permalink
Show head commit sha in footer on preview builds (#2384)
Browse files Browse the repository at this point in the history
  • Loading branch information
joonas-somero authored Jan 8, 2025
1 parent 9d05484 commit 8383c9c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions csc-overrides/partials/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
<div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid">
{% include "partials/copyright.html" %}
{% if page and page.meta and page.meta["head_sha"] %}
<pre style="color: pink;
font-size: small;"
>commit {{ page.meta["head_sha"] | string }}</pre>
{% endif %}
{% if config.extra.social %}
{% include "partials/social.html" %}
{% endif %}
Expand Down
42 changes: 42 additions & 0 deletions hooks/preview.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from pathlib import Path

from git import Repo
from mkdocs.plugins import get_plugin_logger


class PreviewHook:
MKDOCS_HOOK_NAME = "preview-hook"
ENVIRONMENT_KEY = "environment"
PREVIEW_ENVIRONMENT = "preview"

def __init__(self):
self.environment = None
self.startup_command = None
self.logger = get_plugin_logger(self.MKDOCS_HOOK_NAME)
self.head_sha = Repo(Path.cwd()).head.commit.hexsha

@property
def is_preview_build(self):
return self.environment == self.PREVIEW_ENVIRONMENT and \
self.startup_command == "build"

def on_startup(self, command, dirty):
self.startup_command = command
return None

def on_config(self, config):
self.environment = config.extra[self.ENVIRONMENT_KEY]
if self.is_preview_build:
self.logger.info(f"preview build, commit {self.head_sha}")
return None

def on_page_markdown(self, markdown, page, config, files):
if self.is_preview_build:
page.meta["head_sha"] = self.head_sha
return None


hook = PreviewHook()
on_startup = hook.on_startup
on_config = hook.on_config
on_page_markdown = hook.on_page_markdown
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extra:

hooks:
- hooks/archives.py
- hooks/preview.py

plugins:
- tags:
Expand Down

0 comments on commit 8383c9c

Please sign in to comment.