Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849// Match http(s) URLs embedded in prose. The trailing char class is greedy and// includes punctuation that is often part of the *sentence*, not the URL — a// closing paren from "(see https://x.com/y)", a trailing period, a "?" after a// parenthetical. So the regex deliberately over-captures and every match is then// passed through trimTrailing(), which hands the prose characters back.//// Note there is no leading "(" in this pattern: a URL never starts with one, so// a wrapping "(https://…)" simply matches from the "https" and the stray "("// is left in the surrounding text where it belongs.const URL_RE = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/gi;
// Punctuation that can never legitimately END a URL but routinely trails one in// prose. Closing brackets are handled separately (kept only when balanced).const TRAILING = new Set(['.', ',', ';', ':', '!', '?', "'", '"', '<', '>']);
function trimTrailing(url) { let s = url; while (s.length) { const c = s[s.length - 1]; if (TRAILING.has(c)) { s = s.slice(0, -1); continue; } // A ")" is part of the URL only if it closes a "(" inside it (e.g. // wiki/Foo_(bar)); otherwise it's the close of a prose "(…)" wrapper. if (c === ')') { const opens = (s.match(/\(/g) || []).length; const closes = (s.match(/\)/g) || []).length; if (closes > opens) { s = s.slice(0, -1); continue; } } break; } return s;}
export function extractURLs(str) { if (!str) return []; return [...str.matchAll(URL_RE)].map(m => trimTrailing(m[0]));}
// Returns an array of "items" to dispatch for a given message + rule mode.// 'urls' → one item per URL found (skips messages with no URLs)// 'content' → one item: the full message content (skips empty)export function extractItems(content, mode) { if (mode === 'content') { const text = (content ?? '').trim(); return text ? [{ kind: 'content', value: text }] : []; } // default: urls return extractURLs(content).map(u => ({ kind: 'url', value: u }));}