Telegram MarkdownV2 formatter
telegram-md
is a powerful TypesScript library designed to simplify the formatting of text messages in Telegram MarkdownV2 format. It provides a range of methods for applying markdown formatting, such as bold, italic, and links, making it easier to create richly formatted messages programmatically.
npm install @vlad-yakovlev/telegram-md
import { md } from '@vlad-yakovlev/telegram-md'
// Simple message formatting
const message = md`Hello, ${md.bold('World')}!`
api.sendMessage(chatId, md.build(message))
// Using different formatting styles
const complexMessage = md`
This is an ${md.italic('italic')}
and ${md.bold('bold')} text
with a ${md.link('link', 'http://example.com')}!
`
api.sendMessage(chatId, md.build(complexMessage))
Every method escapes all unescaped input. Input is assumed to be escaped only when it's an instance of Markdown.
Stores the result of executing md methods. Used to differentiate between normal strings and escaped strings.
Template tag which can be used to build markdown formatted messages.
md`Hello, ${md.bold('World')}!` // => Markdown with value 'Hello, *World*\\!'
Returns message text that cat be safely sent to telegram API.
md.build(md`Hello, ${md.bold('World')}!`) // => 'Hello, *World*\\!'
md.build('Hello, World!') // => 'Hello, World\\!'
md.bold('bold *text') // => Markdown with value '*bold \*text*'
md.italic('italic *text') // => Markdown with value '_italic \*text_'
md.underline('underline') // => Markdown with value '__underline__'
md.strikethrough('strikethrough') // => Markdown with value '~strikethrough~'
md.spoiler('spoiler') // => Markdown with value '||spoiler||'
md.link('inline URL', 'http://www.example.com/') // => Markdown with value '[inline URL](http://www\\.example\\.com/)'
md.link('inline mention of a user', 'tg://user?id=123456789') // => Markdown with value '[inline mention of a user](tg://user?id\\=123456789)'
md.inlineCode('inline fixed-width code') // => Markdown with value '`inline fixed\\-width code`'
md.code(
'pre-formatted fixed-width code block',
) // => Markdown with value
pre\-formatted fixed\-width code block
md.code(
'pre-formatted fixed-width code block written in the Python',
'python',
) // => Markdown with value
```python
pre\\-formatted fixed\\-width code block written in the Python