Format Telegram message texts with Markdown or HTML
This library abstracts the formatting options for you.
npm install telegram-format
const {html: format} = require('telegram-format');
const {markdownv2: format} = require('telegram-format');
import {html as format} from 'telegram-format';
import {markdownv2 as format} from 'telegram-format';
format.bold('hey');
//=> '*hey*'
format.italic('you');
//=> '_you_'
format.bold(format.italic('they'))
//=> '*_they_*'
format.url('me', 'https://edjopato.de');
//=> '[me](https://edjopato.de)'
// Legacy but still works
const {markdown: format} = require('telegram-format');
import {markdown as format} from 'telegram-format';
When using format
as an alias its easy to switch between Markdown and HTML fast.
When you have something that might be unescaped you need to use format.escape
before formatting it.
const username = 'master_yoda'
format.italic(format.escape(username))
format.monospace
and format.monospaceBlock
will escape on their own as they only need to escape specific characters.
You do not need to escape the input in this cases.
MarkdownV2
and HTML
are fairly similar in escaping inputs but Markdown
is not.
Markdown
is still supported by this library and by Telegram for legacy reasons but it behaves a bit differently.
Markdown
still escapes inputs and does not need format.escape
before.
Also nested formatting is not supported by telegram itself.
Consider switching to MarkdownV2
or HTML
for simplicity reasons.
Type: string
Text to be formatted bold
Type: string
Text to be formatted italic
Type: string
Text to be striked through
Type: string
Text to be underlined
Type: string
Text to be displayed as inline monospace
Type: string
Text to be displayed as monospace block
Type: string
(optional)
Can be used to specify the programming language of the code
Type: string
Label of the url field
Type: string
Url which is called when the user taps on the label
Type: string
Label of the user mention. Might be used with the first name for example.
Type: number
User id to which is linked
Type: string
Text which should be escaped so it does not interfer with formatting.
User generated Text should always be escapted to prevent errors.