summaryrefslogtreecommitdiffstats
path: root/commands/hugo.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2017-06-26 21:34:16 +0200
committerGitHub <noreply@github.com>2017-06-26 21:34:16 +0200
commitc825a7312131b4afa67ee90d593640dee3525d98 (patch)
tree3b60e5dafd771a71b3d4d4b278759d78c34cc7b8 /commands/hugo.go
parent7198ea8a1e575644886c4fe027a41b43cfc1ea5b (diff)
Support open "current content page" in browser
This commit adds a new `--navigateToChanged` and config setting with the same name, that, when running the Hugo server with live reload enabled, will navigate to the current content file's URL on save. This is really useful for site-wide content changes (copyedits etc.). Fixes #3643
Diffstat (limited to 'commands/hugo.go')
-rw-r--r--commands/hugo.go34
1 files changed, 32 insertions, 2 deletions
diff --git a/commands/hugo.go b/commands/hugo.go
index b965ba167..42e129cf1 100644
--- a/commands/hugo.go
+++ b/commands/hugo.go
@@ -981,8 +981,28 @@ func (c *commandeer) newWatcher(port int) error {
}
if !buildWatch && !c.Cfg.GetBool("disableLiveReload") {
- // Will block forever trying to write to a channel that nobody is reading if livereload isn't initialized
- livereload.ForceRefresh()
+
+ navigate := c.Cfg.GetBool("navigateToChanged")
+
+ var p *hugolib.Page
+
+ if navigate {
+
+ // It is probably more confusing than useful
+ // to navigate to a new URL on RENAME etc.
+ // so for now we use the WRITE event only.
+ name := pickOneWritePath(dynamicEvents)
+
+ if name != "" {
+ p = Hugo.GetContentPage(name)
+ }
+ }
+
+ if p != nil {
+ livereload.NavigateToPath(p.RelPermalink())
+ } else {
+ livereload.ForceRefresh()
+ }
}
}
case err := <-watcher.Errors:
@@ -1007,6 +1027,16 @@ func (c *commandeer) newWatcher(port int) error {
return nil
}
+func pickOneWritePath(events []fsnotify.Event) string {
+ for _, ev := range events {
+ if ev.Op&fsnotify.Write == fsnotify.Write {
+ return ev.Name
+ }
+ }
+
+ return ""
+}
+
func (c *commandeer) isStatic(path string) bool {
return strings.HasPrefix(path, c.PathSpec().GetStaticDirPath()) || (len(c.PathSpec().GetThemesDirPath()) > 0 && strings.HasPrefix(path, c.PathSpec().GetThemesDirPath()))
}