UNPKG

htmlmetaparser

Version:

A `htmlparser2` handler for parsing rich metadata from HTML. Includes HTML metadata, JSON-LD, RDFa, microdata, OEmbed, Twitter cards and AppLinks.

79 lines 2.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OEmbedProviders = exports.OEmbedMatch = void 0; /** * Create a matcher for a provider. */ class OEmbedMatch { constructor(provider) { this.routes = []; for (const endpoint of provider.endpoints) { if (typeof endpoint.url === "string") { this.routes.push({ matches: Array.isArray(endpoint.schemes) ? endpoint.schemes.map((x) => this.schemeToRegExp(x)) : [this.urlToRegExp(provider.provider_url)], url: endpoint.url.replace(/\?.*$/, ""), formats: endpoint.formats, }); } } } schemeToRegExp(scheme) { return new RegExp(`^${this.escapeRegExp(scheme)}$`); } urlToRegExp(url) { return new RegExp(`^${this.escapeRegExp(url)}`); } escapeRegExp(str) { return str .replace(/[|\\{}()[\]^$+*?.]/g, "\\$&") .replace(/^http:/, "https?:") .replace(/\\\*/, ".+"); } match(url) { for (const route of this.routes) { for (const match of route.matches) { if (match.test(url)) { return this.formatResult(route.url, url, route.formats); } } } return; } formatResult(oembedUrl, url, formats) { const type = formats === undefined ? "json" : formats.includes("json") ? "json" : formats[0]; return { href: `${oembedUrl}?type=${encodeURIComponent(type)}&url=${encodeURIComponent(url)}`, type: `application/${type}+oembed`, }; } } exports.OEmbedMatch = OEmbedMatch; /** * Find a matching provider. */ class OEmbedProviders { constructor(providers) { this.matchers = []; for (const provider of providers) { this.add(provider); } } add(provider) { this.matchers.push(new OEmbedMatch(provider)); } match(url) { for (const matcher of this.matchers) { const res = matcher.match(url); if (res) return res; } } } exports.OEmbedProviders = OEmbedProviders; //# sourceMappingURL=oembed.js.map