/**
* Decode HTML entities in strings
* Converts ' " & < > etc. to their actual characters
*/
export function decodeHtmlEntities(text: string): string {
if (!text || typeof text !== 'string') {
return text;
}
// Create a temporary element to decode HTML entities
const textarea = document.createElement('textarea');
textarea.innerHTML = text;
return textarea.value;
}
/**
* Server-side HTML entity decoding (for Node.js/Next.js API routes)
*/
export function decodeHtmlEntitiesServer(text: string): string {
if (!text || typeof text !== 'string') {
return text;
}
// Map of common HTML entities
const entityMap: Record = {
''': "'",
'"': '"',
'&': '&',
'<': '<',
'>': '>',
''': "'",
''': "'",
'/': '/',
'`': '`',
'=': '=',
};
return text.replace(/&[#\w]+;/g, (entity) => {
return entityMap[entity] || entity;
});
}