Mirror of https://github.com/roostorg/coop
github.com/roostorg/coop
1/**
2 * Utility functions for detecting and handling content URLs that should be displayed in iframes
3 */
4
5 /**
6 * Get the content URL patterns from environment variables
7 * Defaults to 'notion' for backward compatibility
8 * Supports comma-separated patterns
9 */
10 export function getContentUrlPatterns(): string[] {
11 const pattern: string = import.meta.env.VITE_CONTENT_URL_PATTERN ?? 'notion';
12 return pattern.split(',').map(p => p.trim()).filter(p => p.length > 0);
13 }
14
15/**
16 * Check if a URL should be displayed in an iframe
17 * @param url The URL to check
18 * @returns True if the URL should be displayed in an iframe
19 */
20export function shouldDisplayInIframe(url: string): boolean {
21 const patterns = getContentUrlPatterns();
22 const urlLower = url.toLowerCase();
23 return patterns.some(pattern => urlLower.includes(pattern.toLowerCase()));
24}
25
26/**
27 * Check if a URL field value should be displayed in an iframe
28 * @param urlField The URL field value to check
29 * @returns True if the URL field should be displayed in an iframe
30 */
31export function shouldDisplayUrlFieldInIframe(urlField: any): boolean {
32 if (!urlField || typeof urlField !== 'object' || !('type' in urlField)) {
33 return false;
34 }
35
36 if (urlField.type !== 'URL' || typeof urlField.value !== 'string') {
37 return false;
38 }
39
40 return shouldDisplayInIframe(urlField.value);
41}
42
43/**
44 * Find the first URL that should be displayed in an iframe from a list of URL fields
45 * @param urlFields Array of URL field values
46 * @returns The first URL field that should be displayed in an iframe, or undefined
47 */
48export function findFirstIframeUrl(urlFields: any[]): any | undefined {
49 return urlFields.find(shouldDisplayUrlFieldInIframe);
50}