-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show head commit sha in footer on preview builds (#2384)
- Loading branch information
1 parent
9d05484
commit 8383c9c
Showing
3 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ extra: | |
|
||
hooks: | ||
- hooks/archives.py | ||
- hooks/preview.py | ||
|
||
plugins: | ||
- tags: | ||
|