diff --git a/src/lib/utils.js b/src/lib/utils.js index 21016dd..bd9d9d7 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -279,6 +279,40 @@ async function lookupUser(msg, str, filter) { } } +// https://stackoverflow.com/a/39243641 +const htmlEntities = { + nbsp: " ", + cent: "¢", + pound: "£", + yen: "¥", + euro: "€", + copy: "©", + reg: "®", + lt: "<", + gt: ">", + quot: '"', + amp: "&", + apos: "'", +}; + +function parseHtmlEntities(str) { + return str.replace(/&([^;]+);/g, function (entity, entityCode) { + var match; + + if (entityCode in htmlEntities) { + return htmlEntities[entityCode]; + /*eslint no-cond-assign: 0*/ + } else if ((match = entityCode.match(/^#x([\da-fA-F]+)$/))) { + return String.fromCharCode(parseInt(match[1], 16)); + /*eslint no-cond-assign: 0*/ + } else if ((match = entityCode.match(/^#(\d+)$/))) { + return String.fromCharCode(~~match[1]); + } else { + return entity; + } + }); +} + module.exports = { pastelize, getTopColor, @@ -291,4 +325,5 @@ module.exports = { hastebin, selectionMessage, lookupUser, + parseHtmlEntities, };