Skip to content
Open
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
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,19 +230,20 @@ func isSnap() bool {

// processMarkdownImages processes markdown source and converts relative image paths to data URIs
func processMarkdownImages(markdown string, baseDir string) string {
// Process markdown image syntax: ![alt](path)
imgMarkdownRegex := regexp.MustCompile(`!\[([^\]]*)\]\(([^)]+)\)`)
// Process markdown image syntax: ![alt](path "optional title")
imgMarkdownRegex := regexp.MustCompile(`!\[([^\]]*)\]\(([^)\s]+)(\s+(?:"[^"]*"|'[^']*'|\([^)]*\)))?\)`)
markdown = imgMarkdownRegex.ReplaceAllStringFunc(markdown, func(match string) string {
parts := imgMarkdownRegex.FindStringSubmatch(match)
if len(parts) < 3 {
return match
}
alt := parts[1]
imgPath := parts[2]
title := parts[3]

if isRelativePath(imgPath) {
if dataURI := imageToDataURI(imgPath, baseDir); dataURI != "" {
return fmt.Sprintf("![%s](%s)", alt, dataURI)
return fmt.Sprintf("![%s](%s%s)", alt, dataURI, title)
}
}
return match
Expand Down
65 changes: 65 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"

"github.com/yuin/goldmark"
)

func TestProcessMarkdownImages(t *testing.T) {
baseDir := t.TempDir()
imagePath := filepath.Join(baseDir, "images", "diagram.png")
if err := os.MkdirAll(filepath.Dir(imagePath), 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(imagePath, []byte("image data"), 0644); err != nil {
t.Fatal(err)
}

tests := []struct {
name string
markdown string
wantTitleHTML string
}{
{
name: "double-quoted title",
markdown: `![Architecture diagram](images/diagram.png "Architecture diagram")`,
wantTitleHTML: `title="Architecture diagram"`,
},
{
name: "single-quoted title",
markdown: `![Architecture diagram](images/diagram.png 'Architecture diagram')`,
wantTitleHTML: `title="Architecture diagram"`,
},
{
name: "parenthesized title",
markdown: `![Architecture diagram](images/diagram.png (Architecture diagram))`,
wantTitleHTML: `title="Architecture diagram"`,
},
{
name: "without title",
markdown: `![Architecture diagram](images/diagram.png)`,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
processed := processMarkdownImages(tt.markdown, baseDir)
if !strings.Contains(processed, `data:image/png;base64,aW1hZ2UgZGF0YQ==`) {
t.Fatalf("image was not embedded: %s", processed)
}

var rendered bytes.Buffer
if err := goldmark.New().Convert([]byte(processed), &rendered); err != nil {
t.Fatal(err)
}
if tt.wantTitleHTML != "" && !strings.Contains(rendered.String(), tt.wantTitleHTML) {
t.Fatalf("rendered image lost its title attribute: %s", rendered.String())
}
})
}
}
Loading