File tree Expand file tree Collapse file tree 4 files changed +47
-5
lines changed Expand file tree Collapse file tree 4 files changed +47
-5
lines changed Original file line number Diff line number Diff line change @@ -4,5 +4,11 @@ version = "1.2.5"
4
4
edition.workspace = true
5
5
6
6
[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
Original file line number Diff line number Diff line change
1
+ use once_cell:: sync:: Lazy ;
2
+ use regex:: Regex ;
1
3
use serde:: { Deserialize , Serialize } ;
2
4
use std:: { num:: NonZeroU64 , ops:: Add } ;
3
5
@@ -34,3 +36,33 @@ impl Add<u64> for Timestamp {
34
36
unsafe { Self ( NonZeroU64 :: new_unchecked ( self . 0 . get ( ) + rhs) ) }
35
37
}
36
38
}
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
+ }
Original file line number Diff line number Diff line change 1
1
use std:: sync:: Arc ;
2
2
3
- use commons:: util:: Timestamp ;
3
+ use commons:: util:: { sanitize_markdown , Timestamp } ;
4
4
use discord_api:: { config:: EventName , WebhookClient } ;
5
5
use eos:: DateTime ;
6
6
use serde:: { Deserialize , Serialize } ;
@@ -367,8 +367,9 @@ impl StreamWatcher {
367
367
. iter ( )
368
368
. enumerate ( )
369
369
. map ( |( i, c) | {
370
+ let sanitized_title = sanitize_markdown ( & c. title ) ;
370
371
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 ( ) {
372
373
if i == 26 {
373
374
title. push_str ( "..." ) ;
374
375
break ;
@@ -381,10 +382,11 @@ impl StreamWatcher {
381
382
} ) ;
382
383
}
383
384
format ! (
384
- "`{}.` [**{} \u{1F855} **]({}) \u{2022} **{}**\u{00A0} views\n " ,
385
+ "`{}.` [**{} \u{1F855} **]({} '{}' ) \u{2022} **{}**\u{00A0} views\n " ,
385
386
i + 1 ,
386
387
title,
387
388
c. url,
389
+ sanitized_title,
388
390
c. view_count
389
391
)
390
392
} )
You can’t perform that action at this time.
0 commit comments