Skip to content

Commit a3964ab

Browse files
square-tombstudioclaude
committed
fix(mobile): leave tilde fences and autolink backslashes literal
Treat `~~~` fences as code when shielding escapes, as bubble segments already do, so their backslashes stay literal. Encode backslashes in an `<url>` autolink as `&#92;` in its label and `%5C` in its destination, so none escapes the synthesized `]` or `)`. 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 ddc8b55 commit a3964ab

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ final _bareLinkPattern = RegExp(
1414
);
1515
final _trailingPunctuationPattern = RegExp(r'[.,!?:;]+$');
1616
final _backtickRunPattern = RegExp(r'`+');
17+
final _codeRunPattern = RegExp(r'`+|~{3,}');
1718

1819
/// Converts supported Buzz and HTTP(S) autolinks and bare links into Markdown
1920
/// links while leaving inline and fenced code untouched. Punctuation peeling
@@ -22,35 +23,46 @@ String normalizeBareLinks(String content) =>
2223
mapMarkdownProse(content, _normalizeLinkSegment);
2324

2425
/// Applies [transform] to each run of [content] outside inline and fenced
25-
/// code, leaving code untouched.
26+
/// code, leaving code untouched. Fences may use backticks or tildes.
2627
String mapMarkdownProse(String content, String Function(String) transform) {
2728
final buffer = StringBuffer();
2829
var offset = 0;
2930
var proseStart = 0;
3031
var codeStart = 0;
3132
var inlineDelimiterLength = 0;
3233
var fenceDelimiterLength = 0;
34+
var fenceChar = '';
3335

3436
while (offset < content.length) {
35-
final run = _backtickRunPattern.matchAsPrefix(content, offset);
37+
final run = _codeRunPattern.matchAsPrefix(content, offset);
3638
if (run == null) {
3739
offset++;
3840
continue;
3941
}
4042

4143
final runLength = run.end - run.start;
44+
final runChar = content[run.start];
4245
if (fenceDelimiterLength > 0) {
43-
if (_isClosingFence(content, run.start, run.end, fenceDelimiterLength)) {
46+
if (runChar == fenceChar &&
47+
_isClosingFence(content, run.start, run.end, fenceDelimiterLength)) {
4448
buffer.write(content.substring(codeStart, run.end));
4549
fenceDelimiterLength = 0;
4650
proseStart = run.end;
4751
}
4852
} else if (inlineDelimiterLength > 0) {
49-
if (runLength == inlineDelimiterLength) {
53+
if (runChar == '`' && runLength == inlineDelimiterLength) {
5054
buffer.write(content.substring(codeStart, run.end));
5155
inlineDelimiterLength = 0;
5256
proseStart = run.end;
5357
}
58+
} else if (runChar == '~') {
59+
// Tildes only fence code; shorter runs are strikethrough.
60+
if (_isOpeningFence(content, run.start, runLength)) {
61+
buffer.write(transform(content.substring(proseStart, run.start)));
62+
codeStart = run.start;
63+
fenceDelimiterLength = runLength;
64+
fenceChar = runChar;
65+
}
5466
} else if (_isEscaped(content, run.start)) {
5567
// An escaped backtick is literal; the rest of its run may still open.
5668
offset = run.start + 1;
@@ -65,6 +77,7 @@ String mapMarkdownProse(String content, String Function(String) transform) {
6577
buffer.write(transform(content.substring(proseStart, run.start)));
6678
codeStart = run.start;
6779
fenceDelimiterLength = runLength;
80+
fenceChar = runChar;
6881
}
6982

7083
offset = run.end;
@@ -276,9 +289,8 @@ String _normalizeProseLinks(String segment) {
276289
// An escaped `\<` is literal, not an autolink; its URL stays a bare link.
277290
var normalized = segment.replaceAllMapped(
278291
_autolinkPattern,
279-
(match) => _isEscaped(segment, match.start)
280-
? match[0]!
281-
: '[${match[1]}](${match[1]})',
292+
(match) =>
293+
_isEscaped(segment, match.start) ? match[0]! : _autolink(match[1]!),
282294
);
283295
normalized = normalized.replaceAllMapped(
284296
_bareLinkPattern,
@@ -287,6 +299,11 @@ String _normalizeProseLinks(String segment) {
287299
return normalized;
288300
}
289301

302+
// An autolink keeps its backslashes literally. Encode them so none is read
303+
// as an escape of the synthesized `]` or `)`.
304+
String _autolink(String url) =>
305+
'[${url.replaceAll(r'\', '&#92;')}](${url.replaceAll(r'\', '%5C')})';
306+
290307
String _normalizeBareLink(String segment, Match match) {
291308
final matched = match[0]!;
292309
var url = matched;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,13 +514,15 @@ void main() {
514514
(r'\`literal\` then `a\#b`', '`literal` then '),
515515
// Authored stand-in characters are not mistaken for escapes.
516516
('\uFF0A \u2060\uFF0A \\#', '\uFF0A \u2060\uFF0A #'),
517+
('~~~\nx \\# y\n~~~', r'x \# y'),
517518
// gpt_markdown shows this unparsed; the stand-in still reads as #.
518519
('~~a\nb \\#c~~', '\uFF03c'),
519520
// A bare link's trailing backslash stays outside the link.
520521
(r'see https://example.com/a\', 'see '),
521522
(r'in buzz://channel/9f18f3c6-b5e5-453d-b36d-65487913be0b.\', r'.\'),
522523
(r'*https://example.com/a\**', '*'),
523524
(r'see \<https://example.com/b>', 'see <'),
525+
(r'see <https://example.com/a\>', r'https://example.com/a\'),
524526
(
525527
'![shot \\#1](https://example.com/a.png)\n'
526528
'![shot \\#2](https://example.com/b.png)',

0 commit comments

Comments
 (0)