mozilla / page-metadata-parser

DEPRECATED - A Javascript library for parsing metadata on a web page.
https://www.npmjs.com/package/page-metadata-parser
Mozilla Public License 2.0
270 stars 42 forks source link

Typescript types support #116

Closed raz-viber closed 2 years ago

raz-viber commented 4 years ago

it could be awesome if this lib will have type support.

thanks

patarapolw commented 4 years ago

Look at the source code and made one. Lack of JSDoc makes this harder. Will submit to DefinitelyTyped later.

declare module 'page-metadata-parser' {
  export interface IPageMetadata {
    description?: string
    icon: string
    image?: string
    keywords?: string[]
    title?: string
    language?: string
    type?: string
    url: string
    provider: string
  }

  export interface IPageMetadataContext {
    url: string
  }

  export type PageMetadataRule = [
    string,
    (el: HTMLElement) => (string | null)
  ]

  export function getProvider (host: string): string

  export function buildRuleSet<O = any, CMP = any> (ruleSet: {
    rules: PageMetadataRule[]
    scorers: (el: HTMLElement, score: CMP) => CMP
    defaultValue: (context: IPageMetadataContext) => O
    processors: ((output: O, context: IPageMetadataContext) => any)[]
  }): (doc: Document, context: IPageMetadataContext) => void

  export const metadataRuleSets: Record<keyof IPageMetadata, PageMetadataRule>

  export function getMetadata (
    doc: Document | HTMLElement,
    url: string,
    customRuleSets?: Record<string, PageMetadataRule>
  ): IPageMetadata
}