@scania/tegel
Version:
Tegel Design System
31 lines (30 loc) • 1.04 kB
JavaScript
/**
* Processes HTML content to make it more accessible for screen readers by:
* 1. Removing HTML tags while preserving their text content
* 2. Converting HTML entities to their text equivalents
* 3. Normalizing whitespace
*
* @param htmlContent - The HTML content to be processed for screen readers
* @returns Clean, readable text suitable for screen readers
*
* @example
* // Input: "<p>Hello <b>World</b> & Universe!</p>"
* // Output: "Hello World and Universe!"
*/
export const processHtmlForScreenReader = (htmlContent) => {
if (!htmlContent)
return '';
return (htmlContent
// Remove HTML tags
.replace(/<[^>]*>/g, ' ')
// Replace common HTML entities with their text equivalents
.replace(/ /g, ' ')
.replace(/&/g, 'and')
.replace(/</g, 'less than')
.replace(/>/g, 'greater than')
.replace(/"/g, '"')
.replace(/'/g, "'")
// Normalize whitespace
.replace(/\s+/g, ' ')
.trim());
};