From e77c04fe6e044cc77c9f00fe68b00da8241f11f3 Mon Sep 17 00:00:00 2001 From: AmarnathCJD Date: Sun, 11 Aug 2024 13:53:59 +0530 Subject: [PATCH] trim trailing spaces if followed by a new line. --- telegram/formatting.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/telegram/formatting.go b/telegram/formatting.go index bde34944..6b8b422d 100755 --- a/telegram/formatting.go +++ b/telegram/formatting.go @@ -127,13 +127,22 @@ func parseHTMLToTags(htmlStr string) (string, []Tag, error) { return cleanedText, tagOffsets, nil } +func trimTrailing(input string) string { + lastNewlineIndex := strings.LastIndex(input, "\n") + if lastNewlineIndex != -1 && strings.TrimSpace(input[lastNewlineIndex:]) == "" { + return input[:lastNewlineIndex] + } + + return input +} + // getTextLength returns the length of the text content of a node, including its children func getTextLength(n *html.Node) int32 { var tagLength int32 = 0 currentNode := n.FirstChild for currentNode != nil { if currentNode.Type == html.TextNode { - tagLength += utf16RuneCountInString(strings.TrimSpace(currentNode.Data)) + tagLength += utf16RuneCountInString(trimTrailing(currentNode.Data)) } else if currentNode.Type == html.ElementNode { tagLength += getTextLength(currentNode) }