stripHTML removes all html tags from a provided string
(s string)
| 83 | |
| 84 | // stripHTML removes all html tags from a provided string |
| 85 | func (p titleParser) stripHTML(s string) (string, error) { |
| 86 | nodes, err := html.Parse(strings.NewReader(s)) |
| 87 | if err != nil { |
| 88 | // return the original unmodified string in the event html.Parse() |
| 89 | // fails; let the downstream callsites decide if they want to proceed |
| 90 | // with the value or not. |
| 91 | return s, err |
| 92 | } |
| 93 | |
| 94 | var buf bytes.Buffer |
| 95 | var walk func(*html.Node) |
| 96 | walk = func(n *html.Node) { |
| 97 | if n.Type == html.TextNode { |
| 98 | buf.WriteString(n.Data) |
| 99 | } |
| 100 | for c := n.FirstChild; c != nil; c = c.NextSibling { |
| 101 | walk(c) |
| 102 | } |
| 103 | } |
| 104 | walk(nodes) |
| 105 | |
| 106 | return buf.String(), nil |
| 107 | } |