PluginProbe
Extendify / 3.1.2
Extendify v3.1.2
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 / Library / util / replace-pattern-images.js

replace-pattern-images.js in Extendify 3.1.2, at src/Library/util/replace-pattern-images.js

35 lines 942 B
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 const formatImageUrl = (image) =>
2 image?.includes('?q=80&w=1470') ? image : `${image}?q=80&w=1470`;
3
4 const hashString = (s) => {
5 let h = 0;
6 for (let i = 0; i < s.length; i++) {
7 h = (h * 31 + s.charCodeAt(i)) | 0;
8 }
9 return Math.abs(h);
10 };
11
12 const seededShuffle = (arr, seed) => {
13 const result = [...arr];
14 let h = hashString(String(seed));
15 for (let i = result.length - 1; i > 0; i--) {
16 h = (h * 1103515245 + 12345) | 0;
17 const j = Math.abs(h) % (i + 1);
18 [result[i], result[j]] = [result[j], result[i]];
19 }
20 return result;
21 };
22
23 export const replacePatternImages = (content, images, seed) => {
24 if (!images?.length) return content;
25 const matches =
26 content.match(/https:\/\/images\.unsplash\.com\/[^\s"]+/g) || [];
27 const uniqueUrls = [...new Set(matches)];
28 const order = seededShuffle(images, seed);
29 return uniqueUrls.reduce(
30 (updated, url, i) =>
31 updated.replaceAll(url, formatImageUrl(order[i] || url)),
32 content,
33 );
34 };
35