Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add url & opengraph card previews #720

Merged
merged 13 commits into from
Jan 29, 2025
Prev Previous commit
Next Next commit
Use html-escape crate
tarkah committed Jan 28, 2025

Verified

This commit was signed with the committer’s verified signature.
tarkah Cory Forsstrom
commit 916e4c6f1974d09c890d3a99763e753f62b30d77
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions data/Cargo.toml
Original file line number Diff line number Diff line change
@@ -41,6 +41,7 @@ strum = { version = "0.26.3", features = ["derive"] }
derive_more = { version = "1.0.0", features = ["full"] }
anyhow = "1.0.91"
image = "0.24.9"
html-escape = "0.2.13"

[dependencies.irc]
path = "../irc"
14 changes: 4 additions & 10 deletions data/src/preview.rs
Original file line number Diff line number Diff line change
@@ -91,12 +91,12 @@ async fn load_uncached(url: Url, config: &config::Preview) -> Result<Preview, Lo
.captures_iter(&String::from_utf8_lossy(&bytes))
.map(|c| c.extract())
{
let value_1 = unescape(
let value_1 = decode_html_string(
value_1
.trim_start_matches(['\'', '"'])
.trim_end_matches(['\'', '"']),
);
let value_2 = unescape(
let value_2 = decode_html_string(
value_2
.trim_start_matches(['\'', '"'])
.trim_end_matches(['\'', '"']),
@@ -229,14 +229,8 @@ async fn fetch(url: Url, config: &config::Preview) -> Result<Fetched, LoadError>
Ok(fetched)
}

fn unescape(s: &str) -> String {
s.replace("&quot;", "\"")
.replace("&#x27", "'")
.replace("&#39;", "'")
.replace("&nbsp;", " ")
.replace("&amp;", "&")
.replace("&lt;", "<")
.replace("&gt;", ">")
fn decode_html_string(s: &str) -> String {
html_escape::decode_html_entities(s).to_string()
}

#[derive(Debug, thiserror::Error)]