UNPKG

api-interface

Version:
32 lines (31 loc) 1.46 kB
"use strict"; const jsdom_1 = require("jsdom"); const utils_1 = require("../utils"); module.exports = async (params) => { const { url, type } = params; if (!(0, utils_1.isUrl)(url)) throw new Error('"url" parameter is not legal'); const { raw } = await (0, utils_1.getRequest)(url); const dom = new jsdom_1.JSDOM(raw); const titleDom = dom.window.document.querySelector('title'); const title = (titleDom && titleDom.textContent) || null; const links = [].slice.call(dom.window.document.querySelectorAll('link[rel][href]')); const iconDom = links.find((_el) => _el.rel.includes('icon')); let favicon = (iconDom && iconDom.getAttribute('href')) || null; // If `icon` is not the ['https://', 'http://', '//'] protocol, splice on the `origin` of the a tag if (favicon && !(0, utils_1.isHttp)(favicon)) favicon = new URL(url).origin + favicon; const descriptionDom = dom.window.document.querySelector('head meta[name=description]'); const description = (descriptionDom && descriptionDom.getAttribute('content')) || null; const keywordsDom = dom.window.document.querySelector('head meta[name=keywords]'); const keywords = (keywordsDom && keywordsDom.getAttribute('content')) || null; const data = { title, favicon, description, keywords }; if (type && favicon) data.redirect = favicon; return data; };