Skip to content

Commit ae06fba

Browse files
authored
feat: add last modify time at page site (doocs#1650)
1 parent 722b388 commit ae06fba

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

index.html

+36-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<div id="app">LeetCode & Coding Interview Guide @Doocs</div>
2727
<script>
2828
const isEn = () => location.hash.includes('README_EN');
29-
const isRoot = () => ['', '#/', '#/README', '#/README_EN'].includes(location.hash);
29+
const isRoot = () => ['', '#/', '#/README', '#/README_EN'].includes(location.hash);
3030
const sidebar = () => isRoot() ? false : (isEn() ? 'summary_en.md' : 'summary.md');
3131
const cleanedHtml = (html) => {
3232
return html.replace(/<pre>([\s\S]*?)<\/pre>/g, function (match, group) {
@@ -128,6 +128,41 @@
128128
return html + footer
129129
})
130130
},
131+
(hook, vm) => {
132+
hook.beforeEach((html) => {
133+
function formatDateTime(dateTimeString) {
134+
const date = new Date(dateTimeString);
135+
const year = date.getFullYear();
136+
const month = String(date.getMonth() + 1).padStart(2, '0');
137+
const day = String(date.getDate()).padStart(2, '0');
138+
const hours = String(date.getHours()).padStart(2, '0');
139+
const minutes = String(date.getMinutes()).padStart(2, '0');
140+
const seconds = String(date.getSeconds()).padStart(2, '0');
141+
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
142+
}
143+
const file = vm.route.file
144+
const lastIndex = file.lastIndexOf("/");
145+
const filePath = vm.route.path === '/' ? '/README.md' : file.substring(0, lastIndex);
146+
const apiUrl = `https://api.github.com/repos/${$docsify.repo}/commits?path=${filePath}`;
147+
fetch(apiUrl, {method: 'GET'}).then(response => response.json())
148+
.then(data => {
149+
const date = data[0].commit.committer.date
150+
const commitUrl = `https://github.com/${$docsify.repo}/commits/main/${file}`
151+
const lastModified = formatDateTime(date)
152+
const prompt = isEn() ? 'Last updated: ' : '最近更新时间:'
153+
const lastModifiedContent = `
154+
<blockquote>
155+
<a href="${commitUrl}" target="_blank" style="color: #858585">${prompt + lastModified}</a>
156+
</blockquote>
157+
`
158+
document.getElementById('last-modified').innerHTML = lastModifiedContent
159+
})
160+
return (
161+
html + '<span id="last-modified"></span>'
162+
);
163+
});
164+
},
165+
131166
]
132167
}
133168
</script>

0 commit comments

Comments
 (0)