-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathimage_url_filter.go
63 lines (53 loc) · 1.33 KB
/
image_url_filter.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
package pipeline
import (
"net/url"
"github.com/PuerkitoBio/goquery"
)
// ImageURLFilter will match image src and replace with custom format.
type ImageURLFilter struct {
// IgnoreHosts, Host list that will ignore, ["your-host.com", "your-assets.com"], if empty will match all.
IgnoreHosts []string
// MatchHosts, ["some-host.com", "some-assets.com"], host list that match will do format,
// If present will ignore IgnoreHosts rules,
// Otherwice will use IgnoreHosts rules.
MatchHosts []string
// Format method with
Format func(src string) string
}
// Call render
func (f ImageURLFilter) Call(doc *goquery.Document) (err error) {
doc.Find("img").Each(func(i int, node *goquery.Selection) {
src := node.AttrOr("src", "")
if src == "" {
// skip empty src
return
}
// Fix src that not URL Schema
srcURL, err := url.Parse(src)
// ignore invaild src
if err != nil {
return
}
if srcURL.Host == "" {
// skip relative url
return
}
if srcURL.Scheme == "" {
srcURL.Scheme = "https"
src = srcURL.String()
}
var matched = false
if len(f.MatchHosts) > 0 {
// If has MatchHosts, match first
matched = isHost(f.MatchHosts, src)
} else {
// ignore IgnoreHosts
matched = !isHost(f.IgnoreHosts, src)
}
if matched {
newSrc := f.Format(src)
node.SetAttr("src", newSrc)
}
})
return
}