This repository has been archived by the owner on Sep 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage.ts
205 lines (175 loc) · 4.29 KB
/
image.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// ----- Imports ----- //
import { createHash } from 'crypto';
import type { Image as CardImage } from '@guardian/apps-rendering-api-models/image';
import type { BlockElement } from '@guardian/content-api-models/v1/blockElement';
import type { Image as ImageData } from '@guardian/image-rendering';
import { Role } from '@guardian/image-rendering';
import type { Format, Option, Result } from '@guardian/types';
import {
andThen,
fromNullable,
fromUnsafe,
map,
none,
ResultKind,
some,
} from '@guardian/types';
import { pipe2 } from 'lib';
import type { ReactNode } from 'react';
import type { Context } from 'types/parserContext';
// ----- Setup ----- //
const imageResizer = 'https://i.guim.co.uk/img';
const defaultWidths = [140, 500, 1000, 1500, 2000];
// Percentage.
const defaultQuality = 85;
const lowerQuality = 45;
// ----- Types ----- //
const enum Dpr {
One,
Two,
}
interface Srcsets {
srcset: string;
dpr2Srcset: string;
}
interface Image extends ImageData {
caption: Option<DocumentFragment>;
credit: Option<string>;
nativeCaption: Option<string>;
}
interface BodyImageProps {
image: Image;
children?: ReactNode;
format: Format;
}
// ----- Functions ----- //
const getSubdomain = (domain: string): string => domain.split('.')[0];
const sign = (salt: string, path: string): string =>
createHash('md5')
.update(salt + path)
.digest('hex');
function src(salt: string, input: string, width: number, dpr: Dpr): string {
const maybeUrl: Result<string, URL> = fromUnsafe(
() => new URL(input),
'invalid url',
);
switch (maybeUrl.kind) {
case ResultKind.Ok: {
const url = maybeUrl.value;
const service = getSubdomain(url.hostname);
const params = new URLSearchParams({
width: width.toString(),
quality:
dpr === Dpr.Two
? lowerQuality.toString()
: defaultQuality.toString(),
fit: 'bounds',
});
const path = `${url.pathname}?${params.toString()}`;
const sig = sign(salt, path);
return `${imageResizer}/${service}${path}&s=${sig}`;
}
case ResultKind.Err:
default: {
return input;
}
}
}
const srcsetWithWidths = (widths: number[]) => (
url: string,
salt: string,
dpr: Dpr,
): string =>
widths.map((width) => `${src(salt, url, width, dpr)} ${width}w`).join(', ');
const srcset: (
url: string,
salt: string,
dpr: Dpr,
) => string = srcsetWithWidths(defaultWidths);
const srcsets = (url: string, salt: string): Srcsets => ({
srcset: srcset(url, salt, Dpr.One),
dpr2Srcset: srcset(url, salt, Dpr.Two),
});
const parseCredit = (
displayCredit: boolean | undefined,
credit: string | undefined,
): Option<string> =>
pipe2(
displayCredit,
fromNullable,
andThen((display) => (display ? fromNullable(credit) : none)),
);
const parseRole = (role: string | undefined): Role => {
switch (role) {
case 'thumbnail':
return Role.Thumbnail;
case 'halfWidth':
return Role.HalfWidth;
default:
return Role.Standard;
}
};
const parseImage = ({ docParser, salt }: Context) => (
element: BlockElement,
): Option<Image> => {
const masterAsset = element.assets.find(
(asset) => asset.typeData?.isMaster,
);
const data = element.imageTypeData;
return pipe2(
masterAsset,
fromNullable,
andThen((asset) => {
if (
asset.file === undefined ||
asset.file === '' ||
asset.typeData?.width === undefined ||
asset.typeData.height === undefined
) {
return none;
}
return some({
src: src(salt, asset.file, 500, Dpr.One),
...srcsets(asset.file, salt),
alt: fromNullable(data?.alt),
width: asset.typeData.width,
height: asset.typeData.height,
caption: pipe2(data?.caption, fromNullable, map(docParser)),
credit: parseCredit(data?.displayCredit, data?.credit),
nativeCaption: fromNullable(data?.caption),
role: parseRole(data?.role),
});
}),
);
};
const parseCardImage = (
image: CardImage | undefined,
salt: string,
): Option<Image> => {
if (image === undefined) {
return none;
}
return some({
src: src(salt, image.url, 500, Dpr.One),
...srcsets(image.url, salt),
alt: fromNullable(image.altText),
width: image.width,
height: image.height,
caption: none,
credit: none,
nativeCaption: none,
role: Role.Standard,
});
};
// ----- Exports ----- //
export {
Image,
Dpr,
src,
srcset,
srcsetWithWidths,
sign,
parseImage,
parseCardImage,
BodyImageProps,
};