|
| 1 | +use std::sync::LazyLock; |
| 2 | + |
| 3 | +use regex::Regex; |
| 4 | + |
| 5 | +/// Pattern for matching an auto-linked GitHub username which can be a potential github @mention. |
| 6 | +/// |
| 7 | +/// Matches GitHub's username/team patterns: |
| 8 | +/// - Usernames: 1-39 chars, starting with alphanumeric, followed by alphanumeric or hyphens |
| 9 | +/// - Teams: Same as username but allows underscores and slashes for org/team hierarchy |
| 10 | +/// |
| 11 | +/// ref: https://github.com/rust-lang/homu/pull/230 |
| 12 | +static MENTION_REGEX: LazyLock<Regex> = LazyLock::new(|| { |
| 13 | + let username_segment = r"(?x) |
| 14 | + [\p{L}\p{N}] # Must start with letter/number |
| 15 | + (?:[\p{L}\p{N}-] # Followed by allowed base chars |
| 16 | + | \p{M} # Or combining marks (for accented chars) |
| 17 | + ){0,38} # Up to 38 more characters |
| 18 | + "; |
| 19 | + |
| 20 | + let team_segment = r"(?x) |
| 21 | + [\p{L}\p{N}_] # Start with letter/number/underscore |
| 22 | + (?:[\p{L}\p{N}_-] # Team name components |
| 23 | + | \p{M} # Allow combining marks |
| 24 | + ){0,38} # Up to 38 more characters |
| 25 | + "; |
| 26 | + |
| 27 | + let pattern = format!(r"@({username_segment})(?:/({team_segment}))*"); |
| 28 | + |
| 29 | + Regex::new(&pattern).unwrap() |
| 30 | +}); |
| 31 | + |
| 32 | +/// Replaces valid GitHub @mentions with backticks to prevent accidental pings |
| 33 | +/// |
| 34 | +/// For example: |
| 35 | +/// "@user" -> "`@user`". |
| 36 | +/// "@org/team" -> "`@org/team`". |
| 37 | +/// "@org/team/subteam" -> "`@org/team/subteam`". |
| 38 | +pub fn suppress_github_mentions(text: &str) -> String { |
| 39 | + if text.is_empty() || !text.contains('@') { |
| 40 | + return text.to_string(); |
| 41 | + } |
| 42 | + |
| 43 | + let mut buffer = String::with_capacity(text.len()); |
| 44 | + let mut last_end = 0; |
| 45 | + |
| 46 | + for m in MENTION_REGEX.find_iter(text) { |
| 47 | + let (start, end) = (m.start(), m.end()); |
| 48 | + let mention = &text[start..end]; |
| 49 | + |
| 50 | + // Append text between matches |
| 51 | + buffer.push_str(&text[last_end..start]); |
| 52 | + |
| 53 | + if is_valid_mention_context(text, start, end) { |
| 54 | + buffer.push_str(&format!("`{mention}`")); |
| 55 | + } else { |
| 56 | + buffer.push_str(mention); |
| 57 | + } |
| 58 | + |
| 59 | + last_end = end; |
| 60 | + } |
| 61 | + |
| 62 | + buffer.push_str(&text[last_end..]); |
| 63 | + buffer |
| 64 | +} |
| 65 | + |
| 66 | +/// Extension trait for GitHub mention boundary checks |
| 67 | +trait WordChar { |
| 68 | + fn is_word_char(&self) -> bool; |
| 69 | +} |
| 70 | + |
| 71 | +impl WordChar for char { |
| 72 | + fn is_word_char(&self) -> bool { |
| 73 | + self.is_alphanumeric() || *self == '_' |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/// Validates mention boundaries according to GitHub's autolinking rules |
| 78 | +/// |
| 79 | +/// A mention is considered valid if: |
| 80 | +/// 1. Preceded by non-word character (or start of string) |
| 81 | +/// 2. Followed by non-word character (or end of string) |
| 82 | +/// |
| 83 | +/// ref: https://github.com/rust-lang/homu/pull/230 |
| 84 | +fn is_valid_mention_context(text: &str, start: usize, end: usize) -> bool { |
| 85 | + // Check preceding boundary |
| 86 | + if start > 0 { |
| 87 | + if let Some(prev) = text[..start].chars().last() { |
| 88 | + if prev.is_word_char() { |
| 89 | + return false; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // Check following boundary |
| 95 | + if end < text.len() { |
| 96 | + if let Some(next) = text[end..].chars().next() { |
| 97 | + if next.is_word_char() || "-_".contains(next) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + true |
| 104 | +} |
| 105 | + |
| 106 | +#[cfg(test)] |
| 107 | +mod tests { |
| 108 | + use super::*; |
| 109 | + |
| 110 | + #[test] |
| 111 | + fn basic_mentions() { |
| 112 | + assert_eq!(suppress_github_mentions("Hello @user"), "Hello `@user`"); |
| 113 | + assert_eq!( |
| 114 | + suppress_github_mentions("Ping @developer"), |
| 115 | + "Ping `@developer`" |
| 116 | + ); |
| 117 | + assert_eq!( |
| 118 | + suppress_github_mentions("Multiple @user1 and @user2"), |
| 119 | + "Multiple `@user1` and `@user2`" |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + #[test] |
| 124 | + fn team_mentions() { |
| 125 | + assert_eq!(suppress_github_mentions("@org/team"), "`@org/team`"); |
| 126 | + assert_eq!( |
| 127 | + suppress_github_mentions("@rust-lang/libs"), |
| 128 | + "`@rust-lang/libs`" |
| 129 | + ); |
| 130 | + assert_eq!( |
| 131 | + suppress_github_mentions("@org/team/subteam"), |
| 132 | + "`@org/team/subteam`" |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + #[test] |
| 137 | + fn mention_boundaries() { |
| 138 | + // Adjacent punctuation |
| 139 | + assert_eq!( |
| 140 | + suppress_github_mentions("Hello,@user! How are you?"), |
| 141 | + "Hello,`@user`! How are you?" |
| 142 | + ); |
| 143 | + |
| 144 | + // Email addresses |
| 145 | + assert_eq!( |
| 146 | + suppress_github_mentions ("[email protected]"), |
| 147 | + |
| 148 | + ); |
| 149 | + |
| 150 | + // Invalid mentions |
| 151 | + assert_eq!(suppress_github_mentions("@-user"), "@-user"); |
| 152 | + } |
| 153 | + |
| 154 | + #[test] |
| 155 | + fn unicode_support() { |
| 156 | + // Precomposed characters |
| 157 | + assert_eq!( |
| 158 | + suppress_github_mentions("Hello @üsèrñàmé"), |
| 159 | + "Hello `@üsèrñàmé`" |
| 160 | + ); |
| 161 | + |
| 162 | + // Combining marks exclusion |
| 163 | + assert_eq!( |
| 164 | + suppress_github_mentions("Hello @us\u{0301}er"), |
| 165 | + "Hello `@us\u{0301}er`" |
| 166 | + ); |
| 167 | + |
| 168 | + // Bidirectional text |
| 169 | + assert_eq!( |
| 170 | + suppress_github_mentions("Hello @user في العالم"), |
| 171 | + "Hello `@user` في العالم" |
| 172 | + ); |
| 173 | + } |
| 174 | + |
| 175 | + #[test] |
| 176 | + fn edge_cases() { |
| 177 | + // Empty input |
| 178 | + assert_eq!(suppress_github_mentions(""), ""); |
| 179 | + |
| 180 | + // Minimum valid mention |
| 181 | + assert_eq!(suppress_github_mentions("@a"), "`@a`"); |
| 182 | + |
| 183 | + // Maximum length mention |
| 184 | + let long_mention = "@".to_string() + &"a".repeat(39); |
| 185 | + assert_eq!( |
| 186 | + suppress_github_mentions(&long_mention), |
| 187 | + format!("`{long_mention}`") |
| 188 | + ); |
| 189 | + } |
| 190 | +} |
0 commit comments