@jcottam/html-metadata
Version:
This JavaScript library simplifies the extraction of HTML Meta and OpenGraph tags from HTML content or URLs.
58 lines (57 loc) • 1.9 kB
TypeScript
/**
* Configuration options for metadata extraction
*/
export type Options = {
/** Base URL for resolving relative links (e.g., favicon, apple-touch-icon) */
baseUrl?: string;
/** Fetch timeout in milliseconds for URL extraction */
timeout?: number;
/** Specific meta tags to extract. If not provided, all meta tags will be extracted */
metaTags?: string[];
};
/**
* Extracted metadata object containing key-value pairs of meta tags and their content
*/
export type ExtractedData = {
/** Language attribute from the HTML tag */
lang?: string;
/** Page title from the title tag */
title?: string;
/** Favicon URL */
favicon?: string;
/** Apple touch icon URL */
"apple-touch-icon"?: string;
/** Open Graph and other meta tag properties */
[key: string]: string | undefined;
};
/**
* Extracts metadata from an HTML string
*
* @param html - The HTML content to parse
* @param options - Optional configuration for extraction
* @returns Object containing extracted metadata
*
* @example
* ```typescript
* const html = '<html><head><meta property="og:title" content="My Site" /></head></html>'
* const metadata = extractFromHTML(html)
* console.log(metadata['og:title']) // "My Site"
* ```
*/
export declare const extractFromHTML: (html: string, options?: Options) => ExtractedData;
/**
* Extracts metadata from a URL by fetching the HTML content
*
* @param url - The URL to fetch and extract metadata from
* @param options - Optional configuration for extraction
* @returns Promise that resolves to extracted metadata or null if extraction fails
*
* @example
* ```typescript
* const metadata = await extractFromUrl('https://example.com')
* if (metadata) {
* console.log(metadata['og:title'])
* }
* ```
*/
export declare const extractFromUrl: (url: string, options?: Options) => Promise<ExtractedData | null>;