2019-12-11 19:00:38 +00:00
|
|
|
import emojis from './emojis';
|
|
|
|
import ascii, { asciiRegexp } from './ascii';
|
|
|
|
|
|
|
|
const shortnamePattern = new RegExp(/:[-+_a-z0-9]+:/, 'gi');
|
|
|
|
const replaceShortNameWithUnicode = shortname => emojis[shortname] || shortname;
|
2021-09-13 20:41:05 +00:00
|
|
|
const regAscii = new RegExp(`((\\s|^)${asciiRegexp}(?=\\s|$|[!,.?]))`, 'gi');
|
2019-12-11 19:00:38 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const unescapeHTML = string => {
|
2019-12-11 19:00:38 +00:00
|
|
|
const unescaped = {
|
|
|
|
'&': '&',
|
|
|
|
'&': '&',
|
|
|
|
'&': '&',
|
|
|
|
'<': '<',
|
|
|
|
'<': '<',
|
|
|
|
'<': '<',
|
|
|
|
'>': '>',
|
|
|
|
'>': '>',
|
|
|
|
'>': '>',
|
|
|
|
'"': '"',
|
|
|
|
'"': '"',
|
|
|
|
'"': '"',
|
2021-09-13 20:41:05 +00:00
|
|
|
''': "'",
|
|
|
|
''': "'",
|
|
|
|
''': "'"
|
2019-12-11 19:00:38 +00:00
|
|
|
};
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
return string.replace(/&(?:amp|#38|#x26|lt|#60|#x3C|gt|#62|#x3E|apos|#39|#x27|quot|#34|#x22);/gi, match => unescaped[match]);
|
2019-12-11 19:00:38 +00:00
|
|
|
};
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const shortnameToUnicode = str => {
|
2019-12-11 19:00:38 +00:00
|
|
|
str = str.replace(shortnamePattern, replaceShortNameWithUnicode);
|
|
|
|
|
|
|
|
str = str.replace(regAscii, (entire, m1, m2, m3) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
if (!m3 || !(unescapeHTML(m3) in ascii)) {
|
2019-12-11 19:00:38 +00:00
|
|
|
// if the ascii doesnt exist just return the entire match
|
|
|
|
return entire;
|
|
|
|
}
|
|
|
|
|
|
|
|
m3 = unescapeHTML(m3);
|
|
|
|
return ascii[m3];
|
|
|
|
});
|
|
|
|
|
|
|
|
return str;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default shortnameToUnicode;
|