@mikezimm/fps-core-v7
Version:
Library of reusable core interfaces, types and constants migrated from fps-library-v2
32 lines • 1.22 kB
JavaScript
/**
* https://github.com/mikezimm/drilldown7/issues/494
*
* Originally created in drilldownV2 drillFunctions.tsx to convert Note field html to simple searchable strings
* ie makes it possible to search for time stampls like 1:00 instead of
* <span>1:00 PM</span>
*
* Used this prop to impliment in Drilldown... try to carry it forward
groupFields.push(
PropertyPaneToggle('searchHTMLAsReadable', {
label: 'Search HTML columns by',
offText: 'HTML Text',
onText: 'Readable Text',
}));
*/
const domparser = new DOMParser();
function looksLikeHtml(str) {
// Fast bail-out for very short strings
if (!str || str.length < 3)
return false;
// Basic check: starts with '<' and contains at least one tag pair or entity
return /<[^>]+>.*<\/[^>]+>|&[a-zA-Z0-9#]+;/.test(str);
}
export function getVisibleTextFromHtml(htmlString) {
var _a;
if (!looksLikeHtml(htmlString)) {
return htmlString;
}
const doc = domparser.parseFromString(htmlString, 'text/html');
return ((_a = doc.body.textContent) === null || _a === void 0 ? void 0 : _a.trim()) || '';
}
//# sourceMappingURL=getVisibleTextFromHtml.js.map