(property: OpenGraphProperty, $: CheerioAPI)
| 356 | const makeOpenGraphSelector = (name: string) => `meta[property="${name}"]`; |
| 357 | |
| 358 | const parseOpenGraphProperty = (property: OpenGraphProperty, $: CheerioAPI): string | string[] | OpenGraphResult => { |
| 359 | // Some OpenGraph properties can be added multiple times, such as with video:actor. We must handle this case. |
| 360 | const values = [...$(makeOpenGraphSelector(property.name))].map((elem) => $(elem).attr('content')); |
| 361 | |
| 362 | // If there is more than 1 item, keep it a an array. Otherwise, return just the first value. |
| 363 | const content = values.length <= 1 ? (values[0] as string) : (values as string[]); |
| 364 | |
| 365 | // If the property has no children, just return its value immediately. |
| 366 | if (!property.children.length) return content; |
| 367 | |
| 368 | // Otherwise, return an object with the values for the property, along with the values for its children. |
| 369 | return { |
| 370 | // We do this, because for example, there can be a value under og:image which should still be parsed, |
| 371 | // but there can also be child properties such as og:image:url or og:image:size |
| 372 | // "Value" is appended to the end of the property name to make it more clear, and to prevent things such |
| 373 | // as `videoInfo.actor.actor` to grab the actor's name. |
| 374 | ...optionalSpread(`${property.outputName}Value`, content), |
| 375 | ...property.children.reduce( |
| 376 | (acc, curr) => { |
| 377 | const parsed = parseOpenGraphProperty(curr, $); |
| 378 | if (parsed === undefined) return acc; |
| 379 | |
| 380 | return { |
| 381 | ...acc, |
| 382 | ...optionalSpread(curr.outputName, parseOpenGraphProperty(curr, $)), |
| 383 | }; |
| 384 | }, |
| 385 | {} as Dictionary<string | Dictionary>, |
| 386 | ), |
| 387 | }; |
| 388 | }; |
| 389 | |
| 390 | /** |
| 391 | * Easily parse all OpenGraph properties from a page with just a `CheerioAPI` object. |
no test coverage detected
searching dependent graphs…