|
| 1 | +use std::borrow::Cow; |
1 | 2 | use std::collections::HashMap; |
2 | 3 | use std::fmt::Display; |
3 | 4 |
|
@@ -123,17 +124,22 @@ impl Display for ProblemDifficulty { |
123 | 124 | struct ImgHandler; |
124 | 125 | impl TagHandler for ImgHandler { |
125 | 126 | fn handle(&mut self, tag: &html2md::Handle, printer: &mut html2md::StructuredPrinter) { |
126 | | - let src = get_tag_attr(tag, "src").unwrap_or_default(); |
127 | | - let alt = get_tag_attr(tag, "alt"); |
| 127 | + let raw_src = get_tag_attr(tag, "src") |
| 128 | + .map(|s| s.trim().to_string()) |
| 129 | + .filter(|s| !s.is_empty()) |
| 130 | + .unwrap_or_default(); |
128 | 131 |
|
129 | | - console_debug!("SRC image content: {src}"); |
130 | | - console_debug!("ALT image content: {alt:?}"); |
| 132 | + let raw_alt = get_tag_attr(tag, "alt"); |
131 | 133 |
|
132 | | - if let Some(alt) = alt { |
133 | | - printer.append_str(&format!("", alt, &src)); |
134 | | - } else { |
135 | | - printer.append_str(&src); |
136 | | - } |
| 134 | + console_debug!("SRC image content: {raw_src}"); |
| 135 | + console_debug!("ALT image content: {:?}", raw_alt); |
| 136 | + |
| 137 | + let alt: Cow<'_, str> = match raw_alt { |
| 138 | + Some(ref a) if !a.trim().is_empty() => Cow::Borrowed(a.trim()), |
| 139 | + _ => Cow::Borrowed("leetcode"), |
| 140 | + }; |
| 141 | + |
| 142 | + printer.append_str(&format!("[{}]({})", alt, raw_src)); |
137 | 143 | } |
138 | 144 |
|
139 | 145 | fn after_handle(&mut self, _printer: &mut html2md::StructuredPrinter) {} |
|
0 commit comments