|
1 | 1 | # `@telegraf/entity` [](https://deno.land/x/telegraf_entity) [](https://npmjs.com/package/@telegraf/entity) [](https://npmjs.com/package/@telegraf/entity) |
2 | 2 |
|
3 | | -Convert Telegram entities to HTML or Markdown. |
| 3 | +Convert Telegram entities to HTML or Markdown (and back). |
4 | 4 |
|
5 | 5 | > ⚠️ Before you start using this module, consider using [`copyMessage`](https://core.telegram.org/bots/api#copymessage) instead. |
6 | 6 | > |
@@ -82,3 +82,25 @@ import { serialiseWith, escapers } from "@telegraf/entity"; |
82 | 82 | const serialise = serialiseWith(myHTMLSerialiser, escapers.HTML); |
83 | 83 | serialise(ctx.message); |
84 | 84 | ``` |
| 85 | + |
| 86 | +## Parsing HTML and Markdown into entities |
| 87 | + |
| 88 | +We now have a fully Bot API-compliant parser for HTML and Markdown (MarkdownV2 not supported yet). This was ported over from tdlib, so it should parse exactly like Bot API does, and throw the same errors, but natively in JavaScript. |
| 89 | + |
| 90 | +This is quite an advanced usecase, and because it's just as strict as the official API, you will not be able to use this to leniently parse HTML or Markdown (for instance, to massage LLM output into entities for Telegram). |
| 91 | + |
| 92 | +Some usecases enabled by this: |
| 93 | + |
| 94 | +- For Telegram client-like applications, which want to parse exactly like Telegram. It's quite trivial to translate the resulting entities into TL types for use with MTProto, for instance. |
| 95 | +- The ability to statically check any piece of HTML or Markdown to ensure it's valid. |
| 96 | +- Fold existing HTML/Markdown into entities, to be used with Telegraf's [fmt helpers](https://github.com/feathers-studio/telegraf-docs/blob/master/guide/formatting.md#fmt) which construct entities directly. |
| 97 | + |
| 98 | +```TS |
| 99 | +import { parse_html, parse_markdown } from "@telegraf/entity"; |
| 100 | + |
| 101 | +const parsed = parse_html(html); // { text: string, entities: MessageEntity[] } |
| 102 | +// or |
| 103 | +const parsed = parse_markdown(markdown); // { text: string, entities: MessageEntity[] } |
| 104 | +``` |
| 105 | + |
| 106 | +Thanks to [codinary.org](https://codinary.org) for commissioning this feature. |
0 commit comments