Skip to content

Commit 455bfa8

Browse files
committed
Refactor parsing logic for <p> tags in HTML response
1 parent b40f4ea commit 455bfa8

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

response/error.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,20 @@ func parseHTMLResponse(bodyBytes []byte, apiError *APIError, log logger.Logger,
170170
var parse func(*html.Node)
171171
parse = func(n *html.Node) {
172172
if n.Type == html.ElementNode && n.Data == "p" {
173+
var pText strings.Builder
173174
for c := n.FirstChild; c != nil; c = c.NextSibling {
174-
if c.Type == html.TextNode {
175-
// Accumulate text content of <p> tag
176-
messages = append(messages, c.Data)
175+
if c.Type == html.TextNode && strings.TrimSpace(c.Data) != "" {
176+
// Build text content of <p> tag
177+
if pText.Len() > 0 {
178+
pText.WriteString(" ") // Add a space between text nodes within the same <p> tag
179+
}
180+
pText.WriteString(strings.TrimSpace(c.Data))
177181
}
178182
}
183+
if pText.Len() > 0 {
184+
// Add the built text content of the <p> tag to messages
185+
messages = append(messages, pText.String())
186+
}
179187
}
180188
for c := n.FirstChild; c != nil; c = c.NextSibling {
181189
parse(c) // Recursively parse the document

0 commit comments

Comments
 (0)