@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
JavaScript
/**
* 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'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 . 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(/{/gi,'{');
export const HtmlCurleyLeft = {
regex: [/{/gi, /{/gi],
human: `{`,
charEntity: `{`,
numEntity: `{`,
};
// newStr = newStr.replace(/}/gi,'}');
export const HtmlCurleyRight = {
regex: [/}/gi, /}/gi],
human: `}`,
charEntity: `}`,
numEntity: `}`,
};
// newStr = newStr.replace(/\\"/gi,'"');
// newStr = newStr.replace(/"/gi,'"');
export const HtmlDoubleQuote = {
regex: [/\\"/gi, /"/gi, /"/gi,],
human: `"`,
charEntity: `\\"`,
numEntity: `"`,
};
// newStr = newStr.replace(/\\'/gi,"'");
// newStr = newStr.replace(/'/gi,"'");
export const HtmlSingleQuote = {
regex: [/\\'/gi, /'/gi, /'/gi,],
human: `'`,
charEntity: `\\'`,
numEntity: `'`,
};
// newStr = newStr.replace(/:/gi,':');
export const HtmlColon = {
regex: [/:/gi, /:/gi,],
human: `:`,
charEntity: `:`,
numEntity: `:`,
};
// newStr = newStr.replace(/ /gi,' ');
// newStr = newStr.replace(/ /gi,' ');
export const HtmlSpace = {
regex: [/ /gi, / /gi],
human: ` `,
charEntity: ` `,
numEntity: ` `,
};
// newStr = newStr.replace(/</gi,'<');
// newStr = newStr.replace(/</gi,'<');
export const HtmlLessThan = {
regex: [/</gi, /</gi,],
human: `<`,
charEntity: `<`,
numEntity: `<`,
};
// newStr = newStr.replace(/>/gi,'>');
// newStr = newStr.replace(/>/gi,'>');
export const HtmlGreaterThan = {
regex: [/>/gi, />/gi],
human: `>`,
charEntity: `>`,
numEntity: `>`,
};
// newStr = newStr.replace(/&/gi,'&');
// newStr = newStr.replace(/&/gi,'&');
export const HtmlApmersand = {
regex: [/&/gi, /&/gi],
human: `&`,
charEntity: `&`,
numEntity: `&`,
};
export const HtmlLQuestion = {
regex: [/?/gi, /?/gi],
human: `?`,
charEntity: `?`,
numEntity: `?`,
};
export const HtmlParenthRight = {
regex: [/)/gi, /)/gi],
human: `)`,
charEntity: `)`,
numEntity: `)`,
};
export const HtmlParenthLeft = {
regex: [/(/gi, /(/gi],
human: `(`,
charEntity: `(`,
numEntity: `(`,
};
export const HtmlForwardSlash = {
regex: [/⁄/gi, /⁄/gi],
human: `/`,
charEntity: `⁄`,
numEntity: `⁄`,
};
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