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
|
import type { URL } from 'schema-dts';
import type { Opt } from '@jet/environment/types/optional';
import type { CropCode } from '@amp/web-app-components/src/components/Artwork/types';
import { buildSrcSeo } from '@amp/web-app-components/src/components/Artwork/utils/srcset';
const RECOMMENDED_OPEN_GRAPH_IMAGE_WIDTH = 1200;
const RECOMMENDED_OPEN_GRAPH_IMAGE_HEIGHT = 630;
const DEFAULT_OPEN_GRAPH_IMAGE_CROP = 'bb';
const DEFAULT_OPEN_GRAPH_IMAGE_FILE_TYPE = 'png';
/**
* Generate an OpenGraph image URL from a Media API artwork definition
*
* This overrides the default size of the image with the recommendations
* from the Open Graph documentation
*/
export function buildOpenGraphImageURL(
artworkDefinition: Opt<MapLike<JSONValue>>,
crop: CropCode = DEFAULT_OPEN_GRAPH_IMAGE_CROP,
): URL | undefined {
if (!artworkDefinition) {
return undefined;
}
const { url } = artworkDefinition;
if (typeof url !== 'string') {
return undefined;
}
return (
buildSrcSeo(url, {
crop,
width: RECOMMENDED_OPEN_GRAPH_IMAGE_WIDTH,
height: RECOMMENDED_OPEN_GRAPH_IMAGE_HEIGHT,
fileType: DEFAULT_OPEN_GRAPH_IMAGE_FILE_TYPE,
}) ?? undefined
);
}
/**
* Construct a metadata-friendly URL for some Media API-provided artwork
*/
export function buildImageURL(
artworkDefinition: Opt<MapLike<JSONValue>>,
): URL | undefined {
if (!artworkDefinition) {
return undefined;
}
const { url, width, height } = artworkDefinition;
if (
typeof url !== 'string' ||
typeof width !== 'number' ||
typeof height !== 'number'
) {
return undefined;
}
return (
buildSrcSeo(url, {
crop: DEFAULT_OPEN_GRAPH_IMAGE_CROP,
width,
height,
fileType: DEFAULT_OPEN_GRAPH_IMAGE_FILE_TYPE,
}) ?? undefined
);
}
|