fix: Properly decode HTML entities in chat messages
Some checks failed
Dev Deployment (Zero Downtime) / deploy-dev (push) Has been cancelled
Some checks failed
Dev Deployment (Zero Downtime) / deploy-dev (push) Has been cancelled
- Fix ' not being decoded to apostrophe - Decode HTML entities when loading messages from localStorage - Improve server-side HTML entity decoding to handle all variations - Replace hardcoded ' in static text with regular apostrophes - Add support for more HTML entity variations (rsquo, lsquo, etc.)
This commit is contained in:
@@ -21,7 +21,7 @@ export function decodeHtmlEntitiesServer(text: string): string {
|
||||
return text;
|
||||
}
|
||||
|
||||
// Map of common HTML entities
|
||||
// Map of common HTML entities (including all variations of apostrophe)
|
||||
const entityMap: Record<string, string> = {
|
||||
''': "'",
|
||||
'"': '"',
|
||||
@@ -33,9 +33,26 @@ export function decodeHtmlEntitiesServer(text: string): string {
|
||||
'/': '/',
|
||||
'`': '`',
|
||||
'=': '=',
|
||||
'’': "'",
|
||||
'‘': "'",
|
||||
'”': '"',
|
||||
'“': '"',
|
||||
};
|
||||
|
||||
return text.replace(/&[#\w]+;/g, (entity) => {
|
||||
return entityMap[entity] || entity;
|
||||
// First replace known entities
|
||||
let decoded = text;
|
||||
for (const [entity, replacement] of Object.entries(entityMap)) {
|
||||
decoded = decoded.replace(new RegExp(entity, 'gi'), replacement);
|
||||
}
|
||||
|
||||
// Then handle numeric entities (' ' etc.)
|
||||
decoded = decoded.replace(/&#(\d+);/g, (match, num) => {
|
||||
return String.fromCharCode(parseInt(num, 10));
|
||||
});
|
||||
|
||||
decoded = decoded.replace(/&#x([0-9a-f]+);/gi, (match, hex) => {
|
||||
return String.fromCharCode(parseInt(hex, 16));
|
||||
});
|
||||
|
||||
return decoded;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user