Last active
October 12, 2021 20:04
-
-
Save Nex-Otaku/21b3ff63d7c3040309952d2fe4a27f06 to your computer and use it in GitHub Desktop.
Escape MarkdownV2 Telegram
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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