Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nested group rules #495

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions internal/core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,29 @@ func (c Config) GroupConfigNamed(name string) (GroupConfig, error) {
// GroupNameForPath returns the name of the GroupConfig matching the given
// path, relative to the notebook.
func (c Config) GroupNameForPath(path string) (string, error) {
var longestMatch string
var matchedName string

for name, config := range c.Groups {
for _, groupPath := range config.Paths {
matches, err := filepath.Match(groupPath, path)
if err != nil {
return "", errors.Wrapf(err, "failed to match group %s to %s", name, path)
} else if matches {
// Early return if an exact match
return name, nil
}
if strings.HasPrefix(path, groupPath+"/") {
return name, nil
}
if strings.HasPrefix(path, groupPath+"/") {
// If the match is partial, find the longest prefix overlap
if len(groupPath) > len(longestMatch) {
longestMatch = groupPath
matchedName = name
}
}
}
}

return "", nil
return matchedName, nil
}

// FormatConfig holds the configuration for document formats, such as Markdown.
Expand Down
28 changes: 28 additions & 0 deletions internal/core/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,34 @@ func TestParseComplete(t *testing.T) {
})
}

func TestGroupNameForPathPrefersLongestMatch(t *testing.T) {
config := Config{
Groups: map[string]GroupConfig{
"parent": {
Paths: []string{"area"},
},
"child": {
Paths: []string{"area/subfolder"},
},
"other": {
Paths: []string{"other"},
},
},
}

name, err := config.GroupNameForPath("area/subfolder/note.md")
assert.Nil(t, err)
assert.Equal(t, name, "child")

name, err = config.GroupNameForPath("area/note.md")
assert.Nil(t, err)
assert.Equal(t, name, "parent")

name, err = config.GroupNameForPath("other/note.md")
assert.Nil(t, err)
assert.Equal(t, name, "other")
}

func TestParseMergesGroupConfig(t *testing.T) {
conf, err := ParseConfig([]byte(`
[note]
Expand Down
16 changes: 16 additions & 0 deletions tests/fixtures/issue-490/.zk/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[note]
filename = "{{slug title}}"

[group.area]
paths = ["area"]

[group.area.note]
extension = "md"
template = "template1.md"

[group.subarea]
paths = ["area/subfolder"]

[group.subarea.note]
extension = "md"
template = "template2.md"
1 change: 1 addition & 0 deletions tests/fixtures/issue-490/.zk/templates/template1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Template 1
1 change: 1 addition & 0 deletions tests/fixtures/issue-490/.zk/templates/template2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Template 2
Empty file.
16 changes: 16 additions & 0 deletions tests/issue-490.tesh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
$ cd issue-490

# Test that subfolder path takes precedence over parent path
$ zk new area/subfolder --title "TestNote" --dry-run
>Template 2
2>{{working-dir}}/area/subfolder/testnote.md

# Test that parent path still works for direct children
$ zk new area --title "ParentNote" --dry-run
>Template 1
2>{{working-dir}}/area/parentnote.md

# Test that explicit group override still works
$ zk new area/subfolder --group area --title "OverrideTest" --dry-run
>Template 1
2>{{working-dir}}/area/subfolder/overridetest.md