Convert Telegram Bot API message entities into HTML, or hast, and vice versa.
# Bun
bun add telegram-html
# Deno
deno add npm:telegram-html
# pnpm
pnpm add telegram-html
# npm
npm install telegram-htmlfunction messageToHast(message: Message, options?: MessageToHastOptions): Root;This function converts Telegram message into hast. It returns hast Root and accepts two arguments:
-
message (required)
The Telegram message to process.
-
options (optional)
Configuration for the conversion. It has three options:
-
withClass(optional, boolean, default:true)Adds a class attribute to the generated HTML tags.
By default, classes are prefixed with
tg-(e.g.,tg-bold,tg-custom-emoji). If set tofalse, classes are removed. Some entities (like hashtags) will become plain text because they cannot be styled without classes. -
classPrefix(optional, string, default:tg-)The prefix used for the HTML class names.
-
preserveEntityData(optional, boolean, default:false)Preserve original Telegram entity data in the HTML.
Set this to
trueif you need to convert the HTML back to Telegram entities later.Note: This increases the HTML size.
-
Example:
messageToHast(
{
entities: [{ length: 9, offset: 8, type: "bold" }],
text: "This is bold text",
},
{ preserveEntityData: true },
);function messageToHtml(message: Message, options?: MessageToHtmlOptions): string;This function converts a Telegram message into a semantic HTML string. It returns HTML string and accepts two arguments:
-
message (required)
The Telegram message to process.
-
options (optional)
Same options as
messageToHast.
Example:
messageToHtml(
{
entities: [{ length: 9, offset: 8, type: "bold" }],
text: "This is bold text",
},
{ preserveEntityData: true },
);function hastToMessage(hast: Root, options?: HastToMessageOptions): Message;Converts hast into a Telegram message. It returns Telegram message and accepts two arguments:
-
hast (required)
The hast to process.
-
options (optional)
Configuration for the conversion. It has two options:
-
classPrefix(optional, string, default:tg-)The prefix used to identify Telegram entities.
By default, elements with a class name starting with
tg-are identified as [Telegram entities]0tg-entities. For example, an element with the classtg-custom-emojiwill be converted into acustom_emojientity type.Change this option if you are using a different prefix, such as
telegram-.
-
-
skipAutoEntities(optional, boolean, default totrue)Skip entities that the Telegram server detects automatically.
When
true(the default), entities like hashtags, URLs, emails, etc. are skipped. Telegram parses these on its own, so sending them is not needed and only adds extra size.Set to
falseto include all entities.
Example:
hastToMessage(
{
type: "root",
children: [
{ type: "text", value: "This is " },
{
type: "element",
tagName: "b",
properties: { className: ["telegram-bold"] },
children: [{ type: "text", value: "bold text" }],
},
],
},
{ classPrefix: "telegram-", skipAutoEntities: false },
);function htmlToMessage(html: string, options?: HtmlToMessageOptions): Message;This function converts HTML into Telegram message. It returns Telegram message and provides two parameters:
-
html (required)
The HTML string to process.
-
options (optional)
The configuration option. It has the exact same options as hastToMessage.
Example:
htmlToMessage("This is <b>bold text</b>", { skipAutoEntities: false });telegram-html does not provide a way to convert Telegram Message into Markdown. However,
since HTML is widely supported, you can use messageToHtml to
convert the Telegram Message into HTML first, then convert HTML to Markdown using
third-party package and vice versa.
telegram-html provides conversion to hast. That way, it brings you the flexibility to modify them.
After you are done, you can convert them into HTML using
toHtml or into a Telegram message using
hastToMessage.
Please see CONTRIBUTING.md for contribution guidelines.