diff options
author | sth <[email protected]> | 2020-12-02 12:52:26 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-12-02 12:52:26 +0100 |
commit | 0ad378b09cea90a2a70d7ff06af668abe22475a1 (patch) | |
tree | b8513e229047459cb2337e6dff69aa26640039db /transform | |
parent | aebfe156fb2f27057e61b2e50c7576e6b06dab58 (diff) | |
download | hugo-0ad378b09cea90a2a70d7ff06af668abe22475a1.tar.gz hugo-0ad378b09cea90a2a70d7ff06af668abe22475a1.zip |
Use --baseURL path for live-reload URL
Fixes #6595
Diffstat (limited to 'transform')
-rw-r--r-- | transform/livereloadinject/livereloadinject.go | 14 | ||||
-rw-r--r-- | transform/livereloadinject/livereloadinject_test.go | 10 |
2 files changed, 20 insertions, 4 deletions
diff --git a/transform/livereloadinject/livereloadinject.go b/transform/livereloadinject/livereloadinject.go index a6e56ac41..cc80d046e 100644 --- a/transform/livereloadinject/livereloadinject.go +++ b/transform/livereloadinject/livereloadinject.go @@ -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 { diff --git a/transform/livereloadinject/livereloadinject_test.go b/transform/livereloadinject/livereloadinject_test.go index 59124f20b..50fbb8b92 100644 --- a/transform/livereloadinject/livereloadinject_test.go +++ b/transform/livereloadinject/livereloadinject_test.go @@ -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() |