fix(msgviewer): use pager for all text, replace image markers with readable placeholders
The composite renderer bypassed the pager (less) for any email with images, losing colors, scrolling, and search. Now always forward to the pager with [image: alt] text placeholders. Image refs are still extracted and stored for future inline rendering.
This commit is contained in:
@@ -39,3 +39,33 @@ func ExtractImages(r io.Reader) (io.Reader, []ImageRef) {
|
||||
}
|
||||
return buf, images
|
||||
}
|
||||
|
||||
// ReplacePlaceholders converts \x00IMG:N\x00 placeholders into human-readable
|
||||
// [image: alt] text suitable for display in a pager.
|
||||
func ReplacePlaceholders(data []byte, images []ImageRef) []byte {
|
||||
result := bytes.NewBuffer(nil)
|
||||
for _, line := range bytes.Split(data, []byte("\n")) {
|
||||
trimmed := bytes.TrimSpace(line)
|
||||
if len(trimmed) > 6 && trimmed[0] == 0 &&
|
||||
bytes.HasPrefix(trimmed, []byte("\x00IMG:")) &&
|
||||
trimmed[len(trimmed)-1] == 0 {
|
||||
// Extract index
|
||||
numStr := string(trimmed[5 : len(trimmed)-1])
|
||||
idx := 0
|
||||
for _, c := range numStr {
|
||||
if c >= '0' && c <= '9' {
|
||||
idx = idx*10 + int(c-'0')
|
||||
}
|
||||
}
|
||||
if idx < len(images) && images[idx].Alt != "" {
|
||||
fmt.Fprintf(result, "[image: %s]\n", images[idx].Alt)
|
||||
} else {
|
||||
result.WriteString("[image]\n")
|
||||
}
|
||||
} else {
|
||||
result.Write(line)
|
||||
result.WriteByte('\n')
|
||||
}
|
||||
}
|
||||
return result.Bytes()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user