(b *strings.Builder, n *html.Node)
| 37 | return "<div>" + escaped + "</div><br>" + content |
| 38 | } |
| 39 | |
| 40 | // ExtractImageURLs finds image URLs from <img src> tags and |
| 41 | // <figure data-trix-attachment> elements. |
| 42 | func ExtractImageURLs(s string) []string { |
| 43 | doc, err := html.Parse(strings.NewReader(s)) |
| 44 | if err != nil { |
| 45 | return nil |
| 46 | } |
| 47 | var urls []string |
| 48 | findImages(doc, &urls, 0) |
| 49 | return urls |
| 50 | } |
| 51 | |
| 52 | // Attachment describes a downloadable file embedded in rich-text content. |
| 53 | type Attachment struct { |
| 54 | URL string |
| 55 | Filename string |
| 56 | ContentType string |
| 57 | ByteSize *int64 |
| 58 | SGID string |
| 59 | } |
| 60 | |
| 61 | // ExtractAttachments returns downloadable files in their document order. |
| 62 | func ExtractAttachments(s string) []Attachment { |
| 63 | doc, err := html.Parse(strings.NewReader(s)) |
| 64 | if err != nil { |
| 65 | return nil |
| 66 | } |
| 67 | var attachments []Attachment |
| 68 | findAttachments(doc, &attachments) |
| 69 | return attachments |
| 70 | } |
| 71 | |
| 72 | func walkNode(b *strings.Builder, n *html.Node, depth int) { |
| 73 | switch n.Type { //nolint:exhaustive // only text and element nodes need handling |
| 74 | case html.TextNode: |
| 75 | b.WriteString(n.Data) |
| 76 | case html.ElementNode: |
| 77 | switch n.Data { |
| 78 | case "script", "style": |
| 79 | return |
| 80 | case "br": |
| 81 | b.WriteString("\n") |
| 82 | case "img": |
| 83 | alt := getAttr(n, "alt") |
| 84 | if alt != "" { |
| 85 | fmt.Fprintf(b, "[%s]", alt) |
| 86 | } else { |
| 87 | b.WriteString("[image]") |
| 88 | } |
| 89 | return |
| 90 | case "action-text-attachment": |
| 91 | filename := getAttr(n, "filename") |
no test coverage detected