UNPKG

htmlmetaparser

Version:

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

77 lines 2.87 kB
"use strict"; var OEmbedProviders = (function () { function OEmbedProviders(providers) { this.matchers = []; for (var _i = 0, providers_1 = providers; _i < providers_1.length; _i++) { var provider = providers_1[_i]; this.add(provider); } } OEmbedProviders.prototype.add = function (provider) { this.matchers.push(new OEmbedMatch(provider)); }; OEmbedProviders.prototype.match = function (url) { for (var _i = 0, _a = this.matchers; _i < _a.length; _i++) { var matcher = _a[_i]; var res = matcher.match(url); if (res) { return res; } } return; }; return OEmbedProviders; }()); exports.OEmbedProviders = OEmbedProviders; var OEmbedMatch = (function () { function OEmbedMatch(provider) { var _this = this; this.routes = []; for (var _i = 0, _a = provider.endpoints; _i < _a.length; _i++) { var endpoint = _a[_i]; if (typeof endpoint.url === 'string') { this.routes.push({ matches: Array.isArray(endpoint.schemes) ? endpoint.schemes.map(function (x) { return _this.schemeToRegExp(x); }) : [this.urlToRegExp(provider.provider_url)], url: endpoint.url.replace(/\?.*$/, ''), formats: endpoint.formats }); } } } OEmbedMatch.prototype.schemeToRegExp = function (scheme) { return new RegExp("^" + this.escapeRegExp(scheme) + "$"); }; OEmbedMatch.prototype.urlToRegExp = function (url) { return new RegExp("^" + this.escapeRegExp(url)); }; OEmbedMatch.prototype.escapeRegExp = function (str) { return str .replace(/[|\\{}()[\]^$+*?.]/g, '\\$&') .replace(/^http:/, 'https?:') .replace(/\\\*/, '.+'); }; OEmbedMatch.prototype.match = function (url) { for (var _i = 0, _a = this.routes; _i < _a.length; _i++) { var route = _a[_i]; for (var _b = 0, _c = route.matches; _b < _c.length; _b++) { var match = _c[_b]; if (match.test(url)) { return this.formatResult(route.url, url, route.formats); } } } return; }; OEmbedMatch.prototype.formatResult = function (oembedUrl, url, formats) { var type = formats == null ? 'json' : (formats.indexOf('json') > -1 ? 'json' : formats[0]); return { href: oembedUrl + "?type=" + encodeURIComponent(type) + "&url=" + encodeURIComponent(url), type: "application/" + type + "+oembed" }; }; return OEmbedMatch; }()); exports.OEmbedMatch = OEmbedMatch; //# sourceMappingURL=oembed.js.map