Skip to content

Commit 57012bb

Browse files
committed
fix(zalouser): normalize CRLF + include + list markers in collapse rules
Two polish findings from the third upstream review pass on #85039: P2 (confidence 0.88) - normalizeZalouserOutboundText split only on \n, so Windows-style agent output (`---\r\n`, `- a\r\n\r\n- b`) kept a trailing \r on each line. The $-anchored, [ \t]-only HR / list / fence matchers then failed to fire and the broken spacing / literal HR reached Zalo. Fix: normalize \r\n? -> \n up front, mirroring the markdown style parser in text-styles.ts. Covers CRLF and lone-CR (old-mac) endings. P3 (confidence 0.82) - the zalouser markdown parser treats `+ item` as an unordered list, but the blank-line collapse regexes only matched `-`, `*`, and numbered items. A `+` list separated by blank lines still rendered fragmented. Fix: add `+` to the shared list-item character class in all three patterns, and extend the `after` negative lookahead to exclude `*`/`+` so list-to-list gaps stay with the `between` rule. Tests: 6 new outbound-format.test.ts cases (CRLF bullets, CR-terminated HR, lone-CR; `+` bullets, mixed -/*/+/numbered, prose-to-+-list). Full zalouser suite: 194 passed, 2 skipped.
1 parent f368bcf commit 57012bb

2 files changed

Lines changed: 52 additions & 4 deletions

File tree

‎extensions/zalouser/src/outbound-format.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,40 @@ describe("normalizeZalouserOutboundText", () => {
196196
});
197197
});
198198

199+
describe("CRLF / CR line endings (P2 review fix)", () => {
200+
it("collapses blank lines between bullets with CRLF endings", () => {
201+
const input = "- a\r\n\r\n- b";
202+
expect(normalizeZalouserOutboundText(input)).toBe("- a\n- b");
203+
});
204+
205+
it("strips an HR line with a trailing CR", () => {
206+
const input = "before\r\n---\r\nafter";
207+
expect(normalizeZalouserOutboundText(input)).toBe("before\n\nafter");
208+
});
209+
210+
it("handles lone CR (old-mac) line endings", () => {
211+
const input = "- a\r\r- b";
212+
expect(normalizeZalouserOutboundText(input)).toBe("- a\n- b");
213+
});
214+
});
215+
216+
describe("plus-sign list markers (P3 review fix)", () => {
217+
it("collapses blank lines between + bullets", () => {
218+
const input = "+ first\n\n+ second\n\n+ third";
219+
expect(normalizeZalouserOutboundText(input)).toBe("+ first\n+ second\n+ third");
220+
});
221+
222+
it("collapses a mixed -, *, +, numbered list", () => {
223+
const input = "- dash\n\n* star\n\n+ plus\n\n1. one";
224+
expect(normalizeZalouserOutboundText(input)).toBe("- dash\n* star\n+ plus\n1. one");
225+
});
226+
227+
it("collapses blank line between prose and a + list", () => {
228+
const input = "Intro.\n\n\n+ alpha\n+ beta";
229+
expect(normalizeZalouserOutboundText(input)).toBe("Intro.\n+ alpha\n+ beta");
230+
});
231+
});
232+
199233
describe("idempotency", () => {
200234
it("is stable: f(f(x)) === f(x) on a representative agent reply", () => {
201235
const input = [

‎extensions/zalouser/src/outbound-format.ts‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,19 @@ export function normalizeZalouserOutboundText(text: string): string {
6161
return text;
6262
}
6363

64+
// Normalize CRLF / CR to LF first, mirroring the markdown style parser
65+
// (text-styles.ts). Otherwise Windows-style agent output (`---\r\n`,
66+
// `- a\r\n\r\n- b`) keeps a trailing \r on each line, which prevents the
67+
// HR / list / fence matchers below ($-anchored, [ \t]-only) from firing
68+
// and the broken spacing reaches Zalo unchanged.
69+
const normalizedEols = text.replace(/\r\n?/g, "\n");
70+
6471
// Split the text into alternating prose / fenced-code segments. Each
6572
// prose run is normalized independently; fenced runs pass through
6673
// verbatim including their opening + closing fence lines. We rejoin
6774
// with a single newline because the splitter consumed them as line
6875
// separators.
69-
const lines = text.split("\n");
76+
const lines = normalizedEols.split("\n");
7077
const out: string[] = [];
7178
let proseBuf: string[] = [];
7279
// null when outside a fence; otherwise the opener's char + length so we
@@ -139,10 +146,17 @@ function normalizeProseSegment(text: string): string {
139146
// expose another match (e.g. three list items separated by blank lines
140147
// need two passes). Convergence is guaranteed because every transform
141148
// strictly reduces the number of newline characters; it never adds.
149+
// List item markers: ordered (`1.`) or unordered (`-`, `*`, `+`). The
150+
// `+` bullet is valid CommonMark and the zalouser markdown parser treats
151+
// it as a list, so it must share the collapse rules or a `+` list with
152+
// blank lines still renders fragmented on Zalo.
142153
const between =
143-
/([ \t]*(?:\d+\.|[-*])[ \t]+[^\n]*)(?:\n[ \t]*)+\n(?=[ \t]*(?:\d+\.|[-*])[ \t]+[^\n]*)/g;
144-
const before = /([^\n])(?:\n[ \t]*)+\n(?=[ \t]*(?:\d+\.|[-*])[ \t]+[^\n]*)/g;
145-
const after = /([ \t]*(?:\d+\.|[-*])[ \t]+[^\n]*)(?:\n[ \t]*)+\n(?=[^\n -])/g;
154+
/([ \t]*(?:\d+\.|[-*+])[ \t]+[^\n]*)(?:\n[ \t]*)+\n(?=[ \t]*(?:\d+\.|[-*+])[ \t]+[^\n]*)/g;
155+
const before = /([^\n])(?:\n[ \t]*)+\n(?=[ \t]*(?:\d+\.|[-*+])[ \t]+[^\n]*)/g;
156+
// `after` collapses a blank gap between the last list item and following
157+
// PROSE; the lookahead excludes another list marker (-, *, +, space,
158+
// newline) so list-to-list gaps are left to `between`.
159+
const after = /([ \t]*(?:\d+\.|[-*+])[ \t]+[^\n]*)(?:\n[ \t]*)+\n(?=[^\n *+-])/g;
146160

147161
let prev: string;
148162
do {

0 commit comments

Comments
 (0)