chomens-bot-js/util/escapeMarkdown.js

23 lines
491 B
JavaScript
Raw Normal View History

2022-08-14 05:51:45 -04:00
/**
2022-11-15 21:33:16 -05:00
* escape markdown so on discord it will be \_ChipMC\_ instead of _ChipMC_
2022-08-14 05:51:45 -04:00
* @param {String} text
2022-10-16 02:19:14 -04:00
* @param {Boolean} zwsp
2022-08-14 05:51:45 -04:00
* @return {String}
*/
2022-10-16 02:19:14 -04:00
function escapeMarkdown(text, zwsp) {
2022-11-15 21:33:16 -05:00
let unescaped;
let escaped;
2022-08-14 05:51:45 -04:00
try {
2022-11-15 21:33:16 -05:00
unescaped = text.replace(/\\(\*|@|_|`|~|\\)/g, '$1');
escaped = unescaped.replace(/(\*|@|_|`|~|\\)/g, zwsp ?
'\u200b\u200b$1' :
'\\$1',
);
2022-08-14 05:51:45 -04:00
} catch (e) {
return unescaped;
}
return escaped;
}
module.exports = {escapeMarkdown};