-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
187 lines (172 loc) · 5.79 KB
/
index.ts
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import type {ThemeableImage} from '../../composables/config'
import {getTextRef} from "../../composables/config/i18nRef";
import {defaultLang} from "../../composables/config/i18n";
export function shuffleArray(arr: any[]) {
const array = [...arr]
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]]
}
return array
}
export function formatDate(d: any, fmt = 'yyyy-MM-dd hh:mm:ss') {
if (!(d instanceof Date)) {
d = new Date(d)
}
const o: any = {
'M+': d.getMonth() + 1, // 月份
'd+': d.getDate(), // 日
'h+': d.getHours(), // 小时
'm+': d.getMinutes(), // 分
's+': d.getSeconds(), // 秒
'q+': Math.floor((d.getMonth() + 3) / 3), // 季度
'S': d.getMilliseconds() // 毫秒
}
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(
RegExp.$1,
`${d.getFullYear()}`.substr(4 - RegExp.$1.length)
)
}
// eslint-disable-next-line no-restricted-syntax
for (const k in o) {
if (new RegExp(`(${k})`).test(fmt))
fmt = fmt.replace(
RegExp.$1,
RegExp.$1.length === 1 ? o[k] : `00${o[k]}`.substr(`${o[k]}`.length)
)
}
return fmt
}
export function isCurrentWeek(date: Date, target?: Date) {
const now = target || new Date()
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate())
const oneDay = 1000 * 60 * 60 * 24
const nowWeek = today.getDay()
// 本周一的时间
const startWeek = today.getTime() - (nowWeek === 0 ? 6 : nowWeek - 1) * oneDay
return +date >= startWeek && +date <= startWeek + 7 * oneDay
}
export function formatShowDate(date: Date | string) {
const source = date ? +new Date(date) : +new Date()
const now = +new Date()
const diff = now - source
const oneSeconds = 1000
const oneMinute = oneSeconds * 60
const oneHour = oneMinute * 60
const oneDay = oneHour * 24
const oneWeek = oneDay * 7
if (diff < oneMinute) {
return `${Math.floor(diff / oneSeconds)}${getTextRef('time.secondsAgo')}`
}
if (diff < oneHour) {
return `${Math.floor(diff / oneMinute)}${getTextRef('time.minutesAgo')}`
}
if (diff < oneDay) {
return `${Math.floor(diff / oneHour)}${getTextRef('time.hoursAgo')}`
}
if (diff < oneWeek) {
return `${Math.floor(diff / oneDay)}${getTextRef('time.daysAgo')}`
}
return formatDate(new Date(date), 'yyyy-MM-dd')
}
const pattern
= /[a-zA-Z0-9_\u0392-\u03C9\u00C0-\u00FF\u0600-\u06FF\u0400-\u04FF]+|[\u4E00-\u9FFF\u3400-\u4DBF\uF900-\uFAFF\u3040-\u309F\uAC00-\uD7AF]+/g
// copy from https://github.com/youngjuning/vscode-juejin-wordcount/blob/main/count-word.ts
export default function countWord(data: string) {
const m = data.match(pattern)
let count = 0
if (!m) {
return 0
}
for (let i = 0; i < m.length; i += 1) {
if (m[i].charCodeAt(0) >= 0x4E00) {
count += m[i].length
} else {
count += 1
}
}
return count
}
export function chineseSearchOptimize(input: string) {
return input
.replace(/[\u4E00-\u9FA5]/g, ' $& ')
.replace(/\s+/g, ' ')
.trim()
}
/**
* 根据Github地址跨域获取最后更新时间
* @param url
* @returns
*/
export function getGithubUpdateTime(url: string) {
// 提取Github url中的用户名和仓库名
const match = url.match(/github.com\/(.+)/)
if (!match?.[1]) {
return Promise.reject(new Error('Github地址格式错误'))
}
const [owner, repo] = match[1].split('/')
return fetch(`https://api.github.com/repos/${owner}/${repo}`)
.then(res => res.json())
.then((res) => {
return res.updated_at
})
}
/**
* 跨域获取某个Github仓库的指定目录最后更新时间
*/
export function getGithubDirUpdateTime(
owner: string,
repo: string,
dir?: string,
branch?: string
) {
let baseUrl = `https://api.github.com/repos/${owner}/${repo}/commits`
if (branch) {
baseUrl += `/${branch}`
}
if (dir) {
baseUrl += `?path=${dir}`
}
return fetch(baseUrl)
.then(res => res.json())
.then((res) => {
return [res].flat()[0].commit.committer.date
})
}
// 解析页面获取最后更新时间(跨域)
// export async function getGithubUpdateTime(url: string) {
// const res = await fetch(url)
// const html = await res.text()
// const match = html.match(/<relative-time datetime="(.+?)"/)
// if (match) {
// return match[1]
// }
// return ''
// }
export function getImageUrl(
image: ThemeableImage,
isDarkMode: boolean
): string {
if (typeof image === 'string') {
// 如果 ThemeableImage 类型为 string,则直接返回字符串
return image
}
if ('src' in image) {
// 如果 ThemeableImage 类型是一个对象,并且对象有 src 属性,则返回 src 属性对应的字符串
return image.src
}
if ('light' in image && 'dark' in image) {
// 如果 ThemeableImage 类型是一个对象,并且对象同时有 light 和 dark 属性,则根据 isDarkMode 返回对应的 URL
return isDarkMode ? image.dark : image.light
} // 如果 ThemeableImage 类型不是上述情况,则返回空字符串
return ''
}
// 因为这里有些稀奇古怪的问题,路由无法正确重写,所以手动操作一下,貌似是主题的bug?还是vitepress的bug?
export function wrapperCleanUrls(cleanUrls: boolean, route: string) {
const tempUrl = route.replace(/\.html$/, '')
return cleanUrls ? tempUrl : `${tempUrl}.html`
}
export function replaceValue(str: string, value: any) {
return str.replace(/\{\{value\}\}/, value)
}