(doc)
| 53 | * @return {string|undefined} |
| 54 | */ |
| 55 | export 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 |
no test coverage detected