diff --git a/custom_plugin_blog/mkdocs_material_mcdic/constants.py b/custom_plugin_blog/mkdocs_material_mcdic/constants.py index 4668f4c54..34fc7624a 100644 --- a/custom_plugin_blog/mkdocs_material_mcdic/constants.py +++ b/custom_plugin_blog/mkdocs_material_mcdic/constants.py @@ -24,9 +24,20 @@ ] = """ ### **[%s](/%s)** -| :%s: Updated | :%s: Created | :%s: Unique Visited | -| :---: | :---: | :---: | -| %s | %s | %s | +
+| :%s: Updated | :%s: Created | :%s: Unique Visited | :%s: Edit History | +| :---: | :---: | :---: | :---: | +| %s | %s | %s | %s | +
+ +
+| Metadata | Value | +| :---: | :---: | +| :%s: Updated | %s | +| :%s: Created | %s | +| :%s: Unique Visited | %s | +| :%s: Edit History | %s | +
""".strip() EXCERPT_READMORE: typing.Final[str] = "*[**(... Read more)**](/%s)*" @@ -81,3 +92,5 @@ r"^sorted\/most_viewed\.md$" ) RE_ARCHIVES_FINDER: typing.Final[re.Pattern] = re.compile(r"^archives\/[0-9]+\.md$") + +GITHUB_REPO_URL: typing.Final[str] = "https://github.com/McDic/BlogV2" diff --git a/custom_plugin_blog/mkdocs_material_mcdic/core.py b/custom_plugin_blog/mkdocs_material_mcdic/core.py index 0e3ba89af..2cf8217eb 100644 --- a/custom_plugin_blog/mkdocs_material_mcdic/core.py +++ b/custom_plugin_blog/mkdocs_material_mcdic/core.py @@ -19,7 +19,7 @@ from .ga4 import ViewDataValue, fetch_ga4_data from .ga4 import get_client as get_ga4_client from .ga4 import parse_ga4_cache -from .git import get_date_from_git +from .git import get_date_from_git, get_github_edit_history_url from .utils import dict_get, dict_merge_inplace logger = get_plugin_logger("mcdic") @@ -98,6 +98,12 @@ def _create_tempfile( use_directory_urls=True, ) + def _is_from_temp_dir(self, page: Page): + """ + Check if this page is created on the temporary directory. + """ + return Path(page.file.abs_src_path).is_relative_to(self._temp_dir.name) + def _check_page_by( self, page: Page, finder: re.Pattern, check_relative_path: bool ) -> bool: @@ -106,10 +112,7 @@ def _check_page_by( """ return bool( finder.match(page.file.src_path) - and ( - not check_relative_path - or Path(page.file.abs_src_path).is_relative_to(self._temp_dir.name) - ) + and (not check_relative_path or self._is_from_temp_dir(page)) ) def _is_blog_post_page(self, page: Page) -> bool: @@ -495,6 +498,25 @@ def datestr(d: str | None, commit: str | None) -> str: created_date = dict_get(post.meta, "date", "created") original_date = dict_get(post.meta, "date", "original") unique_users = dict_get(post.meta, "views") + edit_history_url = dict_get(post.meta, "edit_history") + + metadata_updated_date = datestr( + updated_date, dict_get(post.meta, "commit", "updated") + ) + metadata_created_date = datestr( + original_date or created_date, + dict_get(post.meta, "commit", "created") if original_date is None else None, + ) + metadata_unique_users = ( + f"{unique_users} users" + if unique_users + else constants.METADATA_NOT_AVAILABLE + ) + metadata_history = ( + "[../%s](%s)" % (edit_history_url.split("/")[-1], edit_history_url) + if edit_history_url + else constants.METADATA_NOT_AVAILABLE + ) return [ constants.METADATA_TABLE_MARKDOWN @@ -504,16 +526,19 @@ def datestr(d: str | None, commit: str | None) -> str: "material-calendar-edit", "material-calendar-plus", "material-eye-plus" if unique_users else "material-eye-remove", - datestr(updated_date, dict_get(post.meta, "commit", "updated")), - datestr( - original_date or created_date, - dict_get(post.meta, "commit", "created") - if original_date is None - else None, - ), - f"{unique_users} users" - if unique_users - else constants.METADATA_NOT_AVAILABLE, + "material-history", + metadata_updated_date, + metadata_created_date, + metadata_unique_users, + metadata_history, + "material-calendar-edit", + metadata_updated_date, + "material-calendar-plus", + metadata_created_date, + "material-eye-plus" if unique_users else "material-eye-remove", + metadata_unique_users, + "material-history", + metadata_history, ), (post.markdown or "") .split(constants.EXCERPT_DIVIDER)[0] @@ -660,6 +685,11 @@ def on_page_markdown( # View metadata page.meta["views"] = self._get_views_by_titles(page) + # History metadata + if not self._is_from_temp_dir(page): + page.meta["edit_history"] = get_github_edit_history_url(page.file.src_uri) + logger.info("edit_history = %s", page.meta["edit_history"]) + # Disable analytics if serving if self._build_mode != "serve": page.meta["enable_analytics"] = True @@ -757,13 +787,13 @@ def on_page_markdown( # ------------------------------------------- # Below cases are for normal documents + joinlist: list[str] = [markdown] + # If it is migrated? - elif dict_get(page.meta, "date", "original"): - return "\n\n".join([constants.POST_MIGRATION_NOTICE, markdown]) + if dict_get(page.meta, "date", "original"): + joinlist.insert(0, constants.POST_MIGRATION_NOTICE) - # Default case - else: - return None + return "\n\n".join(joinlist) def _modify_nav_on_root_page(self, section: Navigation | Section): """ diff --git a/custom_plugin_blog/mkdocs_material_mcdic/git.py b/custom_plugin_blog/mkdocs_material_mcdic/git.py index 1cb45fe1a..fb04ca154 100644 --- a/custom_plugin_blog/mkdocs_material_mcdic/git.py +++ b/custom_plugin_blog/mkdocs_material_mcdic/git.py @@ -5,6 +5,8 @@ from datetime import datetime from pathlib import Path +from . import constants + def get_date_from_git( path: str | Path, @@ -37,3 +39,14 @@ def get_date_from_git( raise FileNotFoundError("No git history found") result = out.decode().strip().split(" ") return result[0], datetime.fromisoformat(result[1]) + + +def get_github_edit_history_url(path: Path | str | None) -> str: + """ + Get URL to the github edit history of given file path. + If `path` is None, then return whole history of the master commit. + """ + if not path: + return constants.GITHUB_REPO_URL + else: + return "%s/commits/master/docs/%s" % (constants.GITHUB_REPO_URL, path) diff --git a/docs/stylesheets/meta.css b/docs/stylesheets/meta.css index 585151270..fb12f4058 100644 --- a/docs/stylesheets/meta.css +++ b/docs/stylesheets/meta.css @@ -27,3 +27,22 @@ padding-top: 0.2rem; content: ""; } + +.md-content { + @media screen and (max-width: 540px) { + div.md-mcdic--metadata-table.horizontal { + display: none; + } + div.md-mcdic--metadata-table.vertical { + display: block; + } + } + @media screen and (min-width: 541px) { + div.md-mcdic--metadata-table.horizontal { + display: block; + } + div.md-mcdic--metadata-table.vertical { + display: none; + } + } +} diff --git a/overrides/partials/post-meta.html b/overrides/partials/post-meta.html index e25e9be5c..d7daaf1c0 100644 --- a/overrides/partials/post-meta.html +++ b/overrides/partials/post-meta.html @@ -48,6 +48,17 @@ {% endif %} + {% if page.meta.edit_history %} +
  • + {% include ".icons/material/history.svg" %} +
    + Edit history: + + {{ "../" + page.meta.edit_history.split("/")[-1] }} + +
    +
  • + {% endif %}