UNPKG

1.38 kBPlain TextView Raw
1const sources = {
2 bdc: {
3 domain: "https://lpbookingcom.imgix.net",
4 regex: /https?:\/\/[\w-]+.bstatic.com/,
5 },
6 gadventures: {
7 domain: "https://lpgadventures.imgix.net",
8 regex: /https?:\/\/media.gadventures.com/,
9 },
10 hostelworld: {
11 domain: "https://lphostelworld.imgix.net",
12 regex: /https?:\/\/(.*)hwstatic.com/,
13 },
14 media: {
15 domain: "https://lonelyplanetimages.imgix.net",
16 regex: /https?:\/\/media.lonelyplanet.com/,
17 },
18 viator: {
19 domain: "https://lpviator.imgix.net",
20 regex: /https?:\/\/cache-graphicslib.viator.com/,
21 },
22 wordpress: {
23 domain: "https://lonelyplanetwp.imgix.net",
24 regex: /https?:\/\/www.lonelyplanet.com\/travel-blog\/tip-article\/wordpress_uploads/,
25 },
26};
27
28interface IImgixProps {
29 auto?: string;
30 crop?: string;
31 fit?: string;
32 h?: number;
33 q?: number;
34 sharp?: number;
35 w?: number;
36 [key: string]: any;
37}
38
39/**
40 * Converts an image path to the imgix url with params
41 */
42export default function imgix(
43 src: string,
44 options: IImgixProps = {},
45 source: string = "media",
46): string {
47 const match = sources[source || "media"];
48
49 if (!match || !match.regex.test(src)) {
50 return src;
51 }
52
53 const url = src.replace(match.regex, match.domain);
54 const query = Object.keys(options)
55 .map(k => `${k}=${options[k]}`)
56 .join("&");
57
58 return `${url}${query ? `?${query}` : ""}`;
59}
60
\No newline at end of file