// Copyright 2018 The Hugo Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package livereloadinject import ( "bytes" "fmt" "html" "net/url" "strings" "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/transform" ) const warnMessage = `"head" or "body" tag is required in html to append livereload script. ` + "As a fallback, Hugo injects it somewhere but it might not work properly." var warnScript = fmt.Sprintf(``, warnMessage) type tag struct { markup []byte appendScript bool warnRequired bool } var tags = []tag{ {markup: []byte("")}, {markup: []byte("")}, {markup: []byte("`, html.EscapeString(src))) i := idx if match.appendScript { i += bytes.Index(b[i:], []byte(">")) + 1 } if match.warnRequired { script = append(script, []byte(warnScript)...) } c = append(c[:i], append(script, c[i:]...)...) if _, err := ft.To().Write(c); err != nil { loggers.Log().Warnf("Failed to inject LiveReload script:", err) } return nil } }