Skip to content

Commit 49f651d

Browse files
met4morfosisBrayan-724
authored andcommitted
fix(markdown): ensure non-empty alt text for images
1 parent f3d6c2a commit 49f651d

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/challenge.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::borrow::Cow;
12
use std::collections::HashMap;
23
use std::fmt::Display;
34

@@ -123,17 +124,22 @@ impl Display for ProblemDifficulty {
123124
struct ImgHandler;
124125
impl TagHandler for ImgHandler {
125126
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();
128131

129-
console_debug!("SRC image content: {src}");
130-
console_debug!("ALT image content: {alt:?}");
132+
let raw_alt = get_tag_attr(tag, "alt");
131133

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));
137143
}
138144

139145
fn after_handle(&mut self, _printer: &mut html2md::StructuredPrinter) {}

0 commit comments

Comments
 (0)