MCPcopy
hub / github.com/apify/crawlee / parseOpenGraphProperty

Function parseOpenGraphProperty

packages/utils/src/internals/open_graph_parser.ts:358–388  ·  view source on GitHub ↗
(property: OpenGraphProperty, $: CheerioAPI)

Source from the content-addressed store, hash-verified

356const makeOpenGraphSelector = (name: string) => `meta[property="${name}"]`;
357
358const 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.

Callers 1

parseOpenGraphFunction · 0.85

Calls 4

makeOpenGraphSelectorFunction · 0.85
optionalSpreadFunction · 0.85
mapMethod · 0.80
reduceMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…