Skip to content

Commit 96a452d

Browse files
Add alt text to clip titles (#67)
1 parent fd7ce14 commit 96a452d

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

commons/Cargo.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@ version = "1.2.5"
44
edition.workspace = true
55

66
[dependencies.serde]
7-
version = "1.0"
8-
features = ["rc", "derive"]
7+
workspace = true
8+
features = ["rc", "derive"]
9+
10+
[dependencies.regex]
11+
workspace = true
12+
13+
[dependencies.once_cell]
14+
workspace = true

commons/src/util.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use once_cell::sync::Lazy;
2+
use regex::Regex;
13
use serde::{Deserialize, Serialize};
24
use std::{num::NonZeroU64, ops::Add};
35

@@ -34,3 +36,33 @@ impl Add<u64> for Timestamp {
3436
unsafe { Self(NonZeroU64::new_unchecked(self.0.get() + rhs)) }
3537
}
3638
}
39+
40+
static ALT_TEXT_WHITELIST: Lazy<Regex> = Lazy::new(|| Regex::new(r"\s*(?:[_*`]+|~~+|\|\|+)\s*|(\s+|^)\w+://").unwrap());
41+
42+
pub fn sanitize_markdown(text: &str) -> String {
43+
ALT_TEXT_WHITELIST.replace_all(text, " ").trim().to_owned()
44+
}
45+
46+
#[cfg(test)]
47+
mod tests {
48+
use super::*;
49+
50+
#[test]
51+
fn test_sanitize_markdown() {
52+
assert_eq!(
53+
sanitize_markdown("simple | text - with $ special & chars %"),
54+
"simple | text - with $ special & chars %"
55+
);
56+
assert_eq!(
57+
sanitize_markdown("hello world https://example.com"),
58+
"hello world example.com"
59+
);
60+
assert_eq!(
61+
sanitize_markdown("https://example.com hello world"),
62+
"example.com hello world"
63+
);
64+
assert_eq!(sanitize_markdown("~~hello world~~"), "hello world");
65+
assert_eq!(sanitize_markdown("*hello world*"), "hello world");
66+
assert_eq!(sanitize_markdown("hello ||world||"), "hello world");
67+
}
68+
}

strumbot/src/watcher.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::sync::Arc;
22

3-
use commons::util::Timestamp;
3+
use commons::util::{sanitize_markdown, Timestamp};
44
use discord_api::{config::EventName, WebhookClient};
55
use eos::DateTime;
66
use serde::{Deserialize, Serialize};
@@ -367,8 +367,9 @@ impl StreamWatcher {
367367
.iter()
368368
.enumerate()
369369
.map(|(i, c)| {
370+
let sanitized_title = sanitize_markdown(&c.title);
370371
let mut title = String::with_capacity(30);
371-
for (i, c) in c.title.chars().enumerate() {
372+
for (i, c) in sanitized_title.chars().enumerate() {
372373
if i == 26 {
373374
title.push_str("...");
374375
break;
@@ -381,10 +382,11 @@ impl StreamWatcher {
381382
});
382383
}
383384
format!(
384-
"`{}.` [**{} \u{1F855}**]({}) \u{2022} **{}**\u{00A0}views\n",
385+
"`{}.` [**{} \u{1F855}**]({} '{}') \u{2022} **{}**\u{00A0}views\n",
385386
i + 1,
386387
title,
387388
c.url,
389+
sanitized_title,
388390
c.view_count
389391
)
390392
})

0 commit comments

Comments
 (0)