Skip to content
TypeScript

fix(mobile): stop indented code overflowing Android chat bubbles (#9347)

Adamulek1231575ada305b9
diff --git a/apps/mobile/src/lib/wideMarkdownBlocks.test.ts b/apps/mobile/src/lib/wideMarkdownBlocks.test.ts
index dd6d040300a..4f745aed885 100644
--- a/apps/mobile/src/lib/wideMarkdownBlocks.test.ts
+++ b/apps/mobile/src/lib/wideMarkdownBlocks.test.ts
@@ -17,6 +17,20 @@ describe("hasWideMarkdownBlock", () => {
expect(hasWideMarkdownBlock(" ```\ncode\n```")).toBe(true);
});
+ it("detects indented code blocks", () => {
+ const prompt = 'before\n\n def search(x):\n return x\n\n"""\n\nafter';
+ expect(hasWideMarkdownBlock(prompt)).toBe(true);
+ expect(hasWideMarkdownBlock("before\n\n\treturn x\n\nafter")).toBe(true);
+ expect(hasWideMarkdownBlock("before\n\n \treturn x\n\nafter")).toBe(true);
+ expect(hasWideMarkdownBlock("> return x")).toBe(true);
+ expect(hasWideMarkdownBlock("> > \treturn x")).toBe(true);
+ expect(hasWideMarkdownBlock(" 1. indented code")).toBe(true);
+ expect(hasWideMarkdownBlock(" - code-like bullet")).toBe(true);
+ expect(hasWideMarkdownBlock("before\n not code\nafter")).toBe(false);
+ expect(hasWideMarkdownBlock("before\n \nafter")).toBe(false);
+ expect(hasWideMarkdownBlock("before\n \t\nafter")).toBe(false);
+ });
+
it("detects top-level and blockquoted ordered-list markers", () => {
expect(hasWideMarkdownBlock("1. One\n2. Two\n3. Three\n4. Four\n5. Five")).toBe(true);
expect(hasWideMarkdownBlock("before\n3) Three")).toBe(true);
@@ -24,11 +38,9 @@ describe("hasWideMarkdownBlock", () => {
expect(hasWideMarkdownBlock("> > 3) Three")).toBe(true);
});
- it("detects nested ordered lists without treating indented code as a list", () => {
+ it("detects nested ordered lists", () => {
expect(hasWideMarkdownBlock("- Parent\n 1. Child\n 2. Child")).toBe(true);
expect(hasWideMarkdownBlock("> - Parent\n> 1. Child")).toBe(true);
- expect(hasWideMarkdownBlock(" 1. indented code")).toBe(false);
- expect(hasWideMarkdownBlock(" - code-like bullet\n 1. indented code")).toBe(false);
});
it("can limit ordered-list width pinning to Android", () => {
@@ -46,7 +58,7 @@ describe("hasWideMarkdownBlock", () => {
expect(hasWideMarkdownBlock(" > quoted", { includeBlockquotes: true })).toBe(true);
expect(hasWideMarkdownBlock("> quoted")).toBe(false);
expect(hasWideMarkdownBlock("prose > quoted", { includeBlockquotes: true })).toBe(false);
- expect(hasWideMarkdownBlock(" > indented code", { includeBlockquotes: true })).toBe(false);
+ expect(hasWideMarkdownBlock(" > indented code", { includeBlockquotes: true })).toBe(true);
});
it("detects GFM tables", () => {
diff --git a/apps/mobile/src/lib/wideMarkdownBlocks.ts b/apps/mobile/src/lib/wideMarkdownBlocks.ts
index c4bd2864e47..57588bab2a2 100644
--- a/apps/mobile/src/lib/wideMarkdownBlocks.ts
+++ b/apps/mobile/src/lib/wideMarkdownBlocks.ts
@@ -1,7 +1,7 @@
/**
* Detects markdown that the renderer draws as a block requiring a definite
- * user-bubble width: fenced code blocks, GFM tables, ordered lists, and
- * blockquotes when requested by the caller.
+ * user-bubble width: fenced and indented code blocks, GFM tables, ordered
+ * lists, and blockquotes when requested by the caller.
*
* Fenced code blocks and tables report an intrinsic width equal to their
* widest line, which is effectively unbounded. A user bubble sizes itself
@@ -43,6 +43,28 @@ function stripBlockquotePrefixes(line: string): string {
return content;
}
+function hasIndentedCodeBlock(text: string): boolean {
+ return text.split("\n").some((rawLine) => {
+ const line = stripBlockquotePrefixes(rawLine);
+ let column = 0;
+ let index = 0;
+
+ // Markdown tabs advance to the next four-column stop.
+ while (index < line.length) {
+ if (line[index] === " ") {
+ column += 1;
+ } else if (line[index] === "\t") {
+ column += 4 - (column % 4);
+ } else {
+ break;
+ }
+ index += 1;
+ }
+
+ return column >= 4 && index < line.length && line[index] !== "\r";
+ });
+}
+
function hasBlockquote(text: string): boolean {
return text.split("\n").some((line) => BLOCKQUOTE_PREFIX.test(line));
}
@@ -89,6 +111,9 @@ export function hasWideMarkdownBlock(
if (options.includeBlockquotes === true && hasBlockquote(text)) {
return true;
}
+ if (hasIndentedCodeBlock(text)) {
+ return true;
+ }
if (options.includeOrderedLists !== false && hasOrderedListItem(text)) {
return true;
}