Markdown entities parser for mtcute
npm install @mtcute/markdown-parserš API Reference
Markdown entities parser for mtcute
> NOTE: The syntax implemented here is not compatible with Bot API _Markdown_, nor _MarkdownV2_.
>
> Please read Syntax below for a detailed explanation
``typescript
import { md } from '@mtcute/markdown-parser'
tg.sendText(
'me',
md
Hello, me! Updates from the feed:
${await getUpdatesFromFeed()}
`
)
Inline entities are defined by some _tag_ surrounding some text, and processing them simply strips their tag.
Supported entities:
- Bold, tag is **__
- Italic, tag is --
- Underline, tag is (_NON-STANDARD_)~~
- Strikethrough, tag is ||
- Spoiler, tag is (_NON-STANDARD_)
- Code (monospaced font), tag is
- Note that escaping text works differently inside code, see below.
> Unlike CommonMark, we use the symbol itself and not its count.
> Thus, using * (asterisk) will always produce bold,
> and using _ (underscore) will always produce italic.
>
> This eliminates a lot of confusion, like: _bold_ ā _bold_, italic ā italic
| Code | Result (visual) | Result (as HTML) |
|----------------------------------------------|-------------------|------------------------------|
| bold | bold | bold |
| __italic__ | __italic__ | italic |
| --underline-- | underline | underline |
| ~~strikethrough~~ | ~~strikethrough~~ | |strikethrough
| ||spoiler|| | N/A | |
| whatever | \whatever\ | whatever |
| _whatever_ | \_whatever\_ | _whatever_ |
| \ | hello world\hello world | |hello world
| \ | __text__\__text__ | |__text__
Pre represents a single block of code, optionally with a language.
This entity starts with \ (triple backtick), optionally preceded with a line break.\\ (triple backtick), optionally followed with language name and a must be
followed with a line break, and ends with \\\
| Code | Result (visual) | Result (as HTML) |
|--------------------------------------------------------------------|---------------------------|---------------------------------------------------------------------------------------------|
|
\\\
hello
\\\ | hello | hello
|\\\
hello\\\ | hello | hello
|\\\javascript
const a =
\\\ | const a = ` | <pre language="javascript">
const a =
</pre>
|$3
Blockquote is a block of text formatted as a quote.
Every line in a blockquote is prefixed with
> character. Due to limitations of markdown,
it is not possible to define expandable blockquotes, prefer using HTML instead.`
> This is a blockquote.
> Some more text.
> And some more.
And this is no longer a blockquote.
`$3
Links are parsed exactly the same as standard markdown (except references are not supported).
Defined like this:
Link text.- Link text may also contain any formatting, but link cannot contain other links inside (obviously).
-
(opening square bracket) inside link text will be treated like a normal character.A markdown-style link can also be used to define a name mention like this:
[Name,
where 1234567 is the ID of the user you want to mention.Additionally, a markdown-style link can be used to define a custom emoji like this:
š, where 123456 is ID of the emoji.> Note: It is up to the client to look up user's input entity by ID.
> 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:
, is user's access hash written as a base-16 unsigned integer.
> Order of the parameters does matter, i.e. tg://user?hash=abc&id=1234567 will not be processed as expected.| Code | Result (visual" class="text-primary hover:underline" target="_blank" rel="noopener noreferrer">Name
| Result (as HTML) | | Google | Google |
| __Google__ | _Google_ | Google |
| [empty link]() | empty link | empty link |
| [empty link] | [empty link] | [empty link] |
| User | N/A | N/A |
| š | N/A | N/A |$3
Quite a powerful feature of this parser is the ability to process overlapping entities. Only inline entities (except
code) can be overlapped.
Since inline entities are only defined by their tag, and nesting same entities doesn't make sense, you can think of the
tags just as start/end markers, and not in terms of nesting.
| Code | Result (visual) | Result (as HTML) |
|-------------------------------|---------------------------|----------------------------------------|
|
Welcome back, __User__! | Welcome back, _User_! | Welcome back, User! |
| bold __and italic__ | bold _and_ _italic_ | bold and italic |Interpolation
Being a tagged template literal,
md supports interpolation.You can interpolate one of the following:
-
string - will not be parsed, and appended to plain text as-is
- In case you want the string to be parsed, use md as a simple function: md\... ${md('bold')} ...\
- number - will be converted to string and appended to plain text as-is
- TextWithEntities or MessageEntity - will add the text and its entities to the output. This is the type returned by md itself:
`ts
const bold = mdbold
const text = mdHello, ${bold}!
`
- falsy value (i.e. null, undefined, false`) - will be ignoredBecause of interpolation, you almost never need to think about escaping anything,
since the values are not even parsed as Markdown, and are appended to the output as-is.