2025-12-15 17:34:12 +08:00
|
|
|
export function truncate(text: string, maxLength: number): string {
|
|
|
|
|
if (text.length > maxLength) {
|
|
|
|
|
return text.substring(0, maxLength) + '...'
|
|
|
|
|
}
|
|
|
|
|
return text
|
|
|
|
|
}
|
2026-01-14 15:15:51 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Escape HTML special characters to prevent XSS and rendering issues
|
|
|
|
|
*/
|
|
|
|
|
export function escapeHtml(text: string): string {
|
|
|
|
|
return text
|
|
|
|
|
.replace(/&/g, '&')
|
|
|
|
|
.replace(/</g, '<')
|
|
|
|
|
.replace(/>/g, '>')
|
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
|
.replace(/'/g, ''')
|
|
|
|
|
}
|