Skip to content

Commit

Permalink
Add new metadata(edit history) and make metadata table flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
McDic committed May 31, 2024
1 parent 93221ad commit 84c138d
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 23 deletions.
19 changes: 16 additions & 3 deletions custom_plugin_blog/mkdocs_material_mcdic/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,20 @@
] = """
### **[%s](/%s)**
| :%s: Updated | :%s: Created | :%s: Unique Visited |
| :---: | :---: | :---: |
| %s | %s | %s |
<div class="md-mcdic--metadata-table horizontal" markdown="1">
| :%s: Updated | :%s: Created | :%s: Unique Visited | :%s: Edit History |
| :---: | :---: | :---: | :---: |
| %s | %s | %s | %s |
</div>
<div class="md-mcdic--metadata-table vertical" markdown="1">
| Metadata | Value |
| :---: | :---: |
| :%s: Updated | %s |
| :%s: Created | %s |
| :%s: Unique Visited | %s |
| :%s: Edit History | %s |
</div>
""".strip()

EXCERPT_READMORE: typing.Final[str] = "*[**(... Read more)**](/%s)*"
Expand Down Expand Up @@ -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"
70 changes: 50 additions & 20 deletions custom_plugin_blog/mkdocs_material_mcdic/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down
13 changes: 13 additions & 0 deletions custom_plugin_blog/mkdocs_material_mcdic/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from datetime import datetime
from pathlib import Path

from . import constants


def get_date_from_git(
path: str | Path,
Expand Down Expand Up @@ -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)
19 changes: 19 additions & 0 deletions docs/stylesheets/meta.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
11 changes: 11 additions & 0 deletions overrides/partials/post-meta.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@
</div>
</li>
{% endif %}
{% if page.meta.edit_history %}
<li class="md-meta__item">
{% include ".icons/material/history.svg" %}
<div class="text">
Edit history:
<a href="{{ page.meta.edit_history }}">
{{ "../" + page.meta.edit_history.split("/")[-1] }}
</a>
</div>
</li>
{% endif %}
</ul>
</div>
</header>
Expand Down

0 comments on commit 84c138d

Please sign in to comment.