Files
2026-10-08 11:09:37 +08:00

30 lines
1009 B
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
/** 合并 className:clsx 处理条件类,tailwind-merge 消解冲突的 Tailwind 类 */
export function cn(...inputs: ClassValue[]): string {
return twMerge(clsx(inputs));
}
/** 将日期格式化为中文可读形式,如 2025 年 3 月 8 日 */
export function formatDate(date: string, locale = "zh-CN"): string {
return new Intl.DateTimeFormat(locale, {
year: "numeric",
month: "long",
day: "numeric",
timeZone: "UTC",
}).format(new Date(`${date}T00:00:00Z`));
}
/** 机器可读日期,用于 <time dateTime> */
export function toDateTimeAttribute(date: string): string {
return new Date(`${date}T00:00:00Z`).toISOString();
}
/** 拼接绝对 URL,用于 canonical / OG / sitemap */
export function absoluteUrl(base: string, pathname: string): string {
const base$ = base.replace(/\/+$/, "");
const path$ = pathname.startsWith("/") ? pathname : `/${pathname}`;
return `${base$}${path$}`;
}