fix: preserve tag case when extracting markdown tags

Fixes #5566
This commit is contained in:
Steven 2026-02-02 20:07:06 +08:00
parent b8029c70ef
commit 689ca4d845
2 changed files with 15 additions and 16 deletions

View File

@ -134,8 +134,8 @@ func (s *service) ExtractTags(content []byte) ([]string, error) {
return nil, err
}
// Deduplicate and normalize tags
return uniqueLowercase(tags), nil
// Deduplicate tags while preserving original case
return uniquePreserveCase(tags), nil
}
// ExtractProperties computes boolean properties about the content.
@ -334,8 +334,8 @@ func (s *service) ExtractAll(content []byte) (*ExtractedData, error) {
return nil, err
}
// Deduplicate and normalize tags
data.Tags = uniqueLowercase(data.Tags)
// Deduplicate tags while preserving original case
data.Tags = uniquePreserveCase(data.Tags)
return data, nil
}
@ -372,16 +372,15 @@ func (s *service) RenameTag(content []byte, oldTag, newTag string) (string, erro
return mdRenderer.Render(root, content), nil
}
// uniqueLowercase returns unique lowercase strings from input.
func uniqueLowercase(strs []string) []string {
seen := make(map[string]bool)
// uniquePreserveCase returns unique strings from input while preserving case.
func uniquePreserveCase(strs []string) []string {
seen := make(map[string]struct{})
var result []string
for _, s := range strs {
lower := strings.ToLower(s)
if !seen[lower] {
seen[lower] = true
result = append(result, lower)
if _, exists := seen[s]; !exists {
seen[s] = struct{}{}
result = append(result, s)
}
}

View File

@ -223,7 +223,7 @@ func TestExtractTags(t *testing.T) {
name: "duplicate tags",
content: "#work is important. #Work #WORK",
withExt: true,
expected: []string{"work"}, // Deduplicated and lowercased
expected: []string{"work", "Work", "WORK"},
},
{
name: "tags with hyphens and underscores",
@ -315,7 +315,7 @@ func TestExtractTags(t *testing.T) {
}
}
func TestUniqueLowercase(t *testing.T) {
func TestUniquePreserveCase(t *testing.T) {
tests := []struct {
name string
input []string
@ -334,18 +334,18 @@ func TestUniqueLowercase(t *testing.T) {
{
name: "duplicates",
input: []string{"tag", "TAG", "Tag"},
expected: []string{"tag"},
expected: []string{"tag", "TAG", "Tag"},
},
{
name: "mixed",
input: []string{"Work", "work", "Important", "work"},
expected: []string{"work", "important"},
expected: []string{"Work", "work", "Important"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := uniqueLowercase(tt.input)
result := uniquePreserveCase(tt.input)
assert.ElementsMatch(t, tt.expected, result)
})
}