Debian Codegraph @

src/modules/resolveUrl.ts

raw
1 import type Swup from '../Swup.js';
2
3 /**
4 * Utility function to validate and run the global option 'resolveUrl'
5 * @param {string} url
6 * @returns {string} the resolved url
7 */
8 export function resolveUrl(this: Swup, url: string): string {
9 if (typeof this.options.resolveUrl !== 'function') {
10 console.warn(`[swup] options.resolveUrl expects a callback function.`);
11 return url;
12 }
13 const result = this.options.resolveUrl(url);
14 if (!result || typeof result !== 'string') {
15 console.warn(`[swup] options.resolveUrl needs to return a url`);
16 return url;
17 }
18 if (result.startsWith('//') || result.startsWith('http')) {
19 console.warn(`[swup] options.resolveUrl needs to return a relative url`);
20 return url;
21 }
22 return result;
23 }
24
25 /**
26 * Compares the resolved version of two paths and returns true if they are the same
27 * @param {string} url1
28 * @param {string} url2
29 * @returns {boolean}
30 */
31 export function isSameResolvedUrl(this: Swup, url1: string, url2: string): boolean {
32 return this.resolveUrl(url1) === this.resolveUrl(url2);
33 }