fix(signal): outbound formatting and markdown IR rendering improvements (#9781)

* fix: Signal and markdown formatting improvements

Markdown IR fixes:
- Fix list-paragraph spacing (extra newline between list items and following paragraphs)
- Fix nested list indentation and newline handling
- Fix blockquote_close emitting redundant newline (inner content handles spacing)
- Render horizontal rules as visible ─── separator instead of silent drop
- Strip inner cell styles in code-mode tables to prevent overlapping with code_block span

Signal formatting fixes:
- Normalize URLs for dedup comparison (strip protocol, www., trailing slash)
- Render headings as bold text (headingStyle: 'bold')
- Add '> ' prefix to blockquotes for visual distinction
- Re-chunk after link expansion to respect chunk size limits

Tests:
- 51 new tests for markdown IR (spacing, lists, blockquotes, tables, HR)
- 18 new tests for Signal formatting (URL dedup, headings, blockquotes, HR, chunking)
- Update Slack nested list test expectation to match corrected IR output

* refactor: style-aware Signal text chunker

Replace indexOf-based chunk position tracking with deterministic
cursor tracking. The new splitSignalFormattedText:

- Splits at whitespace/newline boundaries within the limit
- Avoids breaking inside parentheses (preserves expanded link URLs)
- Slices style ranges at chunk boundaries with correct local offsets
- Tracks position via offset arithmetic instead of fragile indexOf

Removes dependency on chunkText from auto-reply/chunk.

Tests: 19 new tests covering style preservation across chunk boundaries,
edge cases (empty text, under limit, exact split points), and integration
with link expansion.

* fix: correct Signal style offsets with multiple link expansions

applyInsertionsToStyles() was using original coordinates for each
insertion without tracking cumulative shift from prior insertions.
This caused bold/italic/etc styles to drift to wrong text positions
when multiple markdown links expanded in a single message.

Added cumulative shift tracking and a regression test.

* test: clean up test noise and fix ineffective assertions

- Remove console.log from ir.list-spacing and ir.hr-spacing tests
- Fix ir.nested-lists.test.ts: remove ineffective regex assertion
- Fix ir.hr-spacing.test.ts: add actual assertions to edge case test

* refactor: split Signal formatting tests (#9781) (thanks @heyhudson)

---------

Co-authored-by: Hudson <258693705+hudson-rivera@users.noreply.github.com>
Co-authored-by: Peter Steinberger <steipete@gmail.com>
This commit is contained in:
Hudson
2026-02-14 10:57:20 -05:00
committed by GitHub
parent 226bf74634
commit 1d6abddb9f
12 changed files with 1530 additions and 24 deletions

View File

@@ -364,6 +364,14 @@ function appendCell(state: RenderState, cell: TableCell) {
}
}
function appendCellTextOnly(state: RenderState, cell: TableCell) {
if (!cell.text) {
return;
}
state.text += cell.text;
// Do not append styles - this is used for code blocks where inner styles would overlap
}
function renderTableAsBullets(state: RenderState) {
if (!state.table) {
return;
@@ -474,7 +482,8 @@ function renderTableAsCode(state: RenderState) {
state.text += " ";
const cell = cells[i];
if (cell) {
appendCell(state, cell);
// Use text-only append to avoid overlapping styles with code_block
appendCellTextOnly(state, cell);
}
const pad = widths[i] - (cell?.text.length ?? 0);
if (pad > 0) {
@@ -589,27 +598,43 @@ function renderTokens(tokens: MarkdownToken[], state: RenderState): void {
break;
case "blockquote_close":
closeStyle(state, "blockquote");
state.text += "\n";
break;
case "bullet_list_open":
// Add newline before nested list starts (so nested items appear on new line)
if (state.env.listStack.length > 0) {
state.text += "\n";
}
state.env.listStack.push({ type: "bullet", index: 0 });
break;
case "bullet_list_close":
state.env.listStack.pop();
if (state.env.listStack.length === 0) {
state.text += "\n";
}
break;
case "ordered_list_open": {
// Add newline before nested list starts (so nested items appear on new line)
if (state.env.listStack.length > 0) {
state.text += "\n";
}
const start = Number(getAttr(token, "start") ?? "1");
state.env.listStack.push({ type: "ordered", index: start - 1 });
break;
}
case "ordered_list_close":
state.env.listStack.pop();
if (state.env.listStack.length === 0) {
state.text += "\n";
}
break;
case "list_item_open":
appendListPrefix(state);
break;
case "list_item_close":
state.text += "\n";
// Avoid double newlines (nested list's last item already added newline)
if (!state.text.endsWith("\n")) {
state.text += "\n";
}
break;
case "code_block":
case "fence":
@@ -680,7 +705,8 @@ function renderTokens(tokens: MarkdownToken[], state: RenderState): void {
break;
case "hr":
state.text += "\n";
// Render as a visual separator
state.text += "───\n\n";
break;
default:
if (token.children) {
@@ -744,7 +770,13 @@ function mergeStyleSpans(spans: MarkdownStyleSpan[]): MarkdownStyleSpan[] {
const merged: MarkdownStyleSpan[] = [];
for (const span of sorted) {
const prev = merged[merged.length - 1];
if (prev && prev.style === span.style && span.start <= prev.end) {
if (
prev &&
prev.style === span.style &&
// Blockquotes are container blocks. Adjacent blockquote spans should not merge or
// consecutive blockquotes can "style bleed" across the paragraph boundary.
(span.start < prev.end || (span.start === prev.end && span.style !== "blockquote"))
) {
prev.end = Math.max(prev.end, span.end);
continue;
}