MCPcopy Create free account
hub / github.com/ampproject/amphtml / parseSchemaImage

Function parseSchemaImage

src/mediasession-helper.js:55–86  ·  view source on GitHub ↗
(doc)

Source from the content-addressed store, hash-verified

53 * @return {string|undefined}
54 */
55export function parseSchemaImage(doc) {
56 const schema = doc.querySelector('script[type="application/ld+json"]');
57 if (!schema) {
58 // No schema element found
59 return;
60 }
61 const schemaJson = tryParseJson(schema.textContent);
62 if (!schemaJson || !schemaJson['image']) {
63 // No image found in the schema
64 return;
65 }
66
67 // Image definition in schema could be one of :
68 if (typeof schemaJson['image'] === 'string') {
69 // 1. "image": "http://..",
70 return schemaJson['image'];
71 } else if (
72 schemaJson['image']['@list'] &&
73 typeof schemaJson['image']['@list'][0] === 'string'
74 ) {
75 // 2. "image": {.., "@list": ["http://.."], ..}
76 return schemaJson['image']['@list'][0];
77 } else if (typeof schemaJson['image']['url'] === 'string') {
78 // 3. "image": {.., "url": "http://..", ..}
79 return schemaJson['image']['url'];
80 } else if (typeof schemaJson['image'][0] === 'string') {
81 // 4. "image": ["http://.. "]
82 return schemaJson['image'][0];
83 } else {
84 return;
85 }
86}
87
88/**
89 * Parses the og:image if it exists and returns it

Callers 5

getMetadataFunction · 0.90
createHostPageMethod · 0.90
updateMetadata_Method · 0.90

Calls 1

tryParseJsonFunction · 0.90

Tested by

no test coverage detected