Skip to content

Commit 0924228

Browse files
committed
New Function: 编辑中间页显示文档编辑历史
1 parent a08114e commit 0924228

1 file changed

Lines changed: 68 additions & 1 deletion

File tree

docs/intro/edit.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
- 保持格式与原文一致
2525
- 提交前检查是否有遗漏
2626

27+
### 页面更新历史
28+
2729
<script>
2830
// 获取URL参数中的page值
2931
function getPageParam() {
@@ -48,8 +50,73 @@
4850
// 跳转到GitHub编辑页面
4951
window.location.href = 'https://github.com/HowCam/howcam.github.io/edit/main/docs/' + cleanPath + '.md';
5052
}
53+
54+
// 显示页面更新历史
55+
async function showPageHistory() {
56+
const pageUrl = getPageParam();
57+
if (!pageUrl) {
58+
document.getElementById('history-container').innerHTML = '<p>未检测到页面路径参数,无法显示历史记录。</p>';
59+
return;
60+
}
61+
62+
// 移除开头的斜杠和末尾的斜杠
63+
let cleanPath = pageUrl.replace(/^\/|\/$/g, '');
64+
65+
// 确保路径以 docs/ 开头
66+
if (!cleanPath.startsWith('docs/')) {
67+
cleanPath = 'docs/' + cleanPath;
68+
}
69+
70+
// 移除 .md 扩展名(如果有的话)
71+
cleanPath = cleanPath.replace(/\.md$/, '');
72+
73+
try {
74+
const response = await fetch(`https://api.github.com/repos/HowCam/howcam.github.io/commits?path=${cleanPath}.md&per_page=10`);
75+
const commits = await response.json();
76+
77+
if (!Array.isArray(commits) || commits.length === 0) {
78+
document.getElementById('history-container').innerHTML = '<p>暂无该页面的历史记录。</p>';
79+
return;
80+
}
81+
82+
const historyList = commits.map(commit => {
83+
const date = new Date(commit.commit.author.date).toLocaleString('zh-CN', {
84+
year: 'numeric',
85+
month: '2-digit',
86+
day: '2-digit',
87+
hour: '2-digit',
88+
minute: '2-digit'
89+
});
90+
const author = commit.commit.author.name;
91+
const authorAvatar = commit.author ? commit.author.avatar_url : '';
92+
const message = commit.commit.message.split('\n')[0];
93+
const url = commit.html_url;
94+
95+
const avatarHtml = authorAvatar ?
96+
`<img src="${authorAvatar}" alt="${author}" style="width: 20px; height: 20px; border-radius: 50%; vertical-align: middle; margin-left: 4px;">` : '';
97+
98+
return `<li style="display: flex; align-items: center; justify-content: space-between; padding: 6px 12px; background: #e8f5e9; border-radius: 4px; margin-bottom: 4px; font-size: 0.85em;">
99+
<span><strong>${date}</strong> ${author}${avatarHtml} <span style="color: #666;">${message}</span></span>
100+
<a href="${url}" target="_blank" style="color: #2e7d32; text-decoration: none; white-space: nowrap; margin-left: 8px;">查看 →</a>
101+
</li>`;
102+
}).join('');
103+
104+
document.getElementById('history-container').innerHTML = `<div style="max-height: 200px; overflow-y: auto; border: 1px solid #c8e6c9; border-radius: 8px; padding: 8px;"><ul style="list-style: none; padding: 0; margin: 0;">${historyList}</ul></div>`;
105+
} catch (error) {
106+
console.error('获取历史记录失败:', error);
107+
document.getElementById('history-container').innerHTML = '<p>获取历史记录失败,请刷新页面重试。</p>';
108+
}
109+
}
110+
111+
// 页面加载完成后获取历史
112+
window.addEventListener('DOMContentLoaded', showPageHistory);
51113
</script>
52-
<div style="margin: 2rem 0;">
114+
115+
<div id="history-container" style="margin: 2rem 0;">
116+
<p>加载中...</p>
117+
</div>
118+
119+
<div style="margin: 2rem 0; text-align: right;">
53120
<button style="padding:.75em 1.25em; display:inline-block; line-height:1; text-decoration:none; white-space:nowrap; cursor:pointer; border:1px solid #42b983; border-radius:5px; background:#42b983; color:#fff; outline:none; font-size:.75em" onclick="openReferenceURL()"><b>开始编辑</b></button>
54121
</div>
55122

0 commit comments

Comments
 (0)