UNPKG

@mikezimm/fps-core-v7

Version:

Library of reusable core interfaces, types and constants migrated from fps-library-v2

158 lines (154 loc) 5.51 kB
/** * Creation of string from HTML entities * * Used first in ALVFinMan for reading CanvasContent1 in Modern Pages */ export function convertHTMLToJSONv2(str, conversions = AllHTMLConversions) { const result = replaceHTMLEntitiesv2(str, conversions); return result; } /** * This can take SharePoint html content ( like from ModernPage CanvasContent1 field and make it `readable` ) * * 2023-11-24: POTENTIAL BREAKING CHANGE: when changing to: logic/Strings/htmlV2 - * https://github.com/mikezimm/PageInfo/issues/144 * DOES NOT find image links in src\components\molecules\RelatedItems\GetItems.ts * Update: After updating conversons loop to correctly apply RegExp replace (not text version), works. * Also had to change the order of the quot and apos regex to do the double slash ones first * TESTED in PageInfo (GetItems.ts) * * @param str * @returns */ export function replaceHTMLEntitiesv2(str, conversions = AllHTMLConversions) { let newStr = str + ''; if (typeof str !== 'string') { console.log(`replaceHTMLEntities failed because this was not a string type: ~21`, str); return newStr; } /** * NOTE: Bing Chat suggested this function to replace the number entities... but I'm opting for the full version for readability and reusability * JavaScript * AI-generated code. Review and use carefully. More info on FAQ. const replaceHtmlEntities = (str) => { return str.replace(/&#(\d+);/g, function (match, match2) { return String.fromCharCode(+match2); }); }; This function takes a string as input and returns the same string with all HTML entities replaced with their corresponding characters. For example, replaceHtmlEntities("foo&#39;s bar") will return "foo's bar". Please note that this function only replaces numerical HTML entities and does not replace named entities such as &nbsp;. If you need to replace named entities as well, you can use a library like he.js or html-entities. */ conversions.map((thisConversion) => { thisConversion.regex.map((thisReg) => { newStr = newStr.replace(thisReg, thisConversion.human); }); // newStr = newStr.replace( thisConversion.charEntity , thisConversion.human ); // newStr = newStr.replace( thisConversion.numEntity , thisConversion.human ); }); return newStr; } // newStr = newStr.replace(/&#123;/gi,'{'); export const HtmlCurleyLeft = { regex: [/&#123;/gi, /&lbrace;/gi], human: `{`, charEntity: `&lbrace;`, numEntity: `&#123;`, }; // newStr = newStr.replace(/&#125;/gi,'}'); export const HtmlCurleyRight = { regex: [/&#125;/gi, /&rbrace;/gi], human: `}`, charEntity: `&rbrace;`, numEntity: `&#125;`, }; // newStr = newStr.replace(/\\&quot;/gi,'"'); // newStr = newStr.replace(/&quot;/gi,'"'); export const HtmlDoubleQuote = { regex: [/\\&quot;/gi, /&quot;/gi, /&#34;/gi,], human: `"`, charEntity: `\\&quot;`, numEntity: `&#34;`, }; // newStr = newStr.replace(/\\&apos;/gi,"'"); // newStr = newStr.replace(/&#39;/gi,"'"); export const HtmlSingleQuote = { regex: [/\\&apos;/gi, /&apos;/gi, /&#39;/gi,], human: `'`, charEntity: `\\&apos;`, numEntity: `&#39;`, }; // newStr = newStr.replace(/&#58;/gi,':'); export const HtmlColon = { regex: [/&#58;/gi, /&colon;/gi,], human: `:`, charEntity: `&colon;`, numEntity: `&#58;`, }; // newStr = newStr.replace(/&#160;/gi,' '); // newStr = newStr.replace(/&nbsp;/gi,' '); export const HtmlSpace = { regex: [/&#160;/gi, /&nbsp;/gi], human: ` `, charEntity: `&nbsp;`, numEntity: `&#160;`, }; // newStr = newStr.replace(/&lt;/gi,'<'); // newStr = newStr.replace(/&#60;/gi,'<'); export const HtmlLessThan = { regex: [/&#60;/gi, /&lt;/gi,], human: `<`, charEntity: `&lt;`, numEntity: `&#60;`, }; // newStr = newStr.replace(/&#62;/gi,'>'); // newStr = newStr.replace(/&gt;/gi,'>'); export const HtmlGreaterThan = { regex: [/&#62;/gi, /&gt;/gi], human: `>`, charEntity: `&gt;`, numEntity: `&#62;`, }; // newStr = newStr.replace(/&#38;/gi,'&'); // newStr = newStr.replace(/&amp;/gi,'&'); export const HtmlApmersand = { regex: [/&#38;/gi, /&amp;/gi], human: `&`, charEntity: `&amp;`, numEntity: `&#38;`, }; export const HtmlLQuestion = { regex: [/&#63;/gi, /&quest;/gi], human: `?`, charEntity: `&quest;`, numEntity: `&#63;`, }; export const HtmlParenthRight = { regex: [/&#41;/gi, /&rpar;/gi], human: `)`, charEntity: `&rpar;`, numEntity: `&#41;`, }; export const HtmlParenthLeft = { regex: [/&#40;/gi, /&lpar;/gi], human: `(`, charEntity: `&lpar;`, numEntity: `&#40;`, }; export const HtmlForwardSlash = { regex: [/&#8260;/gi, /&frasl;/gi], human: `/`, charEntity: `&frasl;`, numEntity: `&#8260;`, }; export const OriginalHTMLConversions = [ HtmlCurleyLeft, HtmlCurleyRight, HtmlDoubleQuote, HtmlSingleQuote, HtmlColon, HtmlSpace, HtmlLessThan, HtmlGreaterThan, HtmlApmersand, ]; export const AdditionalHTMLConversions = [ HtmlLQuestion, HtmlParenthRight, HtmlParenthLeft, HtmlForwardSlash ]; export const AllHTMLConversions = [ ...OriginalHTMLConversions, ...AdditionalHTMLConversions ]; //# sourceMappingURL=htmlV2.js.map