@lobehub/editor
Version:
A powerful and extensible rich text editor built on Meta's Lexical framework, providing a modern editing experience with React integration.
51 lines (43 loc) • 1.33 kB
JavaScript
/**
* Shared URL validation utilities
*/
// URL validation regex that matches common URL patterns
var URL_REGEX = new RegExp(/^(?:(?:https?|ftp):\/\/)?(?:www\.)?[\dA-Za-z][\w#%+./:=?@~-]*[\w#%+/=@~-]$/);
/**
* Validates if a string is a valid URL
* @param url - The URL to validate
* @returns true if the URL is valid, false otherwise
*/
export function isValidUrl(url) {
if (!url || typeof url !== 'string') return false;
var trimmed = url.trim();
// Check if it matches URL pattern
if (URL_REGEX.test(trimmed)) {
return true;
}
// Check if it's a valid URL with protocol
try {
var urlObj = new URL(trimmed);
return !!urlObj.protocol && !!urlObj.hostname;
} catch (_unused) {
// Not a valid URL
}
// Check if it looks like a domain (e.g., "google.com")
if (/^[\da-z][\w#%+./:=?@~-]*\.[\w#%+/:=?@~-]+$/i.test(trimmed)) {
return true;
}
return false;
}
/**
* Checks if text is a pure URL (single URL without other text)
* @param text - The text to check
* @returns true if text is a pure URL, false otherwise
*/
export function isPureUrl(text) {
if (!text || typeof text !== 'string') return false;
var trimmed = text.trim();
// Check if it's a single line
if (trimmed.includes('\n')) return false;
// Check if it's a valid URL
return isValidUrl(trimmed);
}