The best way to control Claude Code, Codex and more from any computer or phone
fix(mobile): a table right under a line of text renders as a table (#20120)
Co-authored-by: PR Batch Tester <agent@local.test> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
shivam committed
Oct 16, 2026 at 09:39 UTC
dcdb0348e4091ebdc69c8ebfaa8537aa319b3c9a
3 files changed
+123
-2
apps/mobile/src/lib/markdownDocument.ts
+62
@@ -141,6 +141,68 @@ export function markdownWithHtml(markdown: string): string {
141
return markdown;
142
}
143
144
+// A delimiter row such as `|---|:--:|`, possibly inside a blockquote or list item.
145
+function isTableDelimiterRow(line: string): boolean {
146
+ if (!line.includes("-")) return false;
147
+ const row = line.replace(/^[ \t>]+/, "").trim();
148
+ return row
149
+ .replace(/^\||\|$/g, "")
150
+ .split("|")
151
+ .every((cell) => /^:?-+:?$/.test(cell.trim()));
152
+}
153
+
154
+// Cheap pre-check for a delimiter row two lines below text, so most messages skip the parse.
155
+function mayHaveTableUnderText(markdown: string): boolean {
156
+ if (!markdown.includes("|")) return false;
157
+ const lines = markdown.split(/\r\n|\r|\n/);
158
+ for (let index = 2; index < lines.length; index += 1) {
159
+ if (isTableDelimiterRow(lines[index]!) && /[^\s>]/.test(lines[index - 2]!)) return true;
160
+ }
161
+ return false;
162
+}
163
+
164
+/**
165
+ * GFM lets a table interrupt a paragraph, but the native parser (md4c) only starts a table
166
+ * on a paragraph's first line, so a table right under a line of text renders as one
167
+ * run-on paragraph. Insert the blank line md4c needs before each such table.
168
+ */
169
+export function markdownWithTableBreaks(markdown: string): string {
170
+ if (!mayHaveTableUnderText(markdown)) return markdown;
171
+ const lineStarts: number[] = [];
172
+ const visit = (node: MarkdownNode) => {
173
+ if (!("children" in node)) return;
174
+ node.children.forEach((child, index) => {
175
+ const previous = node.children[index - 1];
176
+ const start = child.position?.start;
177
+ if (
178
+ child.type === "table" &&
179
+ previous?.type === "paragraph" &&
180
+ start?.offset !== undefined &&
181
+ previous.position?.end.line === start.line - 1
182
+ )
183
+ lineStarts.push(
184
+ Math.max(
185
+ markdown.lastIndexOf("\n", start.offset - 1),
186
+ markdown.lastIndexOf("\r", start.offset - 1),
187
+ ) + 1,
188
+ );
189
+ visit(child);
190
+ });
191
+ };
192
+ visit(parser.parse(markdown));
193
+ for (const lineStart of lineStarts.reverse()) {
194
+ // The header row may be a lazy line without its container markers (`>` or list indent),
195
+ // so copy the delimiter row's markers onto the blank line and the header row.
196
+ const [, header = "", cells = "", ending = "\n", markers = ""] =
197
+ /^([ \t>]*)([^\r\n]*)(\r\n|\r|\n)([ \t>]*)/.exec(markdown.slice(lineStart)) ?? [];
198
+ markdown =
199
+ markdown.slice(0, lineStart) +
200
+ `${markers.trimEnd()}${ending}${markers}${cells}` +
201
+ markdown.slice(lineStart + header.length + cells.length);
202
+ }
203
+ return markdown;
204
+}
205
+
206
export function parseMarkdownDocument(markdown: string): Root {
207
return parser.parse(markdownWithHtml(markdown));
208
}
apps/mobile/src/lib/markdownTableBreaks.test.ts
new
+56
@@ -0,0 +1,56 @@
1
+import { describe, expect, it } from "vite-plus/test";
2
+import { markdownWithTableBreaks } from "./markdownDocument";
3
+
4
+const table = "| Company | Fit |\n|---|---|\n| Acme | A |";
5
+
6
+describe("tables right under a line of text", () => {
7
+ it("separates a table from the paragraph it interrupts", () => {
8
+ expect(markdownWithTableBreaks(`Companies on file\n${table}`)).toBe(
9
+ `Companies on file\n\n${table}`,
10
+ );
11
+ });
12
+
13
+ it("handles CRLF and CR line endings", () => {
14
+ const crlf = table.replaceAll("\n", "\r\n");
15
+ expect(markdownWithTableBreaks(`Companies on file\r\n${crlf}`)).toBe(
16
+ `Companies on file\r\n\r\n${crlf}`,
17
+ );
18
+ const cr = table.replaceAll("\n", "\r");
19
+ expect(markdownWithTableBreaks(`Companies on file\r${cr}`)).toBe(`Companies on file\r\r${cr}`);
20
+ });
21
+
22
+ it("keeps the blank line inside a blockquote", () => {
23
+ const quoted = table.replace(/^/gm, "> ");
24
+ expect(markdownWithTableBreaks(`> Companies on file\n${quoted}`)).toBe(
25
+ `> Companies on file\n>\n${quoted}`,
26
+ );
27
+ });
28
+
29
+ it("keeps a table with a lazy header row inside its container", () => {
30
+ expect(markdownWithTableBreaks("> Intro\n| a | b |\n> | - | - |\n> | c | d |")).toBe(
31
+ "> Intro\n>\n> | a | b |\n> | - | - |\n> | c | d |",
32
+ );
33
+ expect(markdownWithTableBreaks("- Intro\n| a | b |\n | - | - |\n | c | d |")).toBe(
34
+ "- Intro\n\n | a | b |\n | - | - |\n | c | d |",
35
+ );
36
+ });
37
+
38
+ it("separates every interrupting table, including ones in list items", () => {
39
+ const indented = table.replace(/^/gm, " ");
40
+ expect(markdownWithTableBreaks(`Intro\n${table}\n\n- Item\n${indented}`)).toBe(
41
+ `Intro\n\n${table}\n\n- Item\n\n${indented}`,
42
+ );
43
+ });
44
+
45
+ it("leaves Markdown that already renders alone", () => {
46
+ for (const markdown of [
47
+ `Companies on file\n\n${table}`,
48
+ `# Companies\n${table}`,
49
+ `\`\`\`md\nCompanies on file\n${table}\n\`\`\``,
50
+ "Not a table\n| a | b |\n|---|",
51
+ "a | b\nplain text",
52
+ ]) {
53
+ expect(markdownWithTableBreaks(markdown)).toBe(markdown);
54
+ }
55
+ });
56
+});
apps/mobile/src/native/EnrichedMarkdownText.tsx
+5
-2
@@ -4,7 +4,7 @@ import {
4
formatComposerContextHref,
5
formatComposerContextReference,
6
} from "@t3tools/shared/composerContextReferences";
7
-import { markdownWithHtml } from "../lib/markdownDocument";
7
+import { markdownWithHtml, markdownWithTableBreaks } from "../lib/markdownDocument";
8
import { useCallback, useMemo, useState, type ReactNode } from "react";
9
import { Platform, Share, StyleSheet, View } from "react-native";
10
import {
@@ -140,7 +140,10 @@ function enrichedStyle(
140
141
export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {
142
const { renderImage, resolveImageSource } = props;
143
- const markdown = useMemo(() => markdownWithHtml(props.markdown), [props.markdown]);
143
+ const markdown = useMemo(
144
+ () => markdownWithTableBreaks(markdownWithHtml(props.markdown)),
145
+ [props.markdown],
146
+ );
147
const { themeAppearance } = useAppearancePreferences();
148
const [documentAssets, setDocumentAssets] = useState<DocumentAssetsEvent["assets"]>([]);
149
const onDocumentAssets = useCallback(({ assets }: DocumentAssetsEvent) => {