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: 5 additions & 0 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,13 @@ func Image(file, input, workdir string) error {
// parseImageInput checks whether input is a markdown image reference
// (![alt](path)) or a plain file path. It returns the image path and any
// extracted alt text (empty when the input is a plain path).
// It also handles the common case where the shell escapes "!" to "\!".
func parseImageInput(input string) (path, altText string) {
trimmed := strings.TrimSpace(input)
// Some shells escape "!" to "\!", so strip the leading backslash.
if strings.HasPrefix(trimmed, `\![`) {
trimmed = trimmed[1:]
}
if strings.HasPrefix(trimmed, "![") && strings.HasSuffix(trimmed, ")") {
// Extract alt text between ![ and ]
rest := trimmed[2:]
Expand Down
32 changes: 32 additions & 0 deletions cmd/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ func TestParseImageInput(t *testing.T) {
{"![Screenshot of homepage](shot.png)", "shot.png", "Screenshot of homepage"},
{" ![padded](file.png) ", "file.png", "padded"},
{"not-markdown.png", "not-markdown.png", ""},
{`\![escaped](file.png)`, "file.png", "escaped"},
{`\![alt text](/path/to/img.png)`, "/path/to/img.png", "alt text"},
}
for _, tt := range tests {
path, alt := parseImageInput(tt.input)
Expand All @@ -316,6 +318,36 @@ func TestParseImageInput(t *testing.T) {
}
}

func TestImageMarkdownRefEscapedBang(t *testing.T) {
dir := t.TempDir()
file := filepath.Join(dir, "demo.md")

if err := Init(file, "Test", "dev"); err != nil {
t.Fatal(err)
}

pngPath := filepath.Join(dir, "test.png")
if err := os.WriteFile(pngPath, minimalPNG, 0644); err != nil {
t.Fatal(err)
}

input := `\![My screenshot](` + pngPath + ")"

if err := Image(file, input, ""); err != nil {
t.Fatal(err)
}

content, err := os.ReadFile(file)
if err != nil {
t.Fatal(err)
}

s := string(content)
if !strings.Contains(s, "![My screenshot](") {
t.Errorf("expected alt text 'My screenshot' in image output, got: %s", s)
}
}

func TestImageMarkdownRefBadPath(t *testing.T) {
dir := t.TempDir()
file := filepath.Join(dir, "demo.md")
Expand Down