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');
|
2022-01-12 12:54:04 +00:00
|
|
|
const replaceShortNameWithUnicode = (shortname: string) => 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
|
|
|
|
2022-01-12 12:54:04 +00:00
|
|
|
const unescapeHTML = (string: string) => {
|
|
|
|
const unescaped: { [key: string]: string } = {
|
2019-12-11 19:00:38 +00:00
|
|
|
'&': '&',
|
|
|
|
'&': '&',
|
|
|
|
'&': '&',
|
|
|
|
'<': '<',
|
|
|
|
'<': '<',
|
|
|
|
'<': '<',
|
|
|
|
'>': '>',
|
|
|
|
'>': '>',
|
|
|
|
'>': '>',
|
|
|
|
'"': '"',
|
|
|
|
'"': '"',
|
|
|
|
'"': '"',
|
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
|
|
|
};
|
|
|
|
|
2022-01-12 12:54:04 +00:00
|
|
|
const shortnameToUnicode = (str: string): string => {
|
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;
|