This commit is contained in:
Han
2025-02-17 18:34:21 +08:00
parent 88271e27e6
commit 2c9740dcf7
38 changed files with 389 additions and 87 deletions
+38 -24
View File
@@ -4,41 +4,55 @@ const getDescription = (post: any, num: number = 150) => (post.rendered ? post.r
//处理时间
const fmtTime = (time: any, fmt: string = 'MMMM D, YYYY') => dayjs(time).format(fmt)
// 处理日期
const fmtDate = (time: any) => {
const past = dayjs(time);
const now = dayjs();
const duration = now.diff(past, 'seconds');
if (duration < 60) {
return `${duration} 秒前`;
} else if (duration < 60 * 60) {
const minutes = Math.floor(duration / 60);
return `${minutes} 分钟前`;
} else if (duration < 60 * 60 * 24) {
const hours = Math.floor(duration / (60 * 60));
return `${hours} 小时前`;
} else if (duration < 60 * 60 * 24 * 30) {
const days = Math.floor(duration / (60 * 60 * 24));
return `${days} 天前`;
} else if (duration < 60 * 60 * 24 * 30 * 12) {
const months = Math.floor(duration / (60 * 60 * 24 * 30));
return `${months} 个月前`;
} else {
const years = Math.floor(duration / (60 * 60 * 24 * 30 * 12));
return `${years} 年前`;
}
const fmtDate = (time: string | Date) => {
const now = dayjs()
const past = dayjs(time)
// 计算时间差组件
const years = now.diff(past, 'year')
const months = now.diff(past.add(years, 'year'), 'month')
const days = now.diff(past.add(years, 'year').add(months, 'month'), 'day')
const hours = now.diff(past, 'hour')
const minutes = now.diff(past, 'minute')
const seconds = now.diff(past, 'second')
// 构建时间差描述
return [
years && `${years}年`,
months && `${months}月`,
days && `${days}天`,
!years && !months && hours && `${hours}小时`,
!years && !months && !days && minutes && `${minutes}分钟`,
!years && !months && !days && !hours && `${seconds}秒`
].filter(Boolean).join('')
}
// 处理页码展示
const fmtPage = (page: string | undefined) => page ? page.replace(/\//g, '') : null
// 加载外部脚本
const LoadScript = (src: string): Promise<HTMLScriptElement> => {
const LoadScript = (
src: string,
attrs?: Array<{ k: string; v: string | boolean }>
): Promise<HTMLScriptElement> => {
return new Promise((resolve, reject) => {
const script = document.createElement("script");
script.src = src;
// 添加自定义属性
if (attrs?.length) {
attrs.forEach(({ k, v }) => {
// 处理不同值类型
const value = typeof v === "boolean"
? (v ? "" : null) // 布尔值处理为 HTML 标准属性格式
: String(v); // 其他类型转为字符串
if (value !== null) {
script.setAttribute(k, value);
}
});
}
script.onload = () => resolve(script);
script.onerror = () => reject(new Error(`Failed to load script: ${src}`));
document.head.appendChild(script);
});
}
};
// 加载外部CSS
const LoadStyle = (href: string): Promise<HTMLLinkElement> => {
return new Promise((resolve, reject) => {