package main
import (
"fmt"
"strings"
"golang.org/x/net/html"
)
// ExtractSrc recursively searches for the src attribute in the HTML tree
func ExtractSrc(n *html.Node) string {
if n.Type == html.ElementNode && n.Data == "img" {
return getSrcAttribute(n)
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
if src := ExtractSrc(c); src != "" {
return src
}
}
return ""
}
// getSrcAttribute extracts the src attribute value from an HTML node
func getSrcAttribute(n *html.Node) string {
for i, attr := range n.Attr {
if attr.Key == "src" {
// Replace the value with the public URL
n.Attr[i].Val = "https://api.s.sicepatconsumer.com/v1/file/public/6b932af4-dd16-4126-9f6e-ba0901321331"
return n.Attr[i].Val
}
}
return ""
}
func main() {
htmlContent := `
<p>Willy</p>
<p></p>
<img src="/super-app/1713254482/1713254482) (1).jpg" alt="undefined" style="height: auto;width: auto"/>
<p></p>
`
// Parse the HTML content
doc, err := html.Parse(strings.NewReader(htmlContent))
if err != nil {
fmt.Println("Error parsing HTML:", err)
return
}
// Extract src attribute value and replace with public URL
src := ExtractSrc(doc)
if src != "" {
fmt.Println("Image src:", src)
} else {
fmt.Println("Image src not found")
}
// Output the modified HTML content
htmlRender := &strings.Builder{}
html.Render(htmlRender, doc)
fmt.Println("Modified HTML:", htmlRender.String())
}