agent-ecosystem/src/renderer/utils/urlMatchUtils.ts
iliya bec8a6184a fix: refine regex patterns and improve utility functions for mention handling
- Updated regex patterns in chipUtils and mentionLinkify to enhance boundary detection for mentions.
- Refactored taskChangeRequest to simplify earliest date calculation using array destructuring.
- Improved taskReferenceUtils by replacing character boundary checks with a more concise regex.
- Enhanced teamMessageFiltering to ensure boolean checks for message filtering conditions.
- Adjusted urlMatchUtils to refine URL matching regex for better accuracy.
- Updated crossTeam constants to include comments for regex patterns, improving code clarity.
- Removed unused CommentAttachmentPayload type from api.ts to clean up type definitions.
- Introduced McpInstallScope type for better type safety in mcp.ts.
- Enhanced extensionNormalizers to improve URL normalization and added tests for parseGitHubOwnerRepo function.
- Cleaned up pricing.ts by removing unnecessary eslint disable comments.
- Added tests for new functionality in chipUtils and crossTeam constants, ensuring robust coverage.
2026-03-19 13:35:51 +02:00

45 lines
1.2 KiB
TypeScript

export interface TextMatch {
start: number;
end: number;
value: string;
}
const URL_REGEX = /https?:\/\/\S+/g;
function trimUrlMatch(rawUrl: string): string {
// eslint-disable-next-line sonarjs/slow-regex -- trailing punctuation only, input bounded
return rawUrl.replace(/[),.!?;:]+$/g, '');
}
export function findUrlMatches(text: string): TextMatch[] {
if (!text) return [];
const matches: TextMatch[] = [];
for (const match of text.matchAll(URL_REGEX)) {
const rawValue = match[0];
const start = match.index ?? -1;
if (start < 0) continue;
const trimmedValue = trimUrlMatch(rawValue);
if (!trimmedValue) continue;
matches.push({
start,
end: start + trimmedValue.length,
value: trimmedValue,
});
}
return matches;
}
export function findUrlBoundary(text: string, cursorPos: number): TextMatch | null {
return (
findUrlMatches(text).find((match) => cursorPos >= match.start && cursorPos <= match.end) ?? null
);
}
export function removeUrlMatchFromText(text: string, match: TextMatch): string {
const removeEnd = match.end < text.length && text[match.end] === '\n' ? match.end + 1 : match.end;
return text.slice(0, match.start) + text.slice(removeEnd);
}