mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-26 21:51:02 +03:00
parent
aebfe156fb
commit
0ad378b09c
6 changed files with 42 additions and 14 deletions
|
@ -16,6 +16,9 @@ package livereloadinject
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/gohugoio/hugo/helpers"
|
||||
"github.com/gohugoio/hugo/transform"
|
||||
|
@ -35,7 +38,8 @@ var tags = []tag{
|
|||
|
||||
// New creates a function that can be used
|
||||
// to inject a script tag for the livereload JavaScript in a HTML document.
|
||||
func New(port int) transform.Transformer {
|
||||
func New(baseURL url.URL) transform.Transformer {
|
||||
|
||||
return func(ft transform.FromTo) error {
|
||||
b := ft.From().Bytes()
|
||||
var idx = -1
|
||||
|
@ -51,6 +55,12 @@ func New(port int) transform.Transformer {
|
|||
}
|
||||
}
|
||||
|
||||
path := strings.TrimSuffix(baseURL.Path, "/")
|
||||
|
||||
src := path + "/livereload.js?mindelay=10&v=2"
|
||||
src += "&port=" + baseURL.Port()
|
||||
src += "&path=" + strings.TrimPrefix(path+"/livereload", "/")
|
||||
|
||||
c := make([]byte, len(b))
|
||||
copy(c, b)
|
||||
|
||||
|
@ -59,7 +69,7 @@ func New(port int) transform.Transformer {
|
|||
return err
|
||||
}
|
||||
|
||||
script := []byte(fmt.Sprintf(`<script src="/livereload.js?port=%d&mindelay=10&v=2" data-no-instant defer></script>`, port))
|
||||
script := []byte(fmt.Sprintf(`<script src="%s" data-no-instant defer></script>`, html.EscapeString(src)))
|
||||
|
||||
i := idx
|
||||
if match.appendScript {
|
||||
|
|
|
@ -15,6 +15,7 @@ package livereloadinject
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
|
@ -25,12 +26,17 @@ import (
|
|||
func TestLiveReloadInject(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
|
||||
expectBase := `<script src="/livereload.js?port=1313&mindelay=10&v=2" data-no-instant defer></script>`
|
||||
lrurl, err := url.Parse("http://localhost:1234/subpath")
|
||||
if err != nil {
|
||||
t.Errorf("Parsing test URL failed")
|
||||
return
|
||||
}
|
||||
expectBase := `<script src="/subpath/livereload.js?mindelay=10&v=2&port=1234&path=subpath/livereload" data-no-instant defer></script>`
|
||||
apply := func(s string) string {
|
||||
out := new(bytes.Buffer)
|
||||
in := strings.NewReader(s)
|
||||
|
||||
tr := transform.New(New(1313))
|
||||
tr := transform.New(New(*lrurl))
|
||||
tr.Apply(out, in)
|
||||
|
||||
return out.String()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue