Files
notebook/docs/overrides/partials/content.html
T
kurihada c4058528fc feat: 文章顶部自动显示元数据栏(创建日期、更新日期、字数、阅读时间)
- 安装 mkdocs-git-revision-date-localized-plugin,自动提取 git 提交日期
- 覆盖 content.html,在标题下方注入元数据栏
- JavaScript 自动统计中英文字数并估算阅读时间(400 字/分钟)
- 适配 Material SPA 导航 (document$.subscribe),页面切换时自动更新
- 深色模式兼容
2026-06-04 12:04:13 +08:00

92 lines
3.0 KiB
HTML

{#-
Override Material theme content partial
Adds page metadata bar: creation date, update date, word count, reading time
-#}
{% include "partials/tags.html" %}
{% include "partials/actions.html" %}
{% if "<h1" not in page.content %}
<h1>{{ page.title | d(config.site_name, true)}}</h1>
{% endif %}
{#- Metadata bar #}
{% if page.meta and (page.meta.git_revision_date_localized or page.meta.git_creation_date_localized) %}
<div class="md-page-metadata">
<span class="md-page-metadata__item">
<span class="twemoji">{% include ".icons/material/calendar-plus.svg" %}</span>
创建:{{ page.meta.git_creation_date_localized }}
</span>
<span class="md-page-metadata__item">
<span class="twemoji">{% include ".icons/material/calendar-edit.svg" %}</span>
更新:{{ page.meta.git_revision_date_localized }}
</span>
<span class="md-page-metadata__item" id="word-count">
<span class="twemoji">{% include ".icons/material/text-short.svg" %}</span>
字数:<span id="word-count-value">...</span>
</span>
<span class="md-page-metadata__item" id="reading-time">
<span class="twemoji">{% include ".icons/material/clock-outline.svg" %}</span>
阅读:<span id="reading-time-value">...</span>
</span>
</div>
{% endif %}
{{ page.content }}
{% include "partials/source-file.html" %}
{% include "partials/feedback.html" %}
{% include "partials/comments.html" %}
{#- Word count & reading time script (SPA-aware) #}
<script>
function updateMeta() {
var article = document.querySelector('.md-content__inner') || document.querySelector('.md-content');
if (!article) return;
var text = article.textContent || '';
// Chinese characters
var chineseChars = (text.match(/[一-鿿㐀-䶿]/g) || []).length;
// English/other words
var englishWords = text.replace(/[一-鿿㐀-䶿]/g, ' ')
.split(/\s+/)
.filter(function(w) { return w.length > 0; }).length;
var totalWords = chineseChars + englishWords;
var minutes = Math.max(1, Math.round(totalWords / 400));
var wordEl = document.getElementById('word-count-value');
var timeEl = document.getElementById('reading-time-value');
if (wordEl) wordEl.textContent = totalWords.toLocaleString();
if (timeEl) timeEl.textContent = minutes + ' 分钟';
}
// SPA-compatible: run on load and on navigation
if (typeof document$ !== 'undefined') {
document$.subscribe(function() { updateMeta(); });
} else {
document.addEventListener('DOMContentLoaded', updateMeta);
}
</script>
{#- CSS for metadata bar #}
<style>
.md-page-metadata {
display: flex;
flex-wrap: wrap;
gap: 0.5rem 1.5rem;
margin: 0 0 1.5rem 0;
padding-bottom: 0.75rem;
border-bottom: 1px solid var(--md-default-fg-color--lightest);
font-size: 0.7rem;
color: var(--md-default-fg-color--light);
}
.md-page-metadata__item {
display: inline-flex;
align-items: center;
gap: 0.2rem;
white-space: nowrap;
}
.md-page-metadata__item .twemoji {
width: 0.9rem;
height: 0.9rem;
}
.md-page-metadata__item .twemoji svg {
width: 100%;
height: 100%;
}
</style>