Skip to content

Commit 2d29185

Browse files
browclaude
andcommitted
mobile: cut previews only at fences and spans gpt_markdown parses
A fence now closes only at a line holding just the closing backticks, and a run of backticks inside a line keeps the whole message. Delimiters inside inline code no longer count toward the balance check. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Tom Brow <[email protected]>
1 parent 7fcd306 commit 2d29185

2 files changed

Lines changed: 58 additions & 4 deletions

File tree

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,25 @@
1313
/// pair with one in the dropped tail is balanced in the kept part.
1414
String markdownPreviewSource(String content, int maxLines) {
1515
final lines = content.split('\n');
16+
final prose = <String>[];
1617
var units = 0;
1718
var fenceOpen = false;
1819
String? group;
1920
for (var i = 0; i < lines.length; i++) {
2021
final line = lines[i];
21-
final fences = '```'.allMatches(line).length;
2222
if (fenceOpen) {
23-
fenceOpen = fences.isEven;
23+
fenceOpen = !_fenceClose.hasMatch(line);
2424
continue;
2525
}
2626
final trimmed = line.trimLeft();
2727
if (trimmed.isEmpty) {
2828
group = null;
2929
continue;
3030
}
31+
final opensFence = _fenceOpen.hasMatch(line);
32+
// A run of backticks inside a line pairs in ways the units here do not
33+
// model, so such a message is parsed whole.
34+
if (!opensFence && line.contains('```')) return content;
3135
final lineGroup = trimmed.startsWith('|')
3236
? '|'
3337
: trimmed.startsWith('>')
@@ -36,16 +40,34 @@ String markdownPreviewSource(String content, int maxLines) {
3640
if (lineGroup == null || lineGroup != group) {
3741
if (units == maxLines + 1) {
3842
final kept = lines.take(i).join('\n').trimRight();
39-
return _closesInline(kept) ? kept : content;
43+
final outsideCode = _withoutCode(prose);
44+
return _closesInline(kept) &&
45+
!outsideCode.contains('`') &&
46+
_closesInline(outsideCode)
47+
? kept
48+
: content;
4049
}
4150
units++;
4251
}
4352
group = lineGroup;
44-
fenceOpen = fences.isOdd;
53+
fenceOpen = opensFence;
54+
if (!opensFence) prose.add(line);
4555
}
4656
return content;
4757
}
4858

59+
// gpt_markdown opens a fence only at the start of a line and closes it only
60+
// at a line holding nothing but the closing backticks.
61+
final _fenceOpen = RegExp(r'^ *```');
62+
final _fenceClose = RegExp(r'^\s*```$');
63+
final _inlineCode = RegExp(r'`(?!`)(.+?)(?<!`)`(?!`)');
64+
65+
/// The prose lines without their inline code spans, since delimiters inside
66+
/// a span cannot pair with ones outside it. A backtick left over means a span
67+
/// this cannot place, and the caller keeps the whole message.
68+
String _withoutCode(List<String> prose) =>
69+
prose.map((line) => line.replaceAll(_inlineCode, '')).join('\n');
70+
4971
bool _closesInline(String text) {
5072
bool even(Pattern pattern) => pattern.allMatches(text).length.isEven;
5173
int count(String token) => token.allMatches(text).length;

‎mobile/test/features/channels/message_content/preview_source_test.dart‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,26 @@ void main() {
8989
maxLines: 2,
9090
cut: true,
9191
),
92+
(
93+
name: 'backticks inside a fence',
94+
source: [
95+
'```dart',
96+
"final fence = '```';",
97+
'final x = 1;',
98+
'```',
99+
'One.',
100+
'Two.',
101+
'Three.',
102+
].join('\n'),
103+
maxLines: 2,
104+
cut: true,
105+
),
106+
(
107+
name: 'inline code beside bold',
108+
source: ['`a` **b** `c`', 'two', 'three', 'four'].join('\n'),
109+
maxLines: 1,
110+
cut: true,
111+
),
92112
(name: 'short', source: 'One.\n\nTwo.\n\nThree.', maxLines: 2, cut: false),
93113
(
94114
name: 'bold closing in the tail',
@@ -108,6 +128,18 @@ void main() {
108128
maxLines: 2,
109129
cut: false,
110130
),
131+
(
132+
name: 'bold marker inside inline code',
133+
source: ['`**` **opened', 'two', 'three', 'closed**'].join('\n'),
134+
maxLines: 1,
135+
cut: false,
136+
),
137+
(
138+
name: 'backticks inside a line',
139+
source: ['use ```x``` here', 'two', 'three', 'four'].join('\n'),
140+
maxLines: 1,
141+
cut: false,
142+
),
111143
(
112144
name: 'unclosed fence',
113145
source: ['```', for (var i = 0; i < 10; i++) 'line $i'].join('\n'),

0 commit comments

Comments
 (0)