UNPKG

cleanview

Version:

Clean the content of html articles

36 lines (35 loc) 1.15 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.addBaseUrl = addBaseUrl; exports.merge = merge; const url_parse_1 = __importDefault(require("url-parse")); function addBaseUrl(elements, baseURL) { for (const id in elements) { const el = elements[id]; if (el.tagName === "a") fixUrl(el, "href", baseURL); if (el.tagName === "img") fixUrl(el, "src", baseURL); } } function fixUrl(element, prop, base) { element.attributes = element.attributes.map(function (attr) { if (attr.key !== prop) return attr; const url = merge(base, String(attr.value)); attr.value = url; return attr; }); } function merge(textBase, textURL) { textURL = (textURL || "").trim(); if (textURL.indexOf("//") > -1) return textURL; if (textURL.indexOf("base64") > -1) return textURL; const url = (0, url_parse_1.default)(textURL, textBase); return url.href; }