Fork of Chiri for Astro for my blog
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at c1ddc22aeb5d413d714699c1f7d8cd84a225615e 53 lines 1.5 kB view raw
1import { themeConfig } from '@/config' 2import type { DateFormat } from '@/types' 3 4const MONTHS_EN = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] 5 6const VALID_SEPARATORS = ['.', '-', '/'] 7 8/** 9 * @param date 10 * @param format 11 * @returns 12 */ 13export function formatDate(date: Date, format?: string): string { 14 const formatStr = (format || themeConfig.date.dateFormat).trim() 15 const configSeparator = themeConfig.date.dateSeparator || '-' 16 17 const separator = VALID_SEPARATORS.includes(configSeparator.trim()) ? configSeparator.trim() : '.' 18 19 const year = date.getFullYear() 20 const month = date.getMonth() + 1 21 const day = date.getDate() 22 const monthName = MONTHS_EN[date.getMonth()] 23 24 const pad = (num: number) => String(num).padStart(2, '0') 25 26 switch (formatStr) { 27 case 'YYYY-MM-DD': 28 return `${year}${separator}${pad(month)}${separator}${pad(day)}` 29 30 case 'MM-DD-YYYY': 31 return `${pad(month)}${separator}${pad(day)}${separator}${year}` 32 33 case 'DD-MM-YYYY': 34 return `${pad(day)}${separator}${pad(month)}${separator}${year}` 35 36 case 'MONTH DAY YYYY': 37 return `<span class="month">${monthName}</span> ${day} ${year}` 38 39 case 'DAY MONTH YYYY': 40 return `${day} <span class="month">${monthName}</span> ${year}` 41 42 default: 43 return `${year}${separator}${pad(month)}${separator}${pad(day)}` 44 } 45} 46 47export const SUPPORTED_DATE_FORMATS: readonly DateFormat[] = [ 48 'YYYY-MM-DD', 49 'MM-DD-YYYY', 50 'DD-MM-YYYY', 51 'MONTH DAY YYYY', 52 'DAY MONTH YYYY' 53] as const