api: decode HTML entities in article excerpts

This commit is contained in:
Jeena 2026-03-22 01:17:49 +00:00
parent 81439edf87
commit 571d80fa6b

View file

@ -249,9 +249,20 @@ fn plain_text_excerpt(html: &str, max_chars: usize) -> String {
} }
} }
let collapsed: String = out.split_whitespace().collect::<Vec<_>>().join(" "); let collapsed: String = out.split_whitespace().collect::<Vec<_>>().join(" ");
if collapsed.chars().count() <= max_chars { let decoded = decode_html_entities(&collapsed);
collapsed if decoded.chars().count() <= max_chars {
decoded
} else { } else {
collapsed.chars().take(max_chars).collect::<String>() + "" decoded.chars().take(max_chars).collect::<String>() + ""
} }
} }
fn decode_html_entities(s: &str) -> String {
s.replace("&amp;", "&")
.replace("&lt;", "<")
.replace("&gt;", ">")
.replace("&quot;", "\"")
.replace("&apos;", "'")
.replace("&#39;", "'")
.replace("&nbsp;", " ")
}