"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.loadHtml = loadHtml; exports.parse = parse; exports.root = root; exports.textNode = textNode; exports.cleanup = cleanup; const lodash_1 = __importDefault(require("lodash")); const cheerio = __importStar(require("cheerio")); /** * Load an HTML string and return a cheerio instance * @param html */ function loadHtml(html) { return cheerio.load(html, { _useHtmlParser2: true }); } /** Parse an HTML string and return its content @param html @return {cheerio.Root} */ function parse(html) { const $ = cheerio.load(html, { _useHtmlParser2: true }); const $el = $("html, body").first(); return ($el.length > 0 ? $el : $); } /** Return main element for a DOM @param {cheerio.DOM} @return {cheerio.Node} */ function root($) { const $el = $("html, body, > div").first(); return $el.length > 0 ? $el : $.root(); } /** Return text node of an element @param {cheerio.Node} @return {string} */ function textNode($el) { return lodash_1.default.reduce($el.children, (text, e) => { if (e.type == "text") text += e.data; return text; }, ""); } /** Cleanup a DOM by removing all useless divs @param {cheerio.Node} @param {cheerio.DOM} @return {cheerio.Node} */ function cleanup($el, $) { $el.find("div").each(function () { const $div = $(this); cleanup($div, $); $div.replaceWith($div.html()); }); return $el; }