import * as _ from 'lodash'; import * as xpath from 'xpath'; import * as xmldom from 'xmldom'; export const getXpathDoc = (html: string) => { const dom = xmldom.DOMParser; const doc = new dom({ locator: {}, errorHandler: { warning: function (w) { // }, error: function (e) { // }, fatalError: function (e) { // }, }, }).parseFromString(html); return doc } const get = _.get; export const fixUrl = (url, host) => { let src = url; if (src) { if (!src.startsWith('http')) { src = `${host}${src}`; } try { const newUrl = new URL(src); } catch (error) { console.error('fixImgSrc error', error); src = ''; } } return src; }; export const getHtmlDoc = (html) => { const dom = xmldom.DOMParser; const doc = new dom({ locator: {}, errorHandler: { warning: function (w) { // }, error: function (e) { // }, fatalError: function (e) { // console.error('error parseFromString fatalError', e); } } }).parseFromString(html); return doc; }; export const getText = (path, block) => { const node = xpath.select(`${path}`, block); const text = get(node, '[0].nodeValue', '').trim(); return text; }; export const getTextCreator = (block) => { return (path) => { const node = xpath.select(`${path}`, block); const text = get(node, '[0].nodeValue', '').trim(); return text; }; };