-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (45 loc) · 1.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
function formatDateTime(dateTimeString) {
const date = new Date(dateTimeString);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
const hours = String(date.getHours()).padStart(2, "0");
const minutes = String(date.getMinutes()).padStart(2, "0");
const seconds = String(date.getSeconds()).padStart(2, "0");
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
const plugin = (hook, vm) => {
const repo = vm.config.repo;
const branch = vm.config.lastModifiedBranch || 'main';
hook.beforeEach(html => {
const { file, path } = vm.route;
let text = vm.config.lastModifiedText || "";
if (typeof text === "object") {
Object.keys(text).some(local => {
const isMatch = path && path.indexOf(local) > -1;
text = isMatch ? text[local] : text;
return isMatch;
});
}
const apiUrl = `https://api.github.com/repos/${repo}/commits?path=${file}`;
fetch(apiUrl, { method: "GET" })
.then(response => response.json())
.then(data => {
const date = data[0].commit.committer.date;
const commitUrl = `https://github.com/${repo}/commits/${branch}/${file}`;
const lastModified = formatDateTime(date);
const lastModifiedContent = `
<blockquote>
<a href="${commitUrl}" target="_blank" style="color: #858585">${text +
lastModified}</a>
</blockquote>
`;
document.getElementById(
"last-modified"
).innerHTML = lastModifiedContent;
});
return html + '<span id="last-modified"></span>';
});
};
window.$docsify = window.$docsify || {};
window.$docsify.plugins = (window.$docsify.plugins || []).concat(plugin);