diff --git a/src/Email.zig b/src/Email.zig index f35a887..4d3d780 100644 --- a/src/Email.zig +++ b/src/Email.zig @@ -322,14 +322,22 @@ pub const Message = struct { , .{text_content.?}); } + var final_text = text_content orelse try allocator.dupe(u8, "no text or html versions available"); + + // If text is empty (e.g., HTML with only images without alt tags), provide fallback + if (final_text.len == 0) { + allocator.free(final_text); + final_text = try allocator.dupe(u8, "Message contains only image data without alt tags"); + } + return .{ - .text = text_content orelse "no text or html versions available", - .html = html_content orelse + .text = final_text, + .html = html_content orelse try allocator.dupe(u8, \\ \\No text or HTML version available \\No text or HTML versions available \\ - , + ), }; } diff --git a/src/textTransformation.zig b/src/textTransformation.zig index e47c685..8ba3f34 100644 --- a/src/textTransformation.zig +++ b/src/textTransformation.zig @@ -22,6 +22,25 @@ pub fn htmlToText(allocator: std.mem.Allocator, html: []const u8) ![]const u8 { } else if (i + 8 <= html.len and std.mem.eql(u8, html[i .. i + 8], "")) { in_style = false; i += 7; + } else if (i + 4 <= html.len and std.mem.eql(u8, html[i .. i + 4], "') orelse html.len; + const tag_content = html[i..tag_end]; + if (std.mem.indexOf(u8, tag_content, "alt=\"")) |alt_start| { + const alt_value_start = alt_start + 5; + if (std.mem.indexOfScalarPos(u8, tag_content, alt_value_start, '"')) |alt_end| { + const alt_text = tag_content[alt_value_start..alt_end]; + if (alt_text.len > 0) { + try result.append(allocator, '['); + for (alt_text) |c| { + try result.append(allocator, c); + } + try result.append(allocator, ']'); + } + } + } + i = tag_end; + in_tag = false; } else if ((i + 3 <= html.len and std.mem.eql(u8, html[i .. i + 3], "")) or (i + 4 <= html.len and std.mem.eql(u8, html[i .. i + 4], "\"Another\""; + const text = try htmlToText(allocator, html); + defer allocator.free(text); + try std.testing.expectEqualStrings("[Test Image][Another]", text); +} + +test "htmlToText - img without alt" { + const allocator = std.testing.allocator; + const html = ""; + const text = try htmlToText(allocator, html); + defer allocator.free(text); + try std.testing.expectEqualStrings("", text); +}