From e9e71c68f7bd492f7f7b2af9c3ceb3e472795fa8 Mon Sep 17 00:00:00 2001 From: Falk Harrison Date: Tue, 29 Sep 2026 10:46:31 +0200 Subject: [PATCH 1/2] fix(parser): preserve Markdown rules outside tag footers --- pkg/parser/footer_test.go | 68 +++++++++++++++++++++++++++++++++++++++ pkg/parser/tags.go | 21 +++++++++++- 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 pkg/parser/footer_test.go diff --git a/pkg/parser/footer_test.go b/pkg/parser/footer_test.go new file mode 100644 index 0000000..1f86507 --- /dev/null +++ b/pkg/parser/footer_test.go @@ -0,0 +1,68 @@ +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", + } { + 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"}}, + {"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..87a3e54 100644 --- a/pkg/parser/tags.go +++ b/pkg/parser/tags.go @@ -4,6 +4,8 @@ import ( "bytes" "path/filepath" "strings" + + yaml "go.yaml.in/yaml/v3" ) // ============================================================================ @@ -177,7 +179,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 +194,19 @@ func extractYAMLFooter(data []byte, end int) ([]byte, []string, bool) { return nil, nil, false } +func isTagMetadata(block []byte) bool { + var metadata map[string]any + if err := yaml.Unmarshal(block, &metadata); err != nil { + return false + } + for key := range metadata { + if strings.EqualFold(key, "tags") { + return true + } + } + return false +} + func findLineStart(data []byte, end int) int { start := end for start > 0 && data[start-1] != '\n' { From 1ae252b92e8b1657b8c2cedc20cae770c14e1e24 Mon Sep 17 00:00:00 2001 From: Gubarz <1037896+Gubarz@users.noreply.github.com> Date: Tue, 24 Nov 2026 23:49:26 -0700 Subject: [PATCH 2/2] fix(parser): preserve compact tag footers --- pkg/parser/footer_test.go | 6 ++++++ pkg/parser/tags.go | 28 ++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/pkg/parser/footer_test.go b/pkg/parser/footer_test.go index 1f86507..5bb4eea 100644 --- a/pkg/parser/footer_test.go +++ b/pkg/parser/footer_test.go @@ -17,6 +17,9 @@ func TestFooterPreservesOrdinaryMarkdown(t *testing.T) { "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" @@ -36,6 +39,9 @@ func TestFooterTagsPreserveCommandBetweenRules(t *testing.T) { {"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) { diff --git a/pkg/parser/tags.go b/pkg/parser/tags.go index 87a3e54..829beec 100644 --- a/pkg/parser/tags.go +++ b/pkg/parser/tags.go @@ -3,6 +3,7 @@ package parser import ( "bytes" "path/filepath" + "regexp" "strings" yaml "go.yaml.in/yaml/v3" @@ -194,13 +195,32 @@ 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 { - var metadata map[string]any - if err := yaml.Unmarshal(block, &metadata); err != nil { + 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 key := range metadata { - if strings.EqualFold(key, "tags") { + 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 } }