页:1
将 Markdown 流到 Telegram HTML 转换器 。 安全地将不完整的Markdown(从LLMs中切入)在每一步都转换成有效的Telegram HTML.
支持两种经典的Telegram HTML(mdgram)和 Bot API 10.1 丰富的 HTML 格式(mdgramRich) (中文(简体) ).
安装
npm install @borodin/mdgram
使用量
经典电报 HTML
import { mdgram } from "@borodin/mdgram";
mdgram("Hello **world**");
// "Hello <b>world</b>"
// Handles incomplete markdown (streaming)
mdgram("Hello **wor");
// "Hello <b>wor</b>"
丰富的 HTML( API 10. 1+)
import { mdgramRich } from "@borodin/mdgram";
mdgramRich("# Title\n\n- item 1\n- item 2");
// "<h1>Title</h1>\n<ul>\n<li>item 1</li>\n<li>item 2</li>\n</ul>"
// LaTeX
mdgramRich("Euler's formula: $e^{i\\pi} + 1 = 0$");
// "Euler's formula: <tg-math>e^{i\pi} + 1 = 0</tg-math>"
// Streaming — every prefix is valid Rich HTML
mdgramRich("$e^{i\\pi");
// "<tg-math>e^{i\pi}</tg-math>"
使用 sendRichMessage 编号 : sendRichMessageDraft 发送 Rich HTML 到 Telegram.
paragraphMode: "br" - 为没有工作的客户工作 <p> 边距
某些Telegram 客户端正在提供 <p> 没有垂直边距, 因此相邻
段落可视合并。 在客户端解决之前,你可以选择一个
<br><br>- 在所有客户端看起来相同的基于布局 :
mdgramRich("First.\n\nSecond.", { paragraphMode: "br" });
// "First.<br><br>Second." (default "p" mode: "<p>First.</p>\n<p>Second.</p>")
<br><br> 仅在相邻段落之间插入;带块的边界
元素(标题、列表、表格等)保持不变。 默认 "p" 模式
遵循 Rich HTML 规格.
支持的格式
mdgram – 经典的Telegram HTML
| 标记 | 产出 |
|---|---|
**bold** | <b>bold</b> |
*italic* | <i>italic</i> |
`code` | <code>code</code> |
```lang\n...\n``` | <pre><code class="language-lang">...</code></pre> |
[text](url) | <a href="url">text</a> |
~~strike~~ | <s>strike</s> |
||spoiler|| | <tg-spoiler>spoiler</tg-spoiler> |
> quote | <blockquote>quote</blockquote> |
# heading | <b>heading</b> |
- item 编号 : 1. item | • item 编号 : 1. item |
mdgramRich — 丰富的 HTML( 超集)
一切从 mdgram,加上:
| 标记 | 产出 |
|---|---|
# H1 . ###### H6 | <h1> . <h6> |
- item 编号 : 1. item | <ul><li> 编号 : <ol><li> |
- [x] task | <li><input type="checkbox" checked> |
| table | | <table><tr><th/td> |
==highlight== | <mark>highlight</mark> |
$formula$ | <tg-math>formula</tg-math> |
$$formula$$ | <tg-math-block>formula</tg-math-block> |
```math\nformula\n``` | <tg-math-block>formula</tg-math-block> |
\(formula\) | <tg-math>formula</tg-math> |
\[formula\] | <tg-math-block>formula</tg-math-block> |
--- | <hr/> |
 | <img src="url"/> |
 | <figure><img src="url"/><figcaption>caption</figcaption></figure> |
原始内容 <tg-spoiler>, (中文(简体) ), <tg-emoji>, (中文(简体) ), <tg-thinking>, (中文(简体) ), <u>, (中文(简体) ), <ins>, (中文(简体) ), <sub>, (中文(简体) ), <sup>, (中文(简体) ), <cite>, (中文(简体) ), <details>, (中文(简体) ), <aside>, (中文(简体) ), <footer> | 通过逐字记录 |
货币金额 $100 从未被误认为"LaTeX".
截断
将信件文本限制为 4096 (英语) 和标题 第1024号 UTF-16 代码单元. 丰富的信件支持最多 32768 (英语) 字符键.
import { mdgram, mdgramRich, MESSAGE_LIMIT, CAPTION_LIMIT, RICH_MESSAGE_LIMIT } from "@borodin/mdgram";
mdgram(longMarkdown, { maxLength: MESSAGE_LIMIT });
mdgram(longMarkdown, { maxLength: CAPTION_LIMIT });
mdgramRich(longMarkdown, { maxLength: RICH_MESSAGE_LIMIT });
切出时,输出结束为 … 并且所有打开的标签都正确关闭.
LLM 系统提示
对于 mdgram数字 :
Format responses using Markdown:
- **bold**, *italic*, ~~strikethrough~~
- `inline code` and fenced code blocks (```)
- [links](url), ||spoilers||, > blockquotes
- Lists: - item, 1. item
Do NOT use images, HTML tags, or LaTeX.
对于 mdgramRich数字 :
Format responses using Markdown:
- **bold**, *italic*, ~~strikethrough~~, ==highlight==
- `inline code`, fenced code blocks (```), math blocks (```math)
- $inline LaTeX$, $$block LaTeX$$
- [links](url), ||spoilers||, > blockquotes
- Headings: # H1 through ###### H6
- Lists: - item, 1. item, - [x] task list
- Tables: | col | col |
- Horizontal rule: ---
- Images:  or 
Do NOT use raw HTML tags.
如何运作
- 调试 自动关闭不完整的Markdown令牌(粗体、斜体、代码、破坏器、LaTeX等)
- 标记 用自定义的Telegram渲染器解析已完成的Markdown
- 输出在每个步骤上都是有效的 Telegram HTML —— 可以直接流到
editMessageText或者说sendRichMessageDraft
许可证
麻省理工学院