diff --git a/tg_bot/screens/ui/message_format.go b/tg_bot/screens/ui/message_format.go index 17003f1..a3fbb35 100644 --- a/tg_bot/screens/ui/message_format.go +++ b/tg_bot/screens/ui/message_format.go @@ -252,15 +252,32 @@ func normalizeHTMLTags(input string) string { continue } - if isClosing { - if len(stack) > 0 && stack[len(stack)-1] == tagName { - stack = stack[:len(stack)-1] - out.WriteString(input[i : end+1]) + if isClosing { + // Find the tag in stack + foundIndex := -1 + for j := len(stack) - 1; j >= 0; j-- { + if stack[j] == tagName { + foundIndex = j + break } - } else { - stack = append(stack, tagName) - out.WriteString(input[i : end+1]) } + + if foundIndex >= 0 { + // Close all tags from top of stack down to foundIndex + for j := len(stack) - 1; j > foundIndex; j-- { + out.WriteString("") + } + // Close the found tag + out.WriteString(input[i : end+1]) + // Remove closed tags from stack + stack = stack[:foundIndex] + } + } else { + stack = append(stack, tagName) + out.WriteString(input[i : end+1]) + } i = end + 1 }