import UserService from 'services/UserService'; export default /** @lends module:core.utils.htmlHelpers */ { /** * Highlights fragments within a text with <span mark='highlight'></span>. * @param {String} rawText Text to highlight. * @param {String} fragment highlighted fragment. * @param {Boolean} [escape=true] If true, Handlebars.Utils.escapeExpression will be applied to * the text before highlighting. * @return {String} Highlighted text * */ highlightText(rawText: string, fragment: any, escape = true) { if (!fragment || typeof fragment !== 'string') { return rawText; } let text = rawText; if (!text) { return ''; } if (escape) { text = Handlebars.Utils.escapeExpression(text); } const lowerText = String(text).toLowerCase(); let startIndex = 0; let index; let output = ''; const fragmentLower = fragment.toLowerCase(); while ((index = lowerText.indexOf(fragmentLower, startIndex)) !== -1) { const index2 = index + fragment.length; output += `${text.substring(startIndex, index)}${text.substring(index, index2)}`; startIndex = index2; } if (startIndex < text.length) { output += text.substring(startIndex); } return output; }, /** * Highlights html fragments within a text with <span class='highlight'></span>. * @param {String} rawHTML Html to highlight. * @param {String} fragment highlighted fragment. * @return {String} Highlighted text * */ highlightHtml(rawHTML: any, fragment: string) { if (!rawHTML) { return; } const stringHtml = String(rawHTML).trim(); if (stringHtml.startsWith('<') && stringHtml.endsWith('>')) { return stringHtml .replace(/>[^>]*<\//g, str=> this.highlightText(str, fragment, false)); } return this.highlightText(stringHtml, fragment, false); }, /** * Highlights mentions within a text with <a href='...'></a>. * @param {String} rawText Text to highlight. * @param {Boolean} [escape=true] If true, Handlebars.Utils.escapeExpression will be applied to * the text before highlighting. * @return {String} Highlighted text * */ highlightMentions(rawText, escape) { let text = rawText; if (!text) { return ''; } if (escape || escape === undefined) { text = Handlebars.Utils.escapeExpression(text); } const membersByUserName = UserService.listUsers().reduce((memo, user) => { if (user.userName) { memo[user.userName] = user; } return memo; }, {}); const regex = /(\s|^)@([a-z0-9_\.]+)/gi; return text.replace(regex, (fragment, whitespace, userName: string) => { const user = membersByUserName[userName]; if (user) { return `${whitespace}@${user.userName}`; } return fragment; }); }, /** * Highlights urls within a text with <a href='...'></a>. * @param {String} rawText Text to highlight. * @param {Boolean} [escape=true] If true, Handlebars.Utils.escapeExpression will be applied on to * the text before highlighting. * @return {String} Highlighted text * */ highlightUrls(rawText, escape) { let text = rawText; if (!text) { return ''; } if (escape || escape === undefined) { text = Handlebars.Utils.escapeExpression(text); } const regex = /(?:ht|f)tp(?:s?):\/\/[^\s]*/gi; return text.replace(regex, url => `${url}`); }, /** * Highlights urls within a text with <a href='...'></a>. * @param {String} htmlText HTML to escape all tags info. * @return {String} Escaped text * */ getTextfromHTML(htmlText = '') { return String(htmlText) .replace(/<[^>]*>/g, '') .replace(/"/g, '"') .replace(/ /g, ' '); } };