PluginProbe
Extendify / 3.1.4
Extendify v3.1.4
3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 0.7.0 All 126 releases
extendify / src / Launch / lib / util.js

util.js in Extendify 3.1.4, at src/Launch/lib/util.js

63 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { decodeEntities } from '@wordpress/html-entities';
2
3 /** Removes any hash or qs values from URL - Airtable adds timestamps */
4 export const stripUrlParams = (url) => url?.[0]?.url?.split(/[?#]/)?.[0];
5
6 function cleanAndBuildUnsplashUrl(url) {
7 const cleanUrl = url
8 .replaceAll('\\u0026', '&')
9 // Remove duplicate question marks in URL by replacing second '?' with '&'
10 .replace(/(\?.*?)\?/, '$1&');
11 const imageUrl = new URL(decodeEntities(cleanUrl));
12
13 const size = 1440;
14 const orientation = imageUrl.searchParams.get('orientation');
15
16 if (orientation === 'portrait') {
17 imageUrl.searchParams.set('h', size);
18 imageUrl.searchParams.delete('w');
19 } else if (orientation === 'landscape' || orientation === 'square') {
20 const widthParam = imageUrl.searchParams.get('w');
21 if (widthParam === null || widthParam === '') {
22 imageUrl.searchParams.set('w', size);
23 }
24 }
25
26 imageUrl.searchParams.delete('orientation');
27 imageUrl.searchParams.delete('ixid');
28 imageUrl.searchParams.delete('ixlib');
29 imageUrl.searchParams.append('q', '0');
30 imageUrl.searchParams.append('auto', 'format,compress');
31 imageUrl.searchParams.append('fm', 'avif');
32 return (
33 imageUrl
34 .toString()
35 // Remove duplicate question marks in URL by replacing second '?' with '&'
36 .replace(/(\?.*?)\?/, '$1&')
37 );
38 }
39
40 export const lowerImageQuality = (html) => {
41 return html.replace(
42 /https:\/\/images\.unsplash\.com\/[^"')]+/g,
43 cleanAndBuildUnsplashUrl,
44 );
45 };
46
47 export const hexTomatrixValues = (hex) => {
48 // convert from hex
49 const colorInt = parseInt(hex.replace('#', ''), 16);
50 // convert to rgb
51 // This shifts each primary color value to the right-most 8 bits
52 // then applies a mask to get the value of that color
53 const r = (colorInt >> 16) & 255;
54 const g = (colorInt >> 8) & 255;
55 const b = colorInt & 255;
56 // normalize to 0-1
57 return [
58 Math.round((r / 255) * 10000) / 10000,
59 Math.round((g / 255) * 10000) / 10000,
60 Math.round((b / 255) * 10000) / 10000,
61 ];
62 };
63