Skip to content

Commit ff6fd96

Browse files
committed
Add Formatting Text Walkthrough section
1 parent 3404d5c commit ff6fd96

3 files changed

Lines changed: 76 additions & 0 deletions

File tree

src/formatting-text.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
title: Formatting Text
3+
parent: /#walkthrough
4+
prev: /inline-queries
5+
---
6+
7+
Messages and media captions can contain formatting such as bold text, links, spoilers, and code. MTKruto can parse HTML or Markdown into Telegram message entities.
8+
9+
## HTML
10+
11+
Set `parseMode` to `"HTML"` for a message that contains supported HTML tags.
12+
13+
```ts
14+
await client.sendMessage(
15+
chatId,
16+
'Hello, <b>world</b>! Visit <a href="https://mtkru.to">MTKruto</a>.',
17+
{ parseMode: "HTML" },
18+
);
19+
```
20+
21+
Common supported tags include `<b>`, `<i>`, `<u>`, `<s>`, `<code>`, `<pre>`, `<blockquote>`, and `<a>`.
22+
23+
## Markdown
24+
25+
Set `parseMode` to `"Markdown"` to use Markdown formatting.
26+
27+
```ts
28+
await ctx.reply(
29+
"Hello, *world*\\! Visit [MTKruto](https://mtkru.to)\\.",
30+
{ parseMode: "Markdown" },
31+
);
32+
```
33+
34+
Characters with special meaning must be escaped when they should appear literally. Be especially careful when inserting user-provided text into HTML or Markdown.
35+
36+
## Default Parse Mode
37+
38+
A default parse mode can be configured when constructing the client. It is then used whenever a method does not specify one.
39+
40+
```ts
41+
const client = new Client({
42+
parseMode: "HTML",
43+
/* ... */
44+
});
45+
```
46+
47+
Override the default for a single message by passing another parse mode, or pass `null` to disable parsing.
48+
49+
```ts
50+
await client.sendMessage(chatId, "<b>This remains unchanged.</b>", {
51+
parseMode: null,
52+
});
53+
```
54+
55+
## Explicit Entities
56+
57+
You can provide {{ "MessageEntity" |> t }} objects instead of using markup. Entity offsets and lengths use UTF-16 code units, which is how JavaScript measures string length.
58+
59+
```ts
60+
const text = "Important message";
61+
62+
await client.sendMessage(chatId, text, {
63+
parseMode: null,
64+
entities: [
65+
{
66+
type: "bold",
67+
offset: 0,
68+
length: "Important".length,
69+
},
70+
],
71+
});
72+
```
73+
74+
Explicit entities are useful when text is assembled dynamically because formatting does not depend on escaping markup characters.

src/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Its key features include:
3434
9. [Keyboards and Callback Queries](/keyboards-and-callback-queries) {{ "/keyboards-and-callback-queries" |> i }}
3535
10. [Files](/files) {{ "/files" |> i }}
3636
11. [Inline Queries](/inline-queries) {{ "/inline-queries" |> i }}
37+
12. [Formatting Text](/formatting-text) {{ "/formatting-text" |> i }}
3738

3839
### Guides
3940

src/inline-queries.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
title: Inline Queries
33
parent: /#walkthrough
44
prev: /files
5+
next: /formatting-text
56
---
67

78
Inline mode lets users interact with a bot from any chat by typing its username followed by a query. It must first be enabled for the bot through [@BotFather](https://t.me/BotFather).

0 commit comments

Comments
 (0)