Hello! Welcome to the tg-text-formatter package for Telegram text markdown. This library provides convenient methods for formatting text without the hassle of manual markdown syntax.
You can easily install this package via npm or yarn:
npm i tg-text-formatter
yarn add tg-text-formatter
There are two ways to use this library:
- Using classes
- Using methods
import { TelegramMarkdown } from "tg-text-formatter";
console.log(TelegramMarkdown.bold("idk")); // **idk**
console.log(TelegramMarkdown.cursive("idk")); // __idk__
console.log(TelegramMarkdown.boldCursive("idk")); // **__idk__**
console.log(TelegramMarkdown.crossedOut("idk")); // ~~idk~~
console.log(TelegramMarkdown.monospace("idk")); // `idk`
/**
* Code with language
*/
console.log(TelegramMarkdown.code("console.log(`idk`)", "ts")); // ```ts\nidk```
/**
* Code without language
*/
console.log(TelegramMarkdown.code("console.log(`idk`)")); // ```\nidk```
console.log(TelegramMarkdown.spoiler("idk")); // ||idk||
import {
bold,
cursive,
boldCursive,
crossedOut,
monospace,
code,
spoiler,
} from "tg-text-formatter";
console.log(bold("idk")); // **idk**
console.log(cursive("idk")); // __idk__
console.log(boldCursive("idk")); // **__idk__**
console.log(crossedOut("idk")); // ~~idk~~
console.log(monospace("idk")); // `idk`
/**
* Code with language
*/
console.log(code("console.log(`idk`)", "ts")); // ```ts\nidk```
/**
* Code without language
*/
console.log(code("console.log(`idk`)")); // ```\nidk```
console.log(spoiler("idk")); // ||idk||
The TelegramMentionParsers class provides methods for detecting and parsing Telegram mentions within text.
import { TelegramMentionParsers } from "tg-text-formatter";
console.log(TelegramMentionParsers.isMention("@Stickers")); // true
console.log(TelegramMentionParsers.isMention("@@Stickers")); // false
console.log(TelegramMentionParsers.mentions("@Stickers @akakuke", true)); // ["@Stickers", "@akakuke"]
console.log(TelegramMentionParsers.mentions("@Stickers @@akakuke", true)); // ["@Stickers"]
console.log(TelegramMentionParsers.mentions("Stickers akakuke", true)); // []
console.log(TelegramMentionParsers.isLinkMention("Check this link: https://t.me/username")); // true
isMention(content: string): boolean
- Checks if the content contains a valid mention.mentions(content: string, mentions?: boolean): string[]
- Extracts mentions from the content. Ifmentions
is true, it returns the mentions with the "@" symbol; otherwise, it returns without the "@".isLinkMention(content: string): boolean
- Checks if the content contains a valid Telegram link.
The TelegramMarkdownParser class provides methods for detecting and parsing various markdown styles in Telegram text.
isBold(text: string): boolean
- Checks if the text is bold.parseBold(text: string, markdown = false): string[]
- Parses bold text. Returns the formatted text ifmarkdown
is true, otherwise returns the plain text.isCursive(text: string): boolean
- Checks if the text is cursive.parseCursive(text: string, markdown = false): string[]
- Parses cursive text.isMonospace(text: string): boolean
- Checks if the text is monospace.parseMonospace(text: string, markdown = false): string[]
- Parses monospace text.isCode(text: string): boolean
- Checks if the text is a code block.parseCode(text: string, markdown = false): string[]
- Parses code blocks.isSpoiler(text: string): boolean
- Checks if the text is a spoiler.parseSpoiler(text: string, markdown = false): string[]
- Parses spoilers.isLink(text: string): boolean
- Checks if the text contains a markdown link.parseLink(text: string): boolean
- Parses markdown links. Now you can easily format your Telegram messages and manage mentions with this comprehensive library!