-
Notifications
You must be signed in to change notification settings - Fork 0
/
log2.js
96 lines (83 loc) · 2.5 KB
/
log2.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* @Author: 简隐 [email protected]
* @Date: 2024-03-31 19:15:45
* @LastEditors: 简隐 [email protected]
* @LastEditTime: 2024-04-16 22:28:34
* @FilePath: \latest-blogs\log2.js
* @Description:
*
* Copyright (c) 2024 by 简隐, All Rights Reserved.
*/
// const gitlog = require("gitlog").default;
import gitlog from 'gitlog'
import { computed } from 'vue'
import moment from 'moment/moment'
const VirtualModuleID = 'virtual:git-changelog'
const ResolvedVirtualModuleId = `${VirtualModuleID}`
export default function GitChangelogMarkdownSection(options = {}) {
let commits = {}
return {
name: '@jadeq/vitefdsfsfpress-plugin-markdown-changelog',
buildStart () {
commits = gitlog.default({
// repo: './',
branch: 'master',
number: 1000000,
includeMergeCommitFiles: true,
fields: ["hash", "abbrevHash", "subject", "authorName", "authorDate", "committerName", "committerDate", "authorDateRel", "tag", 'rawBody'],
...options
})
commits = generateFileCommits(commits)
},
resolveId(id) {
if (id === ResolvedVirtualModuleId) {
return ResolvedVirtualModuleId
}
},
load(id) {
if (id !== ResolvedVirtualModuleId)
return null
const changelogData = {
commits,
}
return `export default ${JSON.stringify(changelogData)}`
},
}
}
function commitSortByDate (commits) {
return commits.sort(function (a, b) {
return new Date(a.authorDate) - new Date(b.authorDate) < 0 ? 1 : -1;
});
}
function toChinese(str) {
const matches = str.match(/(\\\d{3}){3}/g);
if (matches) matches.forEach(match => {
let decoded = '';
const splits = match.split('\\');
splits.forEach(code => !code || (decoded += '%' + parseInt(code, 8).toString(16)));
const cChar = decodeURI(decoded);
str = str.replace(match, cChar);
});
// 删除首尾的双引号
str = str.replace(/^"|"$/g, '')
return str;
}
function generateFileCommits (commits) {
return commits.reduce((pre, cur) => {
cur.files.map((originFile) => {
let file = toChinese(originFile)
if (!file.startsWith("docs") || !file.endsWith(".md")) {
return false;
}
if (!pre[file]) {
pre[file] = [];
}
delete cur.files;
delete cur.status;
cur.committerDate = moment(new Date(cur.committerDate)).format('YYYY-MM-DD HH:mm:ss')
pre[file].push(cur);
pre[file] = commitSortByDate(pre[file])
});
return pre;
}, {});
}