Skip to content

Commit 78b9c1a

Browse files
square-tombstudioclaude
committed
fix(mobile): match desktop on tables, alerts and code spans
A real-message diff against desktop's parser found three differences: - A table typed directly under a caption line showed raw pipes. GFM lets a table interrupt a paragraph; plusparse doesn't, so a blank line is inserted before such a table. - `> [!NOTE]` quotes became alerts. Desktop has no alerts, so they stay quotes. - An unclosed backtick run paired with a run in a later block, flipping later code and prose, so math-delimiter escapes leaked into code as entities. A code span now ends with its paragraph. Bubble segmentation now unescapes a link destination itself before the imeta lookup, since content no longer arrives shielded (#8940 relied on that). Also pins gpt_markdown to exactly 1.3.4. Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Tom Brow <[email protected]>
1 parent cd286fe commit 78b9c1a

6 files changed

Lines changed: 124 additions & 4 deletions

File tree

‎mobile/lib/features/channels/message_content.dart‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import 'local_send_bubble_surface.dart';
3939
import 'media_viewer_page.dart';
4040
import 'message_content/link_normalizer.dart';
4141
import 'message_content/markdown_escapes.dart';
42+
import 'message_content/markdown_tables.dart';
4243
import 'message_content/preview_text.dart';
4344
import 'message_media.dart';
4445
import 'message_content/bubble_segments.dart';
@@ -276,7 +277,7 @@ class MessageContent extends HookConsumerWidget {
276277
: null;
277278
final galleryText = trailingGallery?.content ?? normalizedContent;
278279
final markdownContent = useMemoized(
279-
() => escapeMarkdownMathDelimiters(galleryText),
280+
() => separateMarkdownTables(escapeMarkdownMathDelimiters(galleryText)),
280281
[galleryText],
281282
);
282283
final customEmoji = _mergeCustomEmoji(
@@ -379,6 +380,8 @@ class MessageContent extends HookConsumerWidget {
379380
// normalizeBareLinks() already turns bare URLs into Markdown links;
380381
// gpt_markdown autolinks by default, so both would run.
381382
autolink: false,
383+
// Desktop has no GitHub alerts: a `> [!NOTE]` quote stays a quote.
384+
alertBuilder: (_, alert) => alert.asBlockQuote(),
382385
codeBuilder: (context, name, code, closed) => _MessageCodeBlock(
383386
name: name,
384387
code: code,

‎mobile/lib/features/channels/message_content/bubble_segments.dart‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ List<({String text, bool attachment})> messageBubbleSegments(
4646
final isAttachment =
4747
match != null &&
4848
(line.trim().startsWith('!') ||
49-
// Content arrives with escapes shielded; imeta keys are plain.
50-
imeta.containsKey(unshieldMarkdownEscapes(match.group(1)!)));
49+
// CommonMark unescapes link destinations; imeta keys are plain.
50+
imeta.containsKey(
51+
unshieldMarkdownEscapes(shieldMarkdownEscapes(match.group(1)!)),
52+
));
5153
if (attachment != null &&
5254
isAttachment != attachment &&
5355
line.trim().isNotEmpty) {

‎mobile/lib/features/channels/message_content/link_normalizer.dart‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,33 @@ bool _hasInlineCloserOnLine(String content, int start, int delimiterLength) {
186186
return false;
187187
}
188188

189+
/// Whether a closing run follows before the paragraph ends: a code span
190+
/// cannot continue past a blank line or into a list item, heading or fence.
189191
bool _hasInlineCloser(String content, int start, int delimiterLength) {
192+
final end = _paragraphEnd(content, start);
190193
for (final run in _backtickRunPattern.allMatches(content, start)) {
194+
if (run.start >= end) return false;
191195
if (run.end - run.start == delimiterLength) return true;
192196
}
193197
return false;
194198
}
195199

200+
int _paragraphEnd(String content, int start) {
201+
var newline = content.indexOf('\n', start);
202+
while (newline >= 0) {
203+
if (_paragraphBreak.matchAsPrefix(content, newline + 1) != null) {
204+
return newline;
205+
}
206+
newline = content.indexOf('\n', newline + 1);
207+
}
208+
return content.length;
209+
}
210+
211+
final _paragraphBreak = RegExp(
212+
r'[ \t]*(?:$|[-*+][ \t]|\d{1,9}[.)][ \t]|#{1,6}(?:[ \t]|$)|`{3}|~{3})',
213+
multiLine: true,
214+
);
215+
196216
// Numeric entities keep literal label punctuation out of the line-based
197217
// attachment, gallery and preview matchers, which do not balance brackets.
198218
// The renderer decodes them.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/// [content] with a blank line before each pipe table that directly follows
2+
/// a line of paragraph text.
3+
///
4+
/// In GFM, and on desktop, a table can interrupt a paragraph, so a table typed
5+
/// right under its caption renders as a table. gpt_markdown starts a table
6+
/// only on a fresh block and would show those rows as text. Fenced code is
7+
/// left alone.
8+
String separateMarkdownTables(String content) {
9+
if (!content.contains('|')) return content;
10+
final lines = content.split('\n');
11+
final out = <String>[];
12+
String? fence;
13+
for (var i = 0; i < lines.length; i++) {
14+
final line = lines[i];
15+
final trimmed = line.trimLeft();
16+
final marker = _fenceMarker.firstMatch(trimmed)?.group(1);
17+
if (marker != null) {
18+
if (fence == null) {
19+
fence = marker;
20+
} else if (marker[0] == fence[0] && marker.length >= fence.length) {
21+
fence = null;
22+
}
23+
} else if (fence == null &&
24+
i > 0 &&
25+
i + 1 < lines.length &&
26+
line == trimmed &&
27+
lines[i - 1].trim().isNotEmpty &&
28+
!lines[i - 1].startsWith(' ') &&
29+
_startsTable(line, lines[i + 1])) {
30+
out.add('');
31+
}
32+
out.add(line);
33+
}
34+
return out.join('\n');
35+
}
36+
37+
/// Whether [header] and [delimiter] open a GFM table: the delimiter row is
38+
/// only dashes, colons and pipes, with at least one pipe, and both rows have
39+
/// the same cell count.
40+
bool _startsTable(String header, String delimiter) {
41+
if (!header.contains('|') ||
42+
!delimiter.contains('|') ||
43+
!_delimiterRow.hasMatch(delimiter)) {
44+
return false;
45+
}
46+
return _cellCount(header) == _cellCount(delimiter);
47+
}
48+
49+
int _cellCount(String row) {
50+
var cells = row.trim().split(_unescapedPipe);
51+
if (cells.first.trim().isEmpty) cells = cells.sublist(1);
52+
if (cells.isNotEmpty && cells.last.trim().isEmpty) {
53+
cells = cells.sublist(0, cells.length - 1);
54+
}
55+
return cells.length;
56+
}
57+
58+
final _fenceMarker = RegExp(r'^(`{3,}|~{3,})');
59+
final _delimiterRow = RegExp(
60+
r'^ {0,3}\|?\s*:?-+:?\s*(?:\|\s*:?-+:?\s*)*\|?\s*$',
61+
);
62+
final _unescapedPipe = RegExp(r'(?<!\\)\|');

‎mobile/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ dependencies:
3030
path: packages/buzz_secp256k1
3131
pointycastle: ^4.0.0
3232
url_launcher: ^6.3.2
33-
gpt_markdown: '>=1.3.4 <1.4.0'
33+
gpt_markdown: 1.3.4
3434
highlight: ^0.7.0
3535
intl: ^0.20.2
3636
uuid: ^4.5.1

‎mobile/test/features/channels/message_content_test.dart‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,8 @@ void main() {
523523
('> ```sh\n> x\n\n```\necho \\#g\n```', r'echo \#g'),
524524
// A quoted fence ends with its quote, so later prose is parsed.
525525
('> ```sh\n> echo x\n\nplain \\#text', 'plain #text'),
526+
// An unclosed run ends with its paragraph, so later code stays code.
527+
('x ``` y\n- ``` p ```\n- q ``` \\[r\\] ``` s', r'\[r\]'),
526528
// A strike that spans a line break still decodes its escapes.
527529
('~~a\nb \\#c~~', 'b #c'),
528530
// A bare link's trailing backslash stays outside the link.
@@ -572,6 +574,37 @@ void main() {
572574
semantics.dispose();
573575
});
574576

577+
testWidgets('a quote opening with an alert marker stays a quote', (
578+
tester,
579+
) async {
580+
await tester.pumpWidget(
581+
_testable(const MessageContent(content: '> [!NOTE]\n> Builds run.')),
582+
);
583+
expect(find.textContaining('[!NOTE]', findRichText: true), findsWidgets);
584+
});
585+
586+
testWidgets('a table typed under its caption renders as a table', (
587+
tester,
588+
) async {
589+
Finder pipes() => find.textContaining('|', findRichText: true);
590+
for (final (content, table) in [
591+
('**Sizes:**\n| Folder | Size |\n|---|---|\n| `~/a` | 9 GB |', true),
592+
('Sizes:\n\n| Folder | Size |\n|---|---|\n| a | 9 GB |', true),
593+
// As in GFM, a delimiter row with a different cell count does not
594+
// open a table.
595+
('Sizes:\n| Folder | Size | Note |\n|---|---|\n| a | 9 GB |', false),
596+
('```\nSizes:\n| Folder | Size |\n|---|---|\n```', false),
597+
]) {
598+
await tester.pumpWidget(_testable(MessageContent(content: content)));
599+
expect(pipes(), table ? findsNothing : findsWidgets, reason: content);
600+
expect(
601+
find.textContaining('Folder', findRichText: true),
602+
findsWidgets,
603+
reason: content,
604+
);
605+
}
606+
});
607+
575608
testWidgets('forwards text alignment to markdown rendering', (
576609
tester,
577610
) async {

0 commit comments

Comments
 (0)