HTML entities parser for mtcute
npm install @mtcute/html-parserHTML entities parser for mtcute
> NOTE: The syntax implemented here is incompatible with Bot API _HTML_.
>
> Please read Syntax below for a detailed explanation
``ts
import { html } from '@mtcute/html-parser'
tg.sendText(
'me',
html
Hello, me! Updates from the feed:
${await getUpdatesFromFeed()}
`
)
@mtcute/html-parser uses htmlparser2 under the hood, so the parser
supports nearly any HTML. However, since the text is still processed in a custom way for Telegram, the supported subset
of features is documented below:
Line breaks are not preserved, is used instead,
making the syntax very close to the one used when building web pages.
Multiple spaces and indents are collapsed (except in pre), when you do need multiple spaces use instead.
Inline entities are entities that are in-line with other text. We support these entities:
| Name | Code | Result (visual) |
| ---------------- | ---------------------------------------------------------------- | ---------------------------- |
| Bold | text, text | text |text
| Italic | , text | _text_ |text
| Underline | | text |
| Strikethrough | text, text, text | ~~text~~ |
| Spoiler | (or tg-spoiler) | N/A |
| Monospace (code) | text | text |Google
| Text link | | Google |Name
| Text mention | | N/A |
| Custom emoji | (or ) | N/A |
> Note: It is up to the client to look up user's input entity by ID for text mentions.
> In most cases, you can only use IDs of users that were seen by the client while using given storage.
>
> Alternatively, you can explicitly provide access hash like this:
> Name, where abc is user's access hashtg://user?hash=abc&id=1234567
> written as a hexadecimal integer. Order of the parameters does matter, i.e.
> will not be processed as expected.
The only block entity that Telegram supports are
and, therefore it is the only tags we support too.Optionally, language for
block can be specified like this:`html`export type Foo = 42| Code | Result (visual) |
| ----------------------------------------------------------------------------------- | ---------------------------- |
|<pre>multiline\ntext</pre>|multiline|
text
|<pre language="javascript">|
export default 42
</pre>export default 42|can be "expandable", in which case clients will only render the first three lines of the blockquote,`
and the rest will only be shown when the user clicks on the blockquote.html`
This is a blockquote that will be collapsed by default.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
This text is not shown until the blockquote is expanded.unparse()Nested and overlapped entities
HTML is a nested language, and so is this parser. It does support nested entities, but overlapped entities will not work
as expected!Overlapping entities are supported in
, though.Welcome back, User!| Code | Result (visual) |
|---------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------|
|| Welcome back, _User_! |bold and italic
|| bold _and_ italicbold and italic
⚠️ word "italic" is not actually italic! |
|html
⚠️ this is howunparse()handles overlapping entities | bold _and_ _italic_ |Interpolation
Being a tagged template literal,
supports interpolation.stringYou can interpolate one of the following:
-- will not be parsed, and appended to plain text as-ishtml
- In case you want the string to be parsed, useas a simple function:numberhtml\... ${html('bold')} ...\
-- will be converted to string and appended to plain text as-isTextWithEntities
-orMessageEntity- will add the text and its entities to the output. This is the type returned byhtmlitself:`
tsbold
const bold = htmlHello, ${bold}!
const text = html`
null
- falsy value (i.e.,undefined,false`) - will be ignoredNote that because of interpolation, you almost never need to think about escaping anything,
since the values are not even parsed as HTML, and are appended to the output as-is.