Fix the "page picker" logic in --navigateToChanged
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 17 Oct 2021 14:11:00 +0000 (16:11 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 18 Oct 2021 10:13:13 +0000 (12:13 +0200)
Fixes #9051

commands/hugo.go
hugofs/files/classifier.go

index fbe2349a0e3c4a50739966850b11e59c3c81de4f..4f011a33ba3b6d9627f2459bddcf9ec597477e46 100644 (file)
@@ -30,6 +30,8 @@ import (
        "syscall"
        "time"
 
+       "github.com/gohugoio/hugo/hugofs/files"
+
        "github.com/gohugoio/hugo/common/types"
 
        "github.com/gohugoio/hugo/hugofs"
@@ -1200,12 +1202,16 @@ func partitionDynamicEvents(sourceFs *filesystems.SourceFilesystems, events []fs
 func pickOneWriteOrCreatePath(events []fsnotify.Event) string {
        name := ""
 
-       // Some editors (for example notepad.exe on Windows) triggers a change
-       // both for directory and file. So we pick the longest path, which should
-       // be the file itself.
        for _, ev := range events {
-               if (ev.Op&fsnotify.Write == fsnotify.Write || ev.Op&fsnotify.Create == fsnotify.Create) && len(ev.Name) > len(name) {
-                       name = ev.Name
+               if ev.Op&fsnotify.Write == fsnotify.Write || ev.Op&fsnotify.Create == fsnotify.Create {
+                       if files.IsIndexContentFile(ev.Name) {
+                               return ev.Name
+                       }
+
+                       if files.IsContentFile(ev.Name) {
+                               name = ev.Name
+                       }
+
                }
        }
 
index f0e0911ab9b5e48b4fedaf69a9de0c2e53149546..aab199850b2cd381013e72a5d5b44c90085fc328 100644 (file)
@@ -69,6 +69,16 @@ func IsContentFile(filename string) bool {
        return contentFileExtensionsSet[strings.TrimPrefix(filepath.Ext(filename), ".")]
 }
 
+func IsIndexContentFile(filename string) bool {
+       if !IsContentFile(filename) {
+               return false
+       }
+
+       base := filepath.Base(filename)
+
+       return strings.HasPrefix(base, "index.") || strings.HasPrefix(base, "_index.")
+}
+
 func IsHTMLFile(filename string) bool {
        return htmlFileExtensionsSet[strings.TrimPrefix(filepath.Ext(filename), ".")]
 }