Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Entity Parser plugin docs #1121

Merged
merged 53 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
1c0f555
Add grammy-entity plugin
quadratz Sep 9, 2024
e8abe7d
Fix fmt
quadratz Sep 9, 2024
8a46283
Fix typo
quadratz Sep 9, 2024
8670569
Merge branch 'main' into grammy-entity
KnorpelSenf Sep 11, 2024
97fb68b
update docs
quadratz Sep 17, 2024
ec4a786
Merge branch 'main' into grammy-entity
quadratz Sep 18, 2024
af7be6d
update plugin list
quadratz Sep 18, 2024
56a6e47
remove the old file
quadratz Sep 18, 2024
ab91a75
fix formatting
quadratz Sep 18, 2024
1f4bbda
Rename plugin name to `entity-parser`
quadratz Sep 22, 2024
63b72f5
Fix typo entity type `expandableBlockquote`
quadratz Sep 22, 2024
26e163f
Apply suggestions from code review
quadratz Sep 23, 2024
47ae41a
fix fmt
quadratz Sep 23, 2024
e6014ef
fix typo
quadratz Sep 23, 2024
1f889b9
Apply suggestions from code review
quadratz Sep 23, 2024
cd83a90
Apply suggestions from code review
quadratz Sep 23, 2024
8170e59
Sync changes to Indonesian
quadratz Oct 14, 2024
af2832f
Revert "Sync changes to Indonesian"
quadratz Oct 14, 2024
92942ee
Merge branch 'main' into grammy-entity
KnorpelSenf Oct 14, 2024
df69cc5
Merge branch 'main' into grammy-entity
rojvv Oct 15, 2024
17c66a7
Merge branch 'main' into grammy-entity
KnorpelSenf Oct 15, 2024
c69a098
Update site/docs/plugins/entity-parser.md
quadratz Oct 16, 2024
300c5b6
Sync deno 2.0
quadratz Oct 16, 2024
d91166a
Merge branch 'main' into grammy-entity
KnorpelSenf Nov 3, 2024
e9a5025
Merge branch 'main' into grammy-entity
LWJerri Nov 4, 2024
bff39af
Merge branch 'main' into grammy-entity
KnorpelSenf Nov 21, 2024
572984f
Merge branch 'main' into grammy-entity
KnorpelSenf Nov 21, 2024
f0e2f62
sync Russian
MasedMSD Nov 22, 2024
e29a5aa
sync to Ukrainian
niusia-ua Nov 22, 2024
43fd83f
fix fmt errors
MasedMSD Nov 22, 2024
e09e84f
again
MasedMSD Nov 22, 2024
5aaab5d
Update site/docs/uk/plugins/entity-parser.md
LWJerri Nov 22, 2024
4c41245
en: fix formatting
quadratz Nov 22, 2024
d7377d1
sync commit 5740978 to all languages
quadratz Nov 22, 2024
24c5dec
sync changes to indonesian
quadratz Nov 23, 2024
fd584ec
id: fix fmt
quadratz Nov 23, 2024
b1faf0e
little fix
MasedMSD Nov 23, 2024
eb6bc11
fix fmt
MasedMSD Nov 23, 2024
b0b1300
ru: changed order
MasedMSD Dec 3, 2024
ad8ae5c
Add spanish
habemuscode Dec 6, 2024
56a846f
Apply formatter
habemuscode Dec 6, 2024
124f161
Merge branch 'main' into grammy-entity
habemuscode Dec 6, 2024
3e4d055
Apply suggestions from code review
niusia-ua Dec 16, 2024
465daa2
Update site/docs/uk/plugins/entity-parser.md
LWJerri Dec 17, 2024
73cc355
Merge branch 'main' into grammy-entity
KnorpelSenf Dec 21, 2024
b6ff110
Merge branch 'main' into grammy-entity
LWJerri Dec 24, 2024
ce3b123
Merge branch 'main' into grammy-entity
KnorpelSenf Dec 24, 2024
b685ae1
Merge branch 'main' into grammy-entity
MasedMSD Jan 5, 2025
9aa45c9
Merge branch 'main' into grammy-entity
KnorpelSenf Jan 12, 2025
79e5b07
Merge branch 'main' into grammy-entity
KnorpelSenf Jan 13, 2025
b80ac35
Grammy entity zh (#1178)
agoudbg Jan 15, 2025
f9982d7
[Zh] fix "or" translation (#1179)
agoudbg Jan 16, 2025
4c556b7
duzen statt siezen
Jan 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions site/docs/.vitepress/configs/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ const pluginThirdparty = {
text: "Autoquote",
link: "/plugins/autoquote",
},
{
text: "Convert Telegram Entities",
link: "/plugins/grammy-entity",
},
{
text: "[Submit your PR!]",
link: "/plugins/#create-your-own-plugins",
Expand Down
125 changes: 125 additions & 0 deletions site/docs/plugins/grammy-entity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
---
prev: false
next: false
---

# Convert Telegram Entities (grammy-entity)

Converts [Telegram entities](https://core.telegram.org/bots/api#messageentity) to [HTML](https://core.telegram.org/bots/api#messageentity) or [Markdown](https://core.telegram.org/bots/api#markdownv2-style).

## When Should I Use This?

Probably NEVER!

While this plugin can generate Telegram-compatible HTML or MarkdownV2, it’s usually best to just send the text and entities back to Telegram.

Converting them to HTML or Markdown is only necessary in those rare cases when you need to use Telegram-formatted text **outside** of Telegram itself.

If you’re still unsure whether this plugin is the right fit for your use case, please don’t hesitate to ask in our [Telegram group](https://t.me/grammyjs).
In most cases, people find they don’t actually need this plugin to solve their problems!

## Installation

Simply run this line of code in your terminal.

::: code-group

```sh:no-line-numbers [Deno]
deno add @qz/grammy-entity
```

```sh:no-line-numbers [Bun]
bunx jsr add @qz/grammy-entity
```

```sh:no-line-numbers [pnpm]
pnpm dlx jsr add @qz/grammy-entity
```

```sh:no-line-numbers [Yarn]
yarn dlx jsr add @qz/grammy-entity
```

```sh:no-line-numbers [npm]
npx jsr add @qz/grammy-entity
```

:::

## Simple Usage

Using this plugin is super straightforward!
Here’s a quick example.

```ts
import { toHTML, toMarkdownV2 } from "@qz/grammy-entity";

bot.on(":text", async (ctx) => {
const html = toHTML(ctx.msg); // Convert text to HTML string
const md = toMarkdownV2(ctx.msg); // Convert text to MarkdownV2 string
});
```

Both functions will also work with captioned messages, such as photos or videos.

```ts
bot.on(":photo", async (ctx) => {
const html = toHTML(ctx.msg); // Convert caption to HTML string
const md = toMarkdownV2(ctx.msg); // Convert caption to MarkdownV2 string
});
```

You can also pass a text and entities object directly:

```ts
toHTML({ text: '...', entities: [...] }); // Get the HTML string
```

## Advanced Usage

The `toHTML` and `toMarkdown` functions are designed to produce HTML or Markdown that’s compatible with Telegram, which is a sensible default for a Telegram library.

However, if you want to serialize your data differently for another system, you can do that too!
Simply use the `serializeWith` function.

To get started, you’ll need to create a serializer with the following type.

```ts
import type { Serializer } from "@qz/grammy-entity";

const myHTMLSerializer: Serializer (match, node) {
// Your custom implementation here
}
```

Each matched node will be passed to your function, and you only need to wrap it however you like!

If you’re looking for inspiration, check out the implementation of the [serializers](https://github.com/quadratz/grammy-entity/blob/main/src/serializers.ts).
You can easily copy, paste, and tweak it to fit your needs.

We’ve also got some built-in escapers available for your convenience.

```ts
import { escapeHtml, escapeMarkdownV2, type Escaper } from "@qz/grammy-entity";

escapeHtml(text); // Get HTML escaped text
escapeMarkdownV2(text); // Escape text for Telegram's MarkdownV2

// Or create your own escaper
const myEscaper: Escaper = (match) => {/* Your implementation here */};
```

With these tools at your disposal, you can whip up your own HTML serializer like this:

```ts
import { escapeHtml, serializeWith } from "@qz/grammy-entity";

const serialize = serializeWith(myHTMLSerializer, escapeHtml);
serialize(ctx.msg);
```

## Plugin Summary

- Name: grammy-entity
- Package: <https://jsr.io/@qz/grammy-entity>
- Source: <https://github.com/quadratz/grammy-entity>
Loading