feat: support folder index pages

This commit is contained in:
2026-04-29 09:36:52 -04:00
parent 8c454de809
commit d371c3c602
7 changed files with 223 additions and 21 deletions

View File

@@ -104,10 +104,22 @@ func (s *Service) LoadPage(ctx context.Context, requestPath string) (*Page, erro
return nil, err
}
normalized := normalizeRequestPath(requestPath)
record, err := s.repo.GetDocumentByPath(ctx, normalized)
if err != nil {
return nil, err
var (
record *DocumentRecord
normalized string
lastErr error
)
for _, candidate := range normalizeRequestPathCandidates(requestPath) {
found, err := s.repo.GetDocumentByPath(ctx, candidate)
if err == nil {
record = found
normalized = candidate
break
}
lastErr = err
}
if record == nil {
return nil, lastErr
}
content, err := s.store.Read(record.CurrentHash)
@@ -129,15 +141,15 @@ func (s *Service) LoadPage(ctx context.Context, requestPath string) (*Page, erro
}, nil
}
func normalizeRequestPath(path string) string {
func normalizeRequestPathCandidates(path string) []string {
path = strings.Trim(path, "/")
if path == "" {
return "getting-started.md"
return []string{"index.md", "getting-started.md"}
}
if !strings.HasSuffix(path, ".md") {
path += ".md"
return []string{path + ".md", path + "/index.md"}
}
return path
return []string{path}
}
func (s *Service) syncFile(ctx context.Context, path string) (*DocumentChange, error) {

View File

@@ -0,0 +1,39 @@
package docs
import (
"reflect"
"testing"
)
func TestNormalizeRequestPathCandidatesSupportsIndexFiles(t *testing.T) {
tests := []struct {
name string
path string
want []string
}{
{
name: "root",
path: "",
want: []string{"index.md", "getting-started.md"},
},
{
name: "clean folder or page path",
path: "guide",
want: []string{"guide.md", "guide/index.md"},
},
{
name: "explicit markdown path",
path: "guide/index.md",
want: []string{"guide/index.md"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := normalizeRequestPathCandidates(tt.path)
if !reflect.DeepEqual(got, tt.want) {
t.Fatalf("normalizeRequestPathCandidates(%q) = %#v, want %#v", tt.path, got, tt.want)
}
})
}
}