forked from go-shiori/obelisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
107 lines (88 loc) · 2.33 KB
/
utils.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package obelisk
import (
"encoding/base64"
"fmt"
nurl "net/url"
"reflect"
"regexp"
"strings"
"unsafe"
)
var (
rxStyleURL = regexp.MustCompile(`(?i)^url\((.+)\)$`)
)
// isValidURL checks if URL is valid.
func isValidURL(s string) bool {
_, err := nurl.ParseRequestURI(s)
return err == nil
}
// createAbsoluteURL convert url to absolute path based on base.
func createAbsoluteURL(url string, base *nurl.URL) string {
url = strings.TrimSpace(url)
if url == "" || base == nil {
return ""
}
// If it is data url, return as it is
if strings.HasPrefix(url, "data:") {
return url
}
// If it is fragment path, return as it is
if strings.HasPrefix(url, "#") {
return url
}
// If it is already an absolute URL, clean the URL then return it
tmp, err := nurl.ParseRequestURI(url)
if err == nil && tmp.Scheme != "" && tmp.Hostname() != "" {
cleanURL(tmp)
return tmp.String()
}
// Otherwise, resolve against base URL.
tmp, err = nurl.Parse(url)
if err != nil {
return url
}
cleanURL(tmp)
return base.ResolveReference(tmp).String()
}
// cleanURL removes fragment (#fragment) and UTM queries from URL
func cleanURL(url *nurl.URL) {
queries := url.Query()
for key := range queries {
if strings.HasPrefix(key, "utm_") {
queries.Del(key)
}
}
url.Fragment = ""
url.RawQuery = queries.Encode()
}
// sanitizeStyleURL sanitizes the URL in CSS by removing `url()`,
// quotation mark and trailing slash
func sanitizeStyleURL(url string) string {
cssURL := rxStyleURL.ReplaceAllString(url, "$1")
cssURL = strings.TrimSpace(cssURL)
if strings.HasPrefix(cssURL, `"`) {
return strings.Trim(cssURL, `"`)
}
if strings.HasPrefix(cssURL, `'`) {
return strings.Trim(cssURL, `'`)
}
return cssURL
}
// createDataURL returns base64 encoded data URL
func createDataURL(content []byte, contentType string) string {
b64encoded := base64.StdEncoding.EncodeToString(content)
return fmt.Sprintf("data:%s;base64,%s", contentType, b64encoded)
}
// s2b converts string to a byte slice without memory allocation.
func s2b(s string) (b []byte) {
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh.Data = sh.Data
bh.Cap = sh.Len
bh.Len = sh.Len
return b
}
// b2s converts byte slice to a string without memory allocation.
func b2s(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}