Skip to content

Commit

Permalink
trim trailing spaces if followed by a new line.
Browse files Browse the repository at this point in the history
  • Loading branch information
AmarnathCJD committed Aug 11, 2024
1 parent 6930d6a commit e77c04f
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion telegram/formatting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit e77c04f

Please sign in to comment.