chomens-bot-js/util/escapeMarkdown.js

20 lines
532 B
JavaScript
Raw Normal View History

2022-08-14 05:51:45 -04:00
/* eslint-disable no-var */
/* eslint-disable max-len */
/**
* escape markdown so on discord it will be \_ChipMC\_ instead of ChipMC in italic
* @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-08-14 05:51:45 -04:00
try {
var unescaped = text.replace(/\\(\*|@|_|`|~|\\)/g, '$1');
2022-10-16 02:19:14 -04:00
var escaped = unescaped.replace(/(\*|@|_|`|~|\\)/g, zwsp ? '\u200b\u200b\u200b\u200b\u200b$1' : '\\$1');
2022-08-14 05:51:45 -04:00
} catch (e) {
return unescaped;
}
return escaped;
}
module.exports = {escapeMarkdown};