hugo/transform/livereloadinject.go

24 lines
608 B
Go
Raw Normal View History

package transform
import (
"bytes"
"github.com/spf13/viper"
)
2015-03-18 12:33:12 -04:00
func LiveReloadInject(rw contentRewriter) {
match := []byte("</body>")
port := viper.GetString("port")
replace := []byte(`<script>document.write('<script src="http://'
+ (location.host || 'localhost').split(':')[0]
+ ':` + port + `/livereload.js?mindelay=10"></'
+ 'script>')</script></body>`)
newcontent := bytes.Replace(rw.Content(), match, replace, -1)
if len(newcontent) == len(rw.Content()) {
match := []byte("</BODY>")
newcontent = bytes.Replace(rw.Content(), match, replace, -1)
}
rw.Write(newcontent)
}