Skip to content

Instantly share code, notes, and snippets.

@Nex-Otaku
Last active October 12, 2021 20:04
Show Gist options
  • Save Nex-Otaku/21b3ff63d7c3040309952d2fe4a27f06 to your computer and use it in GitHub Desktop.
Save Nex-Otaku/21b3ff63d7c3040309952d2fe4a27f06 to your computer and use it in GitHub Desktop.
Escape MarkdownV2 Telegram
// https://www.npmjs.com/package/telegramify-markdown
const telegramifyMarkdown = require('telegramify-markdown');
const escapeMarkdownV2 = (text) => {
// Функция telegramifyMarkdown не совсем корректно работает, глючит с форматом __Underlined__,
// поэтому обходим этот глюк с помощью замены "__" на "@@" и обратно.
const dogText = text.replace(/__/gi, '@@');
const lines = dogText.split('\n');
const percentText = lines.map(line => {
return line.startsWith('*') && line.endsWith('*')
? '%%' + line.substring(1, line.length - 1) + '%%'
: line
}).join('\n');
const mdEscapedText = telegramifyMarkdown(percentText);
const undog = mdEscapedText.replace(/@@/gi, '__');
return undog.replace(/%%/gi, '*');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment