diff --git a/lib/utils/text-direction.ts b/lib/utils/text-direction.ts index 071dd4f9..b198dd06 100644 --- a/lib/utils/text-direction.ts +++ b/lib/utils/text-direction.ts @@ -61,4 +61,34 @@ export function applyTextDirection(htmlContent: string, textContent?: string): s // Otherwise, wrap the content with a direction-aware container return `
${htmlContent}
`; +} + +/** + * Process content to determine direction and return direction-enhanced HTML + * + * @param content Content to process (HTML or plain text) + * @returns Object containing processed HTML and detected direction + */ +export function processContentWithDirection(content: string | undefined): { html: string; direction: 'ltr' | 'rtl' } { + if (!content) { + return { html: '', direction: 'ltr' }; + } + + // Extract text from the content for direction detection + const textContent = content.replace(/<[^>]*>/g, '') + .replace(/ /g, ' ') + .replace(/</g, '<') + .replace(/>/g, '>') + .replace(/&/g, '&'); + + // Detect the direction of the text + const direction = detectTextDirection(textContent); + + // Apply the direction to the HTML content + const directionEnhancedHtml = applyTextDirection(content, textContent); + + return { + html: directionEnhancedHtml, + direction + }; } \ No newline at end of file