1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use super::{CowStr, EventRenderer, Result, ThemeElement, create_style};
fn media_marker(dest_url: &str) -> &'static str {
if let Some(marker) = media_marker_from_data_uri(dest_url) {
return marker;
}
let Some(extension) = extract_media_extension(dest_url) else {
return "[MEDIA] ";
};
if is_video_extension(&extension) {
"[VIDEO] "
} else if is_audio_extension(&extension) {
"[AUDIO] "
} else if is_image_extension(&extension) {
"[IMAGE] "
} else {
"[MEDIA] "
}
}
fn media_marker_from_data_uri(dest_url: &str) -> Option<&'static str> {
let value = dest_url.trim();
if !value
.get(..5)
.map(|prefix| prefix.eq_ignore_ascii_case("data:"))
.unwrap_or(false)
{
return None;
}
let header = value[5..]
.split_once(',')
.map(|(header, _)| header)
.unwrap_or("");
let mime = header
.split(';')
.next()
.unwrap_or("")
.trim()
.to_ascii_lowercase();
if mime.starts_with("video/") {
Some("[VIDEO] ")
} else if mime.starts_with("audio/") {
Some("[AUDIO] ")
} else if mime.starts_with("image/") {
Some("[IMAGE] ")
} else {
Some("[MEDIA] ")
}
}
fn is_image_extension(extension: &str) -> bool {
matches!(
extension,
"apng"
| "avif"
| "bmp"
| "dds"
| "dib"
| "emf"
| "exr"
| "gif"
| "hdr"
| "heic"
| "heif"
| "ico"
| "j2c"
| "j2k"
| "jfif"
| "jp2"
| "jpe"
| "jpeg"
| "jpf"
| "jpg"
| "jpm"
| "jpx"
| "jxl"
| "pbm"
| "pgm"
| "png"
| "pnm"
| "ppm"
| "psd"
| "raw"
| "svg"
| "svgz"
| "tga"
| "tif"
| "tiff"
| "wbmp"
| "webp"
| "wmf"
)
}
fn is_video_extension(extension: &str) -> bool {
matches!(
extension,
"3g2"
| "3gp"
| "asf"
| "avi"
| "av1"
| "drc"
| "f4v"
| "flv"
| "h264"
| "h265"
| "hevc"
| "m1v"
| "m2ts"
| "m2v"
| "m4v"
| "mkv"
| "mov"
| "mp4"
| "mpe"
| "mpeg"
| "mpg"
| "mpv"
| "mts"
| "mxf"
| "ogm"
| "ogv"
| "qt"
| "rm"
| "rmvb"
| "ts"
| "vob"
| "webm"
| "wmv"
| "y4m"
)
}
fn is_audio_extension(extension: &str) -> bool {
matches!(
extension,
"8svx"
| "aac"
| "ac3"
| "aif"
| "aifc"
| "aiff"
| "alac"
| "amr"
| "ape"
| "au"
| "caf"
| "dts"
| "eac3"
| "flac"
| "m4a"
| "m4b"
| "m4p"
| "mid"
| "midi"
| "mka"
| "mp1"
| "mp2"
| "mp3"
| "mpa"
| "mpc"
| "oga"
| "ogg"
| "opus"
| "ra"
| "ram"
| "snd"
| "spx"
| "tak"
| "tta"
| "wav"
| "weba"
| "wma"
| "wv"
)
}
fn extract_media_extension(dest_url: &str) -> Option<String> {
let path = dest_url.split(['?', '#']).next().unwrap_or(dest_url);
let filename = path.rsplit(['/', '\\']).next().unwrap_or(path);
let (_, extension) = filename.rsplit_once('.')?;
let extension = extension.trim().to_ascii_lowercase();
if extension.is_empty() {
None
} else {
Some(extension)
}
}
impl<'a> EventRenderer<'a> {
pub(super) fn handle_image_start(&mut self, dest_url: CowStr) -> Result<()> {
let marker = media_marker(dest_url.as_ref());
// If we are inside a table, write the marker into the current cell
if let Some(ref mut table) = self.table_state {
let style = create_style(self.theme, ThemeElement::Link);
let image_marker = style.apply(marker, self.config.no_colors);
table.current_cell.push_str(&image_marker);
self.commit_pending_heading_placeholder_if_content();
return Ok(());
}
self.note_paragraph_content();
// Ensure correct indentation/prefix when an image starts a visual line.
// Paragraph start may have added spaces, but when inside lists/quotes
// there may be no prefix yet. If the current line contains only
// whitespace, normalize it and insert the proper context-aware prefix.
let line_start_idx = self.output.rfind('\n').map(|i| i + 1).unwrap_or(0);
let current_line = &self.output[line_start_idx..];
if current_line.trim().is_empty() {
// Drop any existing leading spaces on the current visual line
// (e.g. content indent added at paragraph start) to avoid double
// indentation, then re-apply consistent prefix/indent.
self.output.truncate(line_start_idx);
self.push_indent_for_line_start();
}
let style = create_style(self.theme, ThemeElement::Link);
let image_marker = style.apply(marker, self.config.no_colors);
self.output.push_str(&image_marker);
self.commit_pending_heading_placeholder_if_content();
Ok(())
}
pub(super) fn handle_image_end(&mut self) -> Result<()> {
// Image handling is completed in start
Ok(())
}
}