### Streaming Mode Configuration Source: https://github.com/zilewang7/telegram-md-entities/blob/master/README.md Enables live-updating UI support for unclosed markdown constructs. ```ts renderMarkdown(buffer, { streaming: true }) ``` -------------------------------- ### Playground JavaScript Implementation Source: https://github.com/zilewang7/telegram-md-entities/blob/master/playground.html JavaScript logic for handling markdown rendering, message splitting, and real-time preview updates. ```javascript import { renderMarkdown, splitMessage, validateMessage, toPreviewHtml, TELEGRAM_PREVIEW_CSS, } from './dist/browser.js'; const styleTag = document.createElement('style'); styleTag.textContent = TELEGRAM_PREVIEW_CSS; document.head.appendChild(styleTag); const input = document.getElementById('input'); const output = document.getElementById('output'); const entitiesBox = document.getElementById('entities'); const issuesBox = document.getElementById('issues'); const streaming = document.getElementById('streaming'); const prefix = document.getElementById('prefix'); const prefixLabel = document.getElementById('prefixLabel'); const play = document.getElementById('play'); const rerender = () => { const full = input.value; const ratio = Number(prefix.value) / 100; const source = full.slice(0, Math.round(full.length * ratio)); prefixLabel.textContent = `${prefix.value}%`; const rendered = renderMarkdown(source, { streaming: streaming.checked }); const chunks = splitMessage(rendered); output.innerHTML = chunks .map((chunk, i) => { const label = chunks.length > 1 ? `
消息 ${i + 1}/${chunks.length} · ${chunk.text.length} chars · ${chunk.entities.length} entities
` : ''; return label + toPreviewHtml(chunk, { includeStyles: false }); }) .join(''); entitiesBox.textContent = JSON.stringify(rendered.entities, null, 2); issuesBox.textContent = JSON.stringify(validateMessage(rendered), null, 2) ; }; input.addEventListener('input', rerender); streaming.addEventListener('change', rerender); prefix.addEventListener('input', rerender); let timer = null; play.addEventListener('click', () => { if (timer) { clearInterval(timer); timer = null; play.textContent = '▶ 播放'; return; } prefix.value = '0'; streaming.checked = true; play.textContent = '⏸ 停止'; timer = setInterval(() => { const next = Math.min(100, Number(prefix.value) + 2); prefix.value = String(next); rerender(); if (next >= 100) { clearInterval(timer); timer = null; play.textContent = '▶ 播放'; } }, 120); }); rerender(); ``` -------------------------------- ### toPreviewHtml Source: https://github.com/zilewang7/telegram-md-entities/blob/master/README.md Generates an HTML representation for visual review. ```APIDOC ## toPreviewHtml(message, options?) ### Description Converts a message object into Telegram-like HTML, useful for offline visual review of how the message will appear. ``` -------------------------------- ### styleSegments Source: https://github.com/zilewang7/telegram-md-entities/blob/master/README.md Compares style runs per character. ```APIDOC ## styleSegments(message) ### Description Analyzes per-character style runs, useful as a display-equivalence comparator. ``` -------------------------------- ### Core API Methods Source: https://github.com/zilewang7/telegram-md-entities/blob/master/README.md The primary functions for rendering, splitting, validating, and converting markdown content for Telegram. ```ts renderMarkdown(markdown, options?) // → { text, entities } splitMessage(message, options?) // → chunks fitting maxLength (4096) AND maxEntities (90) validateMessage(message) // → offline Bot API rule check (offsets, nesting, budgets) toPreviewHtml(message, options?) // → Telegram-like HTML for offline visual review wrapInBlockquote(message, expandable?) // e.g. LLM "thinking" sections concatMessages(...parts) // compose with automatic entity re-offsetting entitiesToMarkdown(message) // REVERSE: {text, entities} -> markdown (this dialect) richBlocksToMarkdown(blocks, opts?) // REVERSE: Bot API 10.1+ rich message blocks -> markdown styleSegments(message) // per-char style runs: display-equivalence comparator ``` -------------------------------- ### entitiesToMarkdown Source: https://github.com/zilewang7/telegram-md-entities/blob/master/README.md Reverses the rendering process by converting entities back to markdown. ```APIDOC ## entitiesToMarkdown(message) ### Description Converts a message object (containing text and entities) back into the library's specific markdown dialect. ``` -------------------------------- ### Playground CSS Styles Source: https://github.com/zilewang7/telegram-md-entities/blob/master/playground.html CSS styles defining the layout and appearance of the playground interface. ```css body { margin: 0; font: 14px/1.5 -apple-system, "Segoe UI", sans-serif; background: #8db6d5; } .layout { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; padding: 16px; height: calc(100vh - 32px); box-sizing: border-box; } .pane { display: flex; flex-direction: column; gap: 10px; min-height: 0; } textarea { flex: 1; resize: none; padding: 10px; font: 13px/1.5 Menlo, Consolas, monospace; border-radius: 8px; border: 1px solid #6a97b8; } .output { flex: 1; overflow: auto; display: flex; flex-direction: column; gap: 12px; } .controls { display: flex; gap: 10px; align-items: center; background: #fff; border-radius: 8px; padding: 8px 12px; } .controls input[type="range"] { flex: 1; } details { background: #fff; border-radius: 8px; padding: 8px 12px; } pre.raw { font-size: 12px; overflow-x: auto; margin: 6px 0 0; } .chunk-label { font-size: 12px; color: #245; margin: 4px 0 2px; } h3 { margin: 0; color: #123; } ``` -------------------------------- ### renderMarkdown Source: https://github.com/zilewang7/telegram-md-entities/blob/master/README.md Converts markdown text into a Telegram-compatible object containing the text and its associated entities. ```APIDOC ## renderMarkdown(markdown, options?) ### Description Converts markdown input into a structure containing the plain text and the corresponding Telegram entities. Supports a streaming mode for token-streaming UIs. ### Parameters - **markdown** (string/buffer) - Required - The markdown content to render. - **options** (object) - Optional - Configuration options, including `{ streaming: boolean }`. ``` -------------------------------- ### wrapInBlockquote Source: https://github.com/zilewang7/telegram-md-entities/blob/master/README.md Wraps a message in a blockquote. ```APIDOC ## wrapInBlockquote(message, expandable?) ### Description Wraps the provided message in a blockquote. The `expandable` parameter can be used to create collapsible content patterns. ``` -------------------------------- ### richBlocksToMarkdown Source: https://github.com/zilewang7/telegram-md-entities/blob/master/README.md Converts Bot API rich message blocks to markdown. ```APIDOC ## richBlocksToMarkdown(blocks, opts?) ### Description Converts Bot API 10.1+ rich message blocks into the library's markdown dialect. ``` -------------------------------- ### Render Markdown to Telegram Entities Source: https://github.com/zilewang7/telegram-md-entities/blob/master/README.md Converts raw markdown strings into a text and entities object suitable for Telegram API methods. ```ts import { renderMarkdown, splitMessage } from 'telegram-md-entities'; const { text, entities } = renderMarkdown(llmOutput); await bot.api.sendMessage(chatId, text, { entities }); // grammy / telegraf / raw HTTP — same shape ``` -------------------------------- ### entitiesToMarkdown({ text, entities }) Source: https://github.com/zilewang7/telegram-md-entities/blob/master/README.md Converts a received Telegram message and its entities back into the package's markdown dialect. ```APIDOC ## entitiesToMarkdown({ text, entities }) ### Description Converts a received Telegram message back to markdown. It flattens overlapping or server-split entities into per-character style runs and rebuilds them as strictly nested markers. ### Parameters - **text** (string) - Required - The raw text content of the message. - **entities** (Array) - Required - The list of Telegram message entities associated with the text. ``` -------------------------------- ### richBlocksToMarkdown(blocks) Source: https://github.com/zilewang7/telegram-md-entities/blob/master/README.md Converts Bot API 10.1+ rich messages into markdown format. ```APIDOC ## richBlocksToMarkdown(blocks) ### Description Converts Bot API 10.1+ rich messages (message.rich_message.blocks) to markdown. It handles headings, tables, details, pre-formatted code, and LaTeX blocks. ### Parameters - **blocks** (RichBlock[]) - Required - The array of rich blocks from the Telegram message. ``` -------------------------------- ### concatMessages Source: https://github.com/zilewang7/telegram-md-entities/blob/master/README.md Concatenates multiple message parts. ```APIDOC ## concatMessages(...parts) ### Description Combines multiple message parts into a single message, automatically handling the re-offsetting of entities. ``` -------------------------------- ### splitMessage Source: https://github.com/zilewang7/telegram-md-entities/blob/master/README.md Splits a message into chunks that fit within Telegram's constraints. ```APIDOC ## splitMessage(message, options?) ### Description Splits a message into chunks that satisfy both the maximum length (4096 characters) and the maximum entity count (90 entities) requirements of the Telegram Bot API. ``` -------------------------------- ### validateMessage Source: https://github.com/zilewang7/telegram-md-entities/blob/master/README.md Performs offline validation of a message against Bot API rules. ```APIDOC ## validateMessage(message) ### Description Validates a message object offline to ensure it complies with Bot API rules regarding offsets, nesting, and entity budgets. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.