-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.go
77 lines (62 loc) · 1.7 KB
/
content.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package telegraph
import (
"bytes"
"io"
"strings"
"golang.org/x/net/html"
)
type FilterFunc func(domNode *html.Node) bool
// ContentFormat transforms data to a DOM-based format to represent the content of the page.
func ContentFormat(data any, filters ...FilterFunc) (n []Node, err error) {
var dst *html.Node
switch src := data.(type) {
case string:
dst, err = html.Parse(strings.NewReader(src))
case []byte:
dst, err = html.Parse(bytes.NewReader(src))
case io.Reader:
dst, err = html.Parse(src)
default:
return nil, ErrInvalidDataType
}
if err != nil {
return nil, err
}
if node := domToNode(dst.FirstChild, filters...); node != nil {
n = append(n, node)
}
return n, nil
}
func domToNode(domNode *html.Node, filters ...FilterFunc) any {
for _, filter := range filters {
if filter(domNode) {
return nil
}
}
if domNode.Type == html.TextNode {
return domNode.Data
}
if domNode.Type != html.ElementNode {
return nil
}
nodeElement := new(NodeElement)
switch strings.ToLower(domNode.Data) {
case "a", "aside", "b", "blockquote", "br", "code", "em", "figcaption", "figure", "h3", "h4", "hr", "i",
"iframe", "img", "li", "ol", "p", "pre", "s", "strong", "u", "ul", "video":
nodeElement.Tag = domNode.Data
for i := range domNode.Attr {
switch strings.ToLower(domNode.Attr[i].Key) {
case "href", "src":
nodeElement.Attrs = map[string]string{domNode.Attr[i].Key: domNode.Attr[i].Val}
default:
continue
}
}
}
for child := domNode.FirstChild; child != nil; child = child.NextSibling {
if node := domToNode(child, filters...); node != nil {
nodeElement.Children = append(nodeElement.Children, domToNode(child, filters...))
}
}
return nodeElement
}