fix(telegram): stop adjacent rich blocks from rendering glued together

Two bugs behind the "System statusEverything is operating normally."
render:

1. Consecutive plain lines with no blank line between them were joined
   into one paragraph's text with an embedded "\n". Telegram's
   rich-block paragraph does not render an interior "\n" as a break --
   it renders nothing, so the words ran together with zero separator.
   Join with a space instead: the correct rendering of a CommonMark
   soft line break anyway, and it survives regardless of how the
   client treats a literal "\n".

2. Telegram's renderer adds no vertical gap between adjacent blocks on
   its own -- only a heading carries its own margin -- so a label line
   immediately followed by a list (or any two non-heading blocks back
   to back) rendered touching, with the list's first item glued to the
   preceding text and no visible line break at all. appendBlock now
   inserts an empty spacer paragraph before any block that doesn't
   follow a heading, making the gap explicit instead of depending on
   client-side spacing that isn't there.

Reproduced directly against markdownToBlocks with the reported input
and confirmed the fix before writing this up.
This commit is contained in:
sam committed 2026-09-02 01:12:53 +10:00
1 parent b05e8f189f
commit b7c05d0a07
2 files changed
+63 -27

No files matched your search

+28 -7
View File
@@ -168,7 +168,7 @@ func (p *markdownBlockParser) handleLine(line string) {
p.codeLines = nil
case headingDepth(trimmed) > 0:
p.flush()
p.blocks = append(p.blocks, headingBlock(trimmed))
p.appendBlock(headingBlock(trimmed))
case isListItem(trimmed):
p.flushParagraph()
p.flushQuote()
@@ -183,11 +183,17 @@ func (p *markdownBlockParser) handleLine(line string) {
case trimmed == "":
p.flush()
default:
// A normal line ends a list or quote block and continues a paragraph.
// A normal line ends a list or quote block and continues a
// paragraph. Joined with a space, not "\n": this is a CommonMark
// soft line break, and Telegram's rich-block paragraph text does
// not render an embedded "\n" as a break at all -- it renders
// nothing, jamming the two lines together with no separator
// whatsoever. A space is the correct soft-break rendering anyway
// and survives regardless of how the client treats a literal "\n".
p.flushList()
p.flushQuote()
if p.paragraph.Len() > 0 {
p.paragraph.WriteString("\n")
p.paragraph.WriteString(" ")
}
p.paragraph.WriteString(line)
}
@@ -201,12 +207,27 @@ func (p *markdownBlockParser) flush() {
p.flushParagraph()
}
// appendBlock adds block to the output. Telegram's rich-block renderer adds
// no vertical gap between adjacent blocks on its own -- only a heading
// carries its own margin -- so two blocks from source lines with no blank
// line between them (an intro line immediately followed by a list, two
// back-to-back paragraphs) render glued together with no visible break at
// all. An empty paragraph spacer before any non-heading-following block
// makes that gap explicit instead of relying on client-side spacing that
// doesn't exist.
func (p *markdownBlockParser) appendBlock(block models.InputRichBlock) {
if len(p.blocks) > 0 && p.blocks[len(p.blocks)-1].Type != models.RichBlockTypeSectionHeading {
p.blocks = append(p.blocks, paragraphBlock(""))
}
p.blocks = append(p.blocks, block)
}
func (p *markdownBlockParser) flushParagraph() {
if p.paragraph.Len() == 0 {
return
}
if text := strings.TrimSpace(p.paragraph.String()); text != "" {
p.blocks = append(p.blocks, paragraphBlock(stripInlineMarkdown(text)))
p.appendBlock(paragraphBlock(stripInlineMarkdown(text)))
}
p.paragraph.Reset()
}
@@ -219,7 +240,7 @@ func (p *markdownBlockParser) flushList() {
for _, item := range p.listItems {
items = append(items, models.InputRichBlockListItem{Blocks: item})
}
p.blocks = append(p.blocks, models.InputRichBlock{
p.appendBlock(models.InputRichBlock{
Type: models.RichBlockTypeList,
InputRichBlockList: &models.InputRichBlockList{
Type: models.RichBlockTypeList,
@@ -242,7 +263,7 @@ func (p *markdownBlockParser) flushQuote() {
inner = append(inner, paragraphBlock(stripInlineMarkdown(body)))
}
if len(inner) > 0 {
p.blocks = append(p.blocks, models.InputRichBlock{
p.appendBlock(models.InputRichBlock{
Type: models.RichBlockTypeBlockQuotation,
InputRichBlockBlockQuotation: &models.InputRichBlockBlockQuotation{
Type: models.RichBlockTypeBlockQuotation,
@@ -255,7 +276,7 @@ func (p *markdownBlockParser) flushQuote() {
func (p *markdownBlockParser) closeCode() {
if len(p.codeLines) > 0 || p.codeLang != "" {
p.blocks = append(p.blocks, preformattedBlock(stripInlineMarkdown(strings.Join(p.codeLines, "\n")), p.codeLang))
p.appendBlock(preformattedBlock(stripInlineMarkdown(strings.Join(p.codeLines, "\n")), p.codeLang))
}
p.codeLines, p.codeLang, p.inCode = nil, "", false
}
+35 -20
View File
@@ -27,11 +27,14 @@ func blockText(b models.InputRichBlock) string {
}
func TestMarkdownToBlocksHeadings(t *testing.T) {
// A spacer paragraph precedes the heading: Telegram adds no gap between
// adjacent blocks on its own, so the parser inserts one explicitly
// before every non-heading-following block. See appendBlock.
blocks := markdownToBlocks("Intro prose.\n### Danger\nMore prose.")
if len(blocks) != 3 {
t.Fatalf("blocks = %d, want 3 (%v)", len(blocks), blockTypes(blocks))
if len(blocks) != 4 {
t.Fatalf("blocks = %d, want 4 (%v)", len(blocks), blockTypes(blocks))
}
heading := blocks[1]
heading := blocks[2]
if heading.Type != models.RichBlockTypeSectionHeading {
t.Fatalf("second block type = %q, want heading", heading.Type)
}
@@ -66,12 +69,14 @@ func TestMarkdownToBlocksHeadingsMapDepthToSize(t *testing.T) {
}
func TestMarkdownToBlocksFencedCode(t *testing.T) {
// Non-heading blocks get an explicit spacer paragraph on both sides; see
// appendBlock.
in := "before\n```go\nfunc main() {}\n```\nafter"
blocks := markdownToBlocks(in)
if len(blocks) != 3 {
t.Fatalf("blocks = %d, want 3 (%v)", len(blocks), blockTypes(blocks))
if len(blocks) != 5 {
t.Fatalf("blocks = %d, want 5 (%v)", len(blocks), blockTypes(blocks))
}
pre := blocks[1]
pre := blocks[2]
if pre.Type != models.RichBlockTypePreformatted {
t.Fatalf("second block type = %q, want pre", pre.Type)
}
@@ -84,12 +89,14 @@ func TestMarkdownToBlocksFencedCode(t *testing.T) {
}
func TestMarkdownToBlocksBulletList(t *testing.T) {
// Non-heading blocks get an explicit spacer paragraph on both sides; see
// appendBlock.
in := "Text before\n- one\n- two\nText after"
blocks := markdownToBlocks(in)
if len(blocks) != 3 {
t.Fatalf("blocks = %d, want 3 (%v)", len(blocks), blockTypes(blocks))
if len(blocks) != 5 {
t.Fatalf("blocks = %d, want 5 (%v)", len(blocks), blockTypes(blocks))
}
list := blocks[1]
list := blocks[2]
if list.Type != models.RichBlockTypeList {
t.Fatalf("second block type = %q, want list", list.Type)
}
@@ -103,12 +110,14 @@ func TestMarkdownToBlocksBulletList(t *testing.T) {
}
func TestMarkdownToBlocksBlockQuotation(t *testing.T) {
// Non-heading blocks get an explicit spacer paragraph on both sides; see
// appendBlock.
in := "prose\n> important note\nafter"
blocks := markdownToBlocks(in)
if len(blocks) != 3 {
t.Fatalf("blocks = %d, want 3 (%v)", len(blocks), blockTypes(blocks))
if len(blocks) != 5 {
t.Fatalf("blocks = %d, want 5 (%v)", len(blocks), blockTypes(blocks))
}
quote := blocks[1]
quote := blocks[2]
if quote.Type != models.RichBlockTypeBlockQuotation {
t.Fatalf("second block type = %q, want blockquote", quote.Type)
}
@@ -138,18 +147,20 @@ func TestMarkdownToBlocksStripsInlineMarkers(t *testing.T) {
func TestMarkdownToBlocksToolBlockBecomesParagraphAndCode(t *testing.T) {
// The tool progress block composes a label line then a ```text fence.
// Non-heading blocks get an explicit spacer paragraph on both sides; see
// appendBlock.
in := "🔧 shell — done\n```text\nexit 0\n```\ndone"
blocks := markdownToBlocks(in)
if len(blocks) != 3 {
t.Fatalf("blocks = %d, want 3 (%v)", len(blocks), blockTypes(blocks))
if len(blocks) != 5 {
t.Fatalf("blocks = %d, want 5 (%v)", len(blocks), blockTypes(blocks))
}
if got := blockText(blocks[0]); got != "🔧 shell — done" {
t.Errorf("label block = %q", got)
}
if blocks[1].Type != models.RichBlockTypePreformatted {
t.Fatalf("second block type = %q, want pre", blocks[1].Type)
if blocks[2].Type != models.RichBlockTypePreformatted {
t.Fatalf("third block type = %q, want pre", blocks[2].Type)
}
if got := blockText(blocks[1]); got != "exit 0" {
if got := blockText(blocks[2]); got != "exit 0" {
t.Errorf("code = %q, want exit 0", got)
}
}
@@ -227,9 +238,13 @@ func TestSplitBlocksPreservesOrderAndContent(t *testing.T) {
if len(all) != len(blocks) {
t.Fatalf("total blocks %d, want %d", len(all), len(blocks))
}
texts := make([]string, len(all))
for i, b := range all {
texts[i] = blockText(b)
// Spacer paragraphs (see appendBlock) are empty and intentionally
// dropped here, same as blocksToPlainText does for the real send path.
var texts []string
for _, b := range all {
if text := blockText(b); text != "" {
texts = append(texts, text)
}
}
got := strings.Join(texts, " ")
want := "para one para two para three"