-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.go
More file actions
38 lines (34 loc) · 816 Bytes
/
script.go
File metadata and controls
38 lines (34 loc) · 816 Bytes
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
package magic
import (
"bytes"
"compress/gzip"
_ "embed"
"regexp"
"strings"
)
//go:embed script/magic.min.js
var magicMinScript []byte
var magicMinScriptWithTags []byte
var regexpHeadTag = regexp.MustCompile("<head.*>")
func init() {
magicMinScriptWithTags = append(append([]byte("<script magic:inject>"), magicMinScript...), []byte("</script>")...)
{
buf := &bytes.Buffer{}
writer, err := gzip.NewWriterLevel(buf, gzip.BestCompression)
if err != nil {
panic(err)
}
if _, err = writer.Write(magicMinScript); err != nil {
panic(err)
}
if err = writer.Close(); err != nil {
panic(err)
}
}
}
func injectLiveScript(templ string) string {
return regexpHeadTag.ReplaceAllStringFunc(templ, func(s string) string {
s = strings.Replace(s, ">", ">{{magic:inject}}", 1)
return s
})
}