diff --git a/pkg/parser/footer_test.go b/pkg/parser/footer_test.go new file mode 100644 index 0000000..5bb4eea --- /dev/null +++ b/pkg/parser/footer_test.go @@ -0,0 +1,74 @@ +package parser + +import ( + "os" + "path/filepath" + "reflect" + "slices" + "testing" +) + +func TestFooterPreservesOrdinaryMarkdown(t *testing.T) { + for _, block := range []string{ + "## Greeting\n```sh\necho hello\n```", + "Ordinary prose mentioning tags: [greeting]", + "description: ordinary metadata", + "description: |\n tags: [greeting]", + "metadata:\n tags: [greeting]", + "- tags: [greeting]", + "tags: [unterminated", + "tags:[unterminated", + "description: |\n tags:[greeting]", + "tags:[greeting]\n" + "```sh\necho hello\n```", + } { + t.Run(block, func(t *testing.T) { + input := "# Documentation\nIntro\n---\n" + block + "\n---\n" + body, tags := extractFooterTags([]byte(input)) + if string(body) != input || len(tags) != 0 { + t.Fatalf("extractFooterTags() = (%q, %v), want (%q, [])", body, tags, input) + } + }) + } +} + +func TestFooterTagsPreserveCommandBetweenRules(t *testing.T) { + for _, tc := range []struct { + name, footer string + wantTags []string + }{ + {"ordinary closing rule", "---\n", nil}, + {"inline tags", "---\ntags: [greeting, sample]\n---\n", []string{"greeting", "sample"}}, + {"case insensitive tags", "---\nTaGs: Greeting, Sample\n---\n", []string{"Greeting", "Sample"}}, + {"compact flow tags", "---\ntags:[greeting, sample]\n---\n", []string{"greeting", "sample"}}, + {"compact comma-separated tags with other metadata", "---\nauthor: demo\nTaGs:Greeting, Sample\n---\n", []string{"Greeting", "Sample"}}, + {"repeated tag entries", "---\ntags: greeting\ntags: sample\n---\n", []string{"greeting", "sample"}}, + {"list tags", "---\ntags:\n - greeting\n - sample\n---\n", []string{"greeting", "sample"}}, + } { + t.Run(tc.name, func(t *testing.T) { + commandBody := "# Documentation\nIntro\n---\n## Greeting\n```sh\necho hello\n```\n" + input := commandBody + tc.footer + body, tags := extractFooterTags([]byte(input)) + wantBody := commandBody + if tc.wantTags == nil { + wantBody = input + } + if string(body) != wantBody || !reflect.DeepEqual(tags, tc.wantTags) { + t.Fatalf("extractFooterTags() = (%q, %v), want (%q, %v)", body, tags, wantBody, tc.wantTags) + } + path := filepath.Join(t.TempDir(), "rules.md") + if err := os.WriteFile(path, []byte(input), 0600); err != nil { + t.Fatal(err) + } + index, err := NewParser().ParseSingleFile(path) + if err != nil { + t.Fatal(err) + } + if len(index.Cheats) != 1 || index.Cheats[0].Command != "echo hello" { + t.Fatalf("parsed cheats = %+v, want one echo hello command", index.Cheats) + } + if tc.wantTags != nil && (!slices.Contains(index.Cheats[0].Tags, "greeting") || !slices.Contains(index.Cheats[0].Tags, "sample")) { + t.Fatalf("footer tags not applied: %v", index.Cheats[0].Tags) + } + }) + } +} diff --git a/pkg/parser/tags.go b/pkg/parser/tags.go index afe6595..829beec 100644 --- a/pkg/parser/tags.go +++ b/pkg/parser/tags.go @@ -3,7 +3,10 @@ package parser import ( "bytes" "path/filepath" + "regexp" "strings" + + yaml "go.yaml.in/yaml/v3" ) // ============================================================================ @@ -177,7 +180,11 @@ func extractYAMLFooter(data []byte, end int) ([]byte, []string, bool) { lineStart := findLineStart(data, lineEnd) line := bytes.TrimRight(data[lineStart:lineEnd], " \t\r") if bytes.Equal(line, []byte("---")) && lineStart != openEnd-3 { - tags := parseYAMLTags(data[lineEnd+1 : openEnd]) + block := data[lineEnd+1 : openEnd] + if !isTagMetadata(block) { + return nil, nil, false + } + tags := parseYAMLTags(block) return data[:lineStart], tags, true } if lineStart == 0 { @@ -188,6 +195,38 @@ func extractYAMLFooter(data []byte, end int) ([]byte, []string, bool) { return nil, nil, false } +var compactTagKey = regexp.MustCompile(`(?mi)^([ \t]*tags:)([^ \t\r\n])`) + +func isTagMetadata(block []byte) bool { + if hasTopLevelTags(block) { + return true + } + // The tag reader also accepts legacy compact keys such as tags:[a]. + // Normalize the spacing for validation, then read the original tags. + if !compactTagKey.Match(block) { + return false + } + return hasTopLevelTags(compactTagKey.ReplaceAll(block, []byte("${1} ${2}"))) +} + +func hasTopLevelTags(block []byte) bool { + var document yaml.Node + if err := yaml.Unmarshal(block, &document); err != nil || len(document.Content) == 0 { + return false + } + root := document.Content[0] + if root.Kind != yaml.MappingNode { + return false + } + for i := 0; i < len(root.Content); i += 2 { + key := root.Content[i] + if key.Kind == yaml.ScalarNode && strings.EqualFold(key.Value, "tags") { + return true + } + } + return false +} + func findLineStart(data []byte, end int) int { start := end for start > 0 && data[start-1] != '\n' {