Repository navigation
Keep paragraph spacing, headings, lists and tables in IM text #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: stack/installed-0.3.2
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,14 @@ | ||
| // Outbound text for IM platforms. Bots write Markdown; each platform adapter turns it | ||
| // into what the platform renders (Telegram HTML) or plain text, and cuts it at the | ||
| // platform's per-message limit without breaking inside a code fence when it can help it. | ||
| // | ||
| // 2026-11-10: paragraph spacing, headings, lists and tables. | ||
| // The previous version dropped every blank line and rejoined blocks with a single | ||
| // newline, so paragraphs landed tight against each other and Steve got "big blobs of | ||
| // text with no spacing". Headings, list markers, table pipes and rules had no rule at | ||
| // all, so they arrived as literal `##`, `-` and `|`. Blocks are now rejoined with a | ||
| // blank line, headings become bold, list items are double spaced, and a Markdown table | ||
| // becomes an aligned monospace block. | ||
|
|
||
| const escapeHtml = text => String(text).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>') | ||
|
|
||
|
|
@@ -33,6 +41,98 @@ function inlinePlain(line) { | |
| .replace(/~~([^~]+)~~/g, '$1') | ||
| } | ||
|
|
||
| const HEADING_RE = /^\s{0,3}#{1,6}\s+(.*?)\s*#*\s*$/ | ||
| const LIST_RE = /^\s{0,3}(?:[-*+]|\d{1,3}[.)])\s+/ | ||
| const RULE_RE = /^\s{0,3}(?:-{3,}|\*{3,}|_{3,})\s*$/ | ||
| const TABLE_ROW_RE = /^\s*\|.*\|\s*$/ | ||
| const TABLE_SEP_RE = /^\s*\|(?:\s*:?-{2,}:?\s*\|)+\s*$/ | ||
|
|
||
| const isListItem = line => LIST_RE.test(line) | ||
| const isRule = line => RULE_RE.test(line) | ||
| const isTableRow = line => TABLE_ROW_RE.test(line) | ||
| const isTableSep = line => TABLE_SEP_RE.test(line) | ||
|
|
||
| // A table row as plain cell strings. The leading and trailing pipe are framing, not cells. | ||
| function tableCells(line) { | ||
| const trimmed = line.trim().replace(/^\|/, '').replace(/\|$/, '') | ||
| return trimmed.split('|').map(cell => cell.trim()) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a Markdown table contains an escaped pipe, splitting on every Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| // Aligned monospace grid. Telegram has no tables, so this is the only shape that keeps | ||
| // columns readable; it is wrapped in <pre> so the padding survives. | ||
| function tableRows(lines) { | ||
| return lines.filter(line => !isTableSep(line)).map(tableCells) | ||
| } | ||
|
|
||
| function padRows(rows, toPlainFn) { | ||
| const plain = rows.map(cells => cells.map(cell => toPlainFn(cell))) | ||
| const width = [] | ||
| for (const cells of plain) { | ||
| cells.forEach((cell, index) => { | ||
| width[index] = Math.max(width[index] ?? 0, Array.from(cell).length) | ||
| }) | ||
| } | ||
| return plain.map(cells => cells | ||
| .map((cell, index) => { | ||
| const pad = width[index] - Array.from(cell).length | ||
| return cell + ' '.repeat(Math.max(0, pad)) | ||
| }) | ||
| .join(' ') | ||
| .replace(/\s+$/, '')) | ||
| } | ||
|
|
||
| // One text block, in order, with list items separated by a blank line. | ||
| function renderLines(lines, inline, wrapHeading) { | ||
| const out = [] | ||
| let previousWasListItem = false | ||
| for (const line of lines) { | ||
| const heading = HEADING_RE.exec(line) | ||
| if (heading !== null) { | ||
| out.push(wrapHeading(inline(heading[1]))) | ||
| previousWasListItem = false | ||
| continue | ||
| } | ||
| if (isRule(line)) { | ||
| // Same wrapper as a heading, so the plain renderer never sees HTML tags. | ||
| out.push(wrapHeading('-'.repeat(12))) | ||
| previousWasListItem = false | ||
| continue | ||
| } | ||
| const item = isListItem(line) | ||
| if (item && previousWasListItem) out.push('') | ||
| out.push(inline(line)) | ||
| previousWasListItem = item | ||
| } | ||
| return out.join('\n') | ||
| } | ||
|
|
||
| function renderHtmlLines(lines) { | ||
| return renderLines(lines, inlineHtml, inner => `<b>${inner}</b>`) | ||
| } | ||
|
|
||
| function renderPlainLines(lines) { | ||
| return renderLines(lines, inlinePlain, inner => inner) | ||
| } | ||
|
|
||
| // Split a block's lines into table runs and ordinary runs, so a table can become <pre> | ||
| // without dragging the paragraphs beside it into monospace. | ||
| function splitRuns(lines) { | ||
| const runs = [] | ||
| let at = 0 | ||
| while (at < lines.length) { | ||
| if (isTableRow(lines[at])) { | ||
| const start = at | ||
| while (at < lines.length && isTableRow(lines[at])) at += 1 | ||
| runs.push({ kind: 'table', lines: lines.slice(start, at) }) | ||
| continue | ||
| } | ||
| const start = at | ||
| while (at < lines.length && !isTableRow(lines[at])) at += 1 | ||
| runs.push({ kind: 'lines', lines: lines.slice(start, at) }) | ||
| } | ||
| return runs | ||
| } | ||
|
|
||
| // One Markdown document as blocks: fenced code stays a block of its own so chunking | ||
| // can keep it whole; the rest splits on blank lines. | ||
| function blocks(markdown) { | ||
|
|
@@ -63,18 +163,36 @@ function blocks(markdown) { | |
| return out | ||
| } | ||
|
|
||
| const renderHtml = block => (block.kind === 'code' | ||
| ? `<pre><code${block.lang ? ` class="language-${escapeHtml(block.lang)}"` : ''}>${escapeHtml(block.lines.join('\n'))}</code></pre>` | ||
| : block.lines.map(inlineHtml).join('\n')) | ||
| const renderHtml = block => { | ||
| if (block.kind === 'code') { | ||
| return `<pre><code${block.lang ? ` class="language-${escapeHtml(block.lang)}"` : ''}>${escapeHtml(block.lines.join('\n'))}</code></pre>` | ||
| } | ||
| return splitRuns(block.lines) | ||
| .map(run => (run.kind === 'table' | ||
| ? `<pre>${escapeHtml(padRows(tableRows(run.lines), inlinePlain).join('\n'))}</pre>` | ||
| : renderHtmlLines(run.lines))) | ||
|
Comment on lines
+170
to
+173
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For a table whose rendered block exceeds Telegram's 4096-character limit, Useful? React with 👍 / 👎. |
||
| .join('\n\n') | ||
| } | ||
|
|
||
| const renderPlain = block => { | ||
| if (block.kind === 'code') return block.lines.join('\n') | ||
| return splitRuns(block.lines) | ||
| .map(run => (run.kind === 'table' | ||
| ? padRows(tableRows(run.lines), inlinePlain).join('\n') | ||
| : renderPlainLines(run.lines))) | ||
| .join('\n\n') | ||
| } | ||
|
|
||
| const renderPlain = block => (block.kind === 'code' ? block.lines.join('\n') : block.lines.map(inlinePlain).join('\n')) | ||
| // Blocks are one paragraph each, so they are rejoined with a BLANK line. This is the | ||
| // line that stops paragraphs running together. | ||
| const BLOCK_JOIN = '\n\n' | ||
|
|
||
| export function toTelegramHtml(markdown) { | ||
| return blocks(markdown).map(renderHtml).join('\n') | ||
| return blocks(markdown).map(renderHtml).join(BLOCK_JOIN) | ||
| } | ||
|
|
||
| export function toPlain(markdown) { | ||
| return blocks(markdown).map(renderPlain).join('\n') | ||
| return blocks(markdown).map(renderPlain).join(BLOCK_JOIN) | ||
| } | ||
|
|
||
| const points = text => Array.from(text).length | ||
|
|
@@ -171,10 +289,10 @@ export function chunkTelegram(markdown, limit = 4096) { | |
| for (const block of blocks(markdown)) { | ||
| const blockHtml = renderHtml(block) | ||
| const blockText = renderPlain(block) | ||
| const candidate = html === '' ? blockHtml : `${html}\n${blockHtml}` | ||
| const candidate = html === '' ? blockHtml : `${html}${BLOCK_JOIN}${blockHtml}` | ||
| if (points(candidate) <= limit) { | ||
| html = candidate | ||
| text = text === '' ? blockText : `${text}\n${blockText}` | ||
| text = text === '' ? blockText : `${text}${BLOCK_JOIN}${blockText}` | ||
| continue | ||
| } | ||
| flush() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For a heading such as
## C#, the new regex consumes the final#as an optional closing marker, so both renderers change the content toC(toTelegramHtml('## C#')returns<b>C</b>). Closing heading markers should require preceding whitespace; otherwise legitimate names and other text ending in hashes are silently altered.Useful? React with 👍 / 👎.