Skip to content

Commit a072f1d

Browse files
T0haclaude
andcommitted
fix: address PR review — fix chunk ordering, extract lang from info string
- Fix chunk_blocks empty-acc clause to pass hard_split result as initial accumulator instead of concatenating reversed lists - Extract only first word from code block info string as language (handles "python title=..." style info strings) - Pin formatted HTML in webhook handler test instead of raw text - Add tests for both fixes Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 13975d3 commit a072f1d

3 files changed

Lines changed: 29 additions & 4 deletions

File tree

lib/bodhi/telegram/formatter.ex

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,13 @@ defmodule Bodhi.Telegram.Formatter do
8585
end
8686

8787
defp render_node(%MDEx.CodeBlock{info: info, literal: text}) do
88-
if info != "" do
89-
"<pre><code class=\"language-#{escape(info)}\">" <>
88+
lang =
89+
info
90+
|> String.split(" ", parts: 2)
91+
|> List.first("")
92+
93+
if lang != "" do
94+
"<pre><code class=\"language-#{escape(lang)}\">" <>
9095
escape(text) <> "</code></pre>"
9196
else
9297
"<pre><code>" <> escape(text) <> "</code></pre>"
@@ -183,7 +188,7 @@ defmodule Bodhi.Telegram.Formatter do
183188
if String.length(block) <= @max_length do
184189
chunk_blocks(rest, [block])
185190
else
186-
hard_split(block) ++ chunk_blocks(rest, [])
191+
chunk_blocks(rest, hard_split(block))
187192
end
188193
end
189194

test/bodhi/telegram/formatter_test.exs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ defmodule Bodhi.Telegram.FormatterTest do
4242
Formatter.format(input)
4343
end
4444

45+
test "code block info string uses only first word as language" do
46+
input = "```python title=\"example.py\"\nprint(1)\n```"
47+
48+
{html, _} = Formatter.format(input)
49+
assert html =~ "language-python"
50+
refute html =~ "title="
51+
end
52+
4553
test "link" do
4654
assert {"<a href=\"http://ex.com\">text</a>", _} =
4755
Formatter.format("[text](http://ex.com)")
@@ -185,6 +193,17 @@ defmodule Bodhi.Telegram.FormatterTest do
185193
assert Enum.join(chunks, "") == line
186194
end
187195

196+
test "first block hard-split preserves order with subsequent blocks" do
197+
# First block > 4096 chars, followed by a normal block
198+
big = String.duplicate("a", 3000) <> "\n" <> String.duplicate("b", 3000)
199+
small = "tail"
200+
text = big <> "\n\n" <> small
201+
chunks = Formatter.split(text)
202+
203+
assert List.last(chunks) =~ "tail"
204+
assert Enum.all?(chunks, &(String.length(&1) <= 4096))
205+
end
206+
188207
test "empty text returns single empty chunk" do
189208
assert Formatter.split("") == [""]
190209
end

test/bodhi/tg_webhook_handler_test.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,11 @@ defmodule Bodhi.TgWebhookHandlerTest do
113113
describe "send_message/2" do
114114
test "Sends and saves message correctly", %{chat: chat, bot_user: bot_user} do
115115
text = Faker.Lorem.paragraph()
116+
{expected_html, _} = Bodhi.Telegram.Formatter.format(text)
116117
chat_id = chat.id
117118

118119
# Expect send_message to be called once with these exact arguments
119-
expect(Bodhi.TelegramMock, :send_message, fn ^chat_id, ^text, _opts ->
120+
expect(Bodhi.TelegramMock, :send_message, fn ^chat_id, ^expected_html, _opts ->
120121
{:ok,
121122
%Telegex.Type.Message{
122123
from: %Telegex.Type.User{

0 commit comments

Comments
 (0)