Skip to content
Merged
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
5 changes: 4 additions & 1 deletion internal/parser/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func parseCodeBlockStart(line []byte) (lang, desc string, ok bool) {

// parseCheatSingleLine parses <!-- cheat ... --> and returns the content.
func parseCheatSingleLine(line []byte) (string, bool) {
if len(line) < 15 {
if len(line) < len("<!--cheat-->") {
return "", false
}
if !bytes.HasPrefix(line, []byte("<!--")) {
Expand All @@ -158,6 +158,9 @@ func parseCheatSingleLine(line []byte) (string, bool) {
if !bytes.EqualFold(inner[:5], []byte("cheat")) {
return "", false
}
if len(inner) > 5 && inner[5] != ' ' && inner[5] != '\t' {
return "", false
}

return string(bytes.TrimSpace(inner[5:])), true
}
Expand Down
32 changes: 32 additions & 0 deletions internal/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,38 @@ func TestParseCheatDSL_Comments(t *testing.T) {
}
}

func TestParseSingleLineEmptyCheatComment(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "single.md")
content := "# Cheats\n\n## Empty metadata\n\n```sh\necho hi\n```\n<!-- cheat -->\n"
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatal(err)
}

p := NewParser()
index, err := p.ParseSingleFile(path)
if err != nil {
t.Fatalf("ParseSingleFile() error: %v", err)
}

if len(index.Cheats) != 1 {
t.Fatalf("ParseSingleFile() cheats = %d, want 1", len(index.Cheats))
}
cheat := index.Cheats[0]
if !cheat.HasCheatBlock {
t.Fatal("single-line <!-- cheat --> did not mark cheat as having a cheat block")
}
if cheat.Command != "echo hi" {
t.Fatalf("command = %q, want %q", cheat.Command, "echo hi")
}
}

func TestParseCheatSingleLineRequiresKeywordBoundary(t *testing.T) {
if _, ok := parseCheatSingleLine([]byte("<!-- cheating -->")); ok {
t.Fatal("parseCheatSingleLine accepted non-cheat keyword")
}
}

func TestNewCheatIndex(t *testing.T) {
idx := NewCheatIndex()
if idx == nil {
Expand Down