UNPKG

@devmehq/open-graph-extractor

Version:

Fast, lightweight Open Graph, Twitter Card, and structured data extractor for Node.js with caching and validation

105 lines (104 loc) 3.05 kB
import type { CheerioAPI } from "cheerio"; import type { Element } from "domhandler"; import type { IImageMetadata, ImageFormat, ISrcSetImage, IVideoMetadata, OGType } from "./types"; export interface IOgObjectMedia { [key: string]: unknown; } export interface IMediaOptions { allMedia?: boolean; customMetaTags?: ConcatArray<{ multiple: boolean; property: string; fieldName: string; }>; onlyGetOpenGraphInfo?: unknown; } export interface IOgImage { url: string; width?: string | number; height?: string | number; type?: string; } export interface IOgVideo { url?: string; width?: string; height?: string; type?: string; } export interface ITwitterImage { url?: string; width?: string; height?: string; alt?: string; } export interface ITwitterPlayer { url?: string; width?: string; height?: string; stream?: string; } export interface IMusicSong { url?: string; track?: string; disc?: string; } export interface IOgObjectMedia { ogTitle?: string; ogType?: OGType; ogUrl?: string; ogDescription?: string; ogImage?: IOgImage | IOgImage[] | string | string[]; ogImageWidth?: string[]; ogImageHeight?: string[]; ogImageType?: string[]; ogVideo?: IOgVideo | IOgVideo[] | string | string[]; ogVideoWidth?: string[]; ogVideoHeight?: string[]; ogVideoType?: string[]; twitterImageSrc?: string[]; twitterImage?: ITwitterImage | ITwitterImage[] | string | string[]; twitterImageWidth?: string[]; twitterImageHeight?: string[]; twitterImageAlt?: string[]; twitterPlayer?: ITwitterPlayer | ITwitterPlayer[] | string | string[]; twitterPlayerWidth?: string[]; twitterPlayerHeight?: string[]; twitterPlayerStream?: string[]; musicSong?: IMusicSong | IMusicSong[] | string | string[]; musicSongTrack?: string[]; musicSongDisc?: string[]; } export declare function mediaSetup(ogObject: IOgObjectMedia, options: IMediaOptions): IOgObjectMedia; /** * Detect image format from URL or content type */ export declare function detectImageFormat(url: string, contentType?: string): ImageFormat | undefined; /** * Parse srcset attribute into structured data */ export declare function parseSrcSet(srcset: string): ISrcSetImage[]; /** * Extract enhanced image metadata */ export declare function extractImageMetadata($: CheerioAPI, element: Element): IImageMetadata; /** * Extract all images with enhanced metadata */ export declare function extractAllImages($: CheerioAPI): IImageMetadata[]; /** * Extract video metadata */ export declare function extractVideoMetadata($: CheerioAPI, url?: string): IVideoMetadata | null; /** * Find the best image from available options */ export declare function selectBestImage(images: IImageMetadata[]): IImageMetadata | null; /** * Extract audio metadata */ export declare function extractAudioMetadata($: CheerioAPI): { url?: string; secureUrl?: string; type?: string; duration?: number; };