Files
mypage/lib/utils.ts
RJ 6e5d641c06
All checks were successful
Build and Deploy Next.js Blog to Production / 🔍 Code Quality Checks (push) Successful in 17s
Build and Deploy Next.js Blog to Production / 🏗️ Build and Push Docker Image (push) Successful in 30s
Build and Deploy Next.js Blog to Production / 🚀 Deploy to Production (push) Successful in 48s
📄 a couple updates
2025-12-02 16:10:33 +00:00

82 lines
2.0 KiB
TypeScript

export function formatDate(dateString: string): string {
const date = new Date(dateString)
const months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
]
return `${date.getDate()} ${months[date.getMonth()]} ${date.getFullYear()}`
}
export function formatRelativeDate(dateString: string): string {
const date = new Date(dateString)
const now = new Date()
const diffTime = Math.abs(now.getTime() - date.getTime())
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24))
if (diffDays === 0) return 'today'
if (diffDays === 1) return 'yesterday'
if (diffDays < 7) {
const days = diffDays
return `${days} day${days > 1 ? 's' : ''} ago`
}
if (diffDays < 30) {
const weeks = Math.floor(diffDays / 7)
return `${weeks} week${weeks > 1 ? 's' : ''} ago`
}
if (diffDays < 365) {
const months = Math.floor(diffDays / 30)
return `${months} month${months > 1 ? 's' : ''} ago`
}
const years = Math.floor(diffDays / 365)
return `${years} year${years > 1 ? 's' : ''} ago`
}
export function generateExcerpt(content: string, maxLength = 160): string {
const text = content
.replace(/^---[\s\S]*?---/, '')
.replace(/!\[.*?\]\(.*?\)/g, '')
.replace(/\[([^\]]+)\]\([^)]+\)/g, '$1')
.replace(/[#*`]/g, '')
.trim()
if (text.length <= maxLength) return text
const truncated = text.slice(0, maxLength)
const lastSpace = truncated.lastIndexOf(' ')
return truncated.slice(0, lastSpace) + '...'
}
export function generateSlug(title: string): string {
const romanianMap: Record<string, string> = {
ă: 'a',
â: 'a',
î: 'i',
ș: 's',
ț: 't',
Ă: 'a',
Â: 'a',
Î: 'i',
Ș: 's',
Ț: 't',
}
return title
.split('')
.map(char => romanianMap[char] || char)
.join('')
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '')
}