You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Powerful and type-safe text formatting library for Telegram Bot API with full HTML and MarkdownV2 support.
✨ Features
🎨 Multiple Format Support - HTML, MarkdownV2, and legacy Markdown
🔒 Type-Safe - Full TypeScript support with comprehensive type definitions
🛡️ Auto-Escaping - Automatic escaping of special characters
🎯 Simple API - Intuitive and easy-to-use formatting functions
📦 Zero Dependencies - Lightweight with no external dependencies
🔧 Flexible - Works with any Telegram bot library
⚡ Fast - Optimized for performance
📚 Well-Documented - Extensive documentation and examples
📦 Installation
npm install @telegram.ts/formatters
yarn add @telegram.ts/formatters
pnpm add @telegram.ts/formatters
🚀 Quick Start
import{html,markdownv2}from"@telegram.ts/formatters";// HTML formattingconsthtmlText=html.bold("Bold text")+"\n"+html.italic("Italic text")+"\n"+html.underline("Underlined text")+"\n"+html.inlineURL("Click here","https://example.com");// MarkdownV2 formattingconstmarkdownText=markdownv2.bold("Bold text")+"\n"+markdownv2.italic("Italic text")+"\n"+markdownv2.strikethrough("Strikethrough text");// Send with your bot libraryawaitbot.sendMessage(chatId,htmlText,{parse_mode: "HTML"});awaitbot.sendMessage(chatId,markdownText,{parse_mode: "MarkdownV2"});
📖 Usage
HTML Formatting
import{html}from"@telegram.ts/formatters";// Basic formattinghtml.bold("Bold text");// <b>Bold text</b>html.italic("Italic text");// <i>Italic text</i>html.underline("Underlined");// <u>Underlined</u>html.strikethrough("Strikethrough");// <s>Strikethrough</s>html.spoiler("Spoiler text");// <span class="tg-spoiler">Spoiler text</span>// Links and mentionshtml.inlineURL("Google","https://google.com");// <a href="https://google.com">Google</a>html.inlineMention("John",123456789);// <a href="tg://user?id=123456789">John</a>// Codehtml.inlineCode("const x = 1");// <code>const x = 1</code>html.codeBlock("const x = 1;\nconsole.log(x);","javascript");// <pre><code class="language-javascript">const x = 1;// console.log(x);</code></pre>// Quoteshtml.blockquote("This is a quote");// <blockquote>This is a quote</blockquote>html.expandableBlockquote("Long quote...");// <blockquote expandable>Long quote...</blockquote>// Custom emojihtml.inlineEmoji("👍","5368324170671202286");// <tg-emoji emoji-id="5368324170671202286">👍</tg-emoji>
MarkdownV2 Formatting
import{markdownv2}from"@telegram.ts/formatters";// Basic formattingmarkdownv2.bold("Bold text");// *Bold text*markdownv2.italic("Italic text");// _Italic text_markdownv2.underline("Underlined");// __Underlined__markdownv2.strikethrough("Strike");// ~Strike~markdownv2.spoiler("Spoiler");// ||Spoiler||// Links and mentionsmarkdownv2.inlineURL("Google","https://google.com");// [Google](https://google.com)markdownv2.inlineMention("John",123456789);// [John](tg://user?id=123456789)// Codemarkdownv2.inlineCode("const x = 1");// `const x = 1`markdownv2.codeBlock("const x = 1;","javascript");// ```javascript// const x = 1;// ```// Quotesmarkdownv2.blockquote("Quote");// >Quotemarkdownv2.expandableBlockquote("Long");// **>Long// Custom emojimarkdownv2.inlineEmoji("👍","5368324170671202286");// 
Combining Formats
import{html}from"@telegram.ts/formatters";// Combine multiple formatsconsttext=html.combine([html.bold("Hello"),html.italic("World")]," ");// <b>Hello</b> <i>World</i>// Create listsconstlist=html.list(["First item","Second item","Third item"],true);// true for ordered list// 1. First item// 2. Second item// 3. Third item
Working with Entities
import{parseEntities,entitiesToFormatted}from"@telegram.ts/formatters";// Parse formatted text to entitiesconsttext=html.bold("Hello")+" "+html.italic("World");constentities=parseEntities(text,"HTML");// Convert entities back to formatted textconstformattedText=entitiesToFormatted("Hello World",entities,"HTML");
Utilities
import{escapeHTML,escapeMarkdownV2,stripHTML,getTextLength,}from"@telegram.ts/formatters";// Escape special charactersescapeHTML("<script>alert('XSS')</script>");// <script>alert('XSS')</script>escapeMarkdownV2("Text with *special* chars");// Text with \\*special\\* chars// Strip HTML tagsstripHTML("<b>Bold</b> text");// Bold text// Get text length without formattinggetTextLength("<b>Hello</b>","HTML");// 5
import{html}from"@telegram.ts/formatters";functioncreateMenu(){returnhtml.combine([html.bold("🎮 MAIN MENU"),html.strikethrough("━━━━━━━━━━━━━"),"",html.bold("1. ")+html.underline("Profile")+" 👤",html.bold("2. ")+html.underline("Settings")+" ⚙️",html.bold("3. ")+html.underline("Help")+" ❓","",html.italic("Choose an option"),],"\n",);}
Code Example
import{html}from"@telegram.ts/formatters";constcode=`import { TelegramClient } from "telegramsjs";const client = new TelegramClient("TOKEN");client.on("ready", () => { console.log("Bot is ready!");});client.login();`;constmessage=html.combine([html.bold("📝 Example Code"),"",html.codeBlock(code.trim(),"typescript"),"",html.italic("Copy and paste to get started!"),],"\n",);
import{html}from"@telegram.ts/formatters";functioncreateError(error: Error){returnhtml.combine([html.bold("❌ ERROR"),html.strikethrough("━━━━━━━━━━"),"",html.italic("An error occurred:"),"",html.codeBlock(error.message,"text"),"",html.underline("Please try again later"),],"\n",);}
📚 API Reference
HTML Formatter
Function
Description
Example
bold(text)
Bold text
<b>text</b>
italic(text)
Italic text
<i>text</i>
underline(text)
Underlined text
<u>text</u>
strikethrough(text)
Strikethrough text
<s>text</s>
spoiler(text)
Spoiler text
<span class="tg-spoiler">text</span>
blockquote(text)
Block quote
<blockquote>text</blockquote>
expandableBlockquote(text)
Expandable quote
<blockquote expandable>text</blockquote>
inlineURL(text, url)
Inline link
<a href="url">text</a>
inlineMention(text, userId)
User mention
<a href="tg://user?id=123">text</a>
inlineCode(text)
Inline code
<code>text</code>
codeBlock(text, lang?)
Code block
<pre><code>text</code></pre>
inlineEmoji(emoji, id)
Custom emoji
<tg-emoji emoji-id="id">emoji</tg-emoji>
combine(parts, sep?)
Combine strings
Joins array with separator
list(items, ordered?)
Create list
Formats array as list
MarkdownV2 Formatter
Function
Description
Example
bold(text)
Bold text
*text*
italic(text)
Italic text
_text_
underline(text)
Underlined text
__text__
strikethrough(text)
Strikethrough text
~text~
spoiler(text)
Spoiler text
||text||
blockquote(text)
Block quote
>text
expandableBlockquote(text)
Expandable quote
**>text
inlineURL(text, url)
Inline link
[text](url)
inlineMention(text, userId)
User mention
[text](tg://user?id=123)
inlineCode(text)
Inline code
`text`
codeBlock(text, lang?)
Code block
```lang\ntext\n```
inlineEmoji(emoji, id)
Custom emoji

Utilities
Function
Description
escapeHTML(text)
Escape HTML special characters
escapeMarkdownV2(text)
Escape MarkdownV2 special characters
escapeMarkdown(text)
Escape Markdown special characters
stripHTML(text)
Remove HTML tags from text
getTextLength(text, mode)
Get text length without formatting
Parser Functions
Function
Description
parseEntities(text, mode)
Parse formatted text to entities
entitiesToFormatted(text, entities, mode)
Convert entities to formatted text
calculateOffset(text, position)
Calculate offset in plain text
mergeEntities(entities)
Merge overlapping entities
🔧 Integration Examples
With TelegramsJS
import{TelegramClient}from"telegramsjs";import{html}from"@telegram.ts/formatters";constclient=newTelegramClient("YOUR_BOT_TOKEN");client.on("message",async(message)=>{if(message.content==="/start"){consttext=html.combine([html.bold("Welcome!"),html.italic("I'm a bot with beautiful formatting"),],"\n",);awaitmessage.reply(text,{parse_mode: "HTML"});}});client.login();
With node-telegram-bot-api
importTelegramBotfrom"node-telegram-bot-api";import{html}from"@telegram.ts/formatters";constbot=newTelegramBot("YOUR_BOT_TOKEN",{polling: true});bot.onText(/\/start/,(msg)=>{consttext=html.bold("Hello!")+"\n"+html.italic("Welcome to the bot");bot.sendMessage(msg.chat.id,text,{parse_mode: "HTML"});});