ToText converts HTML content to plain text, preserving basic structure.
(s string)
| 10 | |
| 11 | // ToText converts HTML content to plain text, preserving basic structure. |
| 12 | func ToText(s string) string { |
| 13 | doc, err := html.Parse(strings.NewReader(s)) |
| 14 | if err != nil { |
| 15 | return s |
| 16 | } |
| 17 | var b strings.Builder |
| 18 | walkNode(&b, doc) |
| 19 | // Collapse runs of 3+ newlines into 2 |
| 20 | result := b.String() |
| 21 | for strings.Contains(result, "\n\n\n") { |
| 22 | result = strings.ReplaceAll(result, "\n\n\n", "\n\n") |
| 23 | } |
| 24 | return strings.TrimSpace(result) |
| 25 | } |
| 26 | |
| 27 | // ExtractImageURLs finds image URLs from <img src> tags and |
| 28 | // <figure data-trix-attachment> elements. |