Files
buzz/mobile/lib/features/channels/message_content/token_pill.dart
Tom Brow b80217de32 fix(mobile): end #channel tokens at apostrophes, quotes and dashes (#8860)
Mobile renders `#channel` tokens as chips, but a token followed by an
apostrophe, quote, ellipsis or dash stayed plain text: `#general's`,
`"#general"`, `#general…` and `#general—` all lost their chip because
the token only ended at whitespace or `, ; . ! ? : ) ] }`.

This ends channel tokens at those characters too (`' " ‘ ’ “ ” » … –
—`). The message body and the one-line preview each had their own copy
of the channel regex; they now share one `channelTokenPattern`, so they
can't drift apart again.

`@mention` boundaries are unchanged on purpose: on mobile and desktop
the same boundary also decides which mentions are bound and tagged when
sending, so changing it is a separate, cross-surface decision.

### Validation

Added tests:
- Preview table covering each new trailing character (and matching
opening quotes).
- Message body: `#general's` keeps the chip and renders the `'s` suffix.

Both fail with the previous boundary. The full mobile test suite passed
in pre-push.

Simulator render on an iPhone 18 Pro (iOS 27): real message-body and
preview widgets with channels `general` and `eng` loaded, rendered with
both stacked PRs applied.

| Before (`main`) | After
([#8860](https://github.com/block/buzz/pull/8860) +
[#8862](https://github.com/block/buzz/pull/8862)) |
| --- | --- |
| <img
src="https://raw.githubusercontent.com/block/buzz/5afead6a162d5c8554ad17175fc2dccff93bf2af/pr-8862--01-before-main.png"
width="300"> | <img
src="https://raw.githubusercontent.com/block/buzz/5afead6a162d5c8554ad17175fc2dccff93bf2af/pr-8862--02-after-both-prs.png"
width="300"> |

Human app test: not applicable under the mobile-client testing
exception; agent review and simulator validation above.

buzz-review-completed

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Tom Brow <tomb@squareup.com>
Co-authored-by: Tom Brow <tomb@squareup.com>
2026-10-31 09:30:23 -07:00

142 lines
4.1 KiB
Dart

part of '../message_content.dart';
String? _channelNameForId(Map<String, String> channels, String channelId) {
for (final entry in channels.entries) {
if (entry.value == channelId) return entry.key;
}
return null;
}
class _ChannelLinkMd extends InlineMd {
final Map<String, String> channelNames;
final void Function(String channelId)? onChannelTap;
late final RegExp _exp = channelTokenPattern(channelNames.keys);
_ChannelLinkMd({required this.channelNames, this.onChannelTap});
/// Excluded from link labels: this component renders a [WidgetSpan], and a
/// placeholder nested inside the link's own placeholder does not paint on
/// iOS — an authored `[#channel](url)` renders as nothing. Link resolution
/// wins over token detection inside a label.
@override
Set<MarkdownScope> get scopes => MarkdownComponent.allScopesExceptLinkLabel;
@override
RegExp get exp => _exp;
@override
InlineSpan span(
BuildContext context,
String text,
final GptMarkdownConfig config,
) {
final raw = exp.firstMatch(text.trim())?.group(0);
if (raw == null) {
return TextSpan(text: text, style: config.style);
}
final channelId = channelNames[raw.substring(1).toLowerCase()];
final channelName = raw.substring(1);
final opensChannel = channelId != null && onChannelTap != null;
final child = _TokenPill(
icon: BuzzIcons.hash,
interactive: opensChannel,
semanticLabel: opensChannel
? 'Open channel $channelName'
: 'Channel $channelName',
text: channelName,
textStyle: config.style?.copyWith(fontWeight: FontWeight.w500),
);
return WidgetSpan(
alignment: PlaceholderAlignment.baseline,
baseline: TextBaseline.alphabetic,
child: opensChannel
? GestureDetector(onTap: () => onChannelTap!(channelId), child: child)
: child,
);
}
}
class _TokenPill extends StatelessWidget {
final IconData? icon;
final bool interactive;
final String? semanticLabel;
final String text;
final TextStyle? textStyle;
const _TokenPill({
super.key,
this.icon,
this.interactive = false,
this.semanticLabel,
required this.text,
this.textStyle,
});
@override
Widget build(BuildContext context) {
final style =
textStyle?.copyWith(color: context.colors.primary) ??
context.textTheme.bodyMedium?.copyWith(color: context.colors.primary);
final fontSize = style?.fontSize ?? 16;
final pill = Container(
padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 1),
decoration: BoxDecoration(
color: context.colors.primary.withValues(alpha: 0.15),
borderRadius: BorderRadius.circular(Radii.sm),
),
child: Row(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
if (icon != null) ...[
Icon(icon, size: fontSize * 0.95, color: context.colors.primary),
const SizedBox(width: Grid.quarter + 1),
],
Text(text, style: style),
],
),
);
if (semanticLabel == null) return pill;
return Semantics(
label: semanticLabel,
button: interactive,
child: ExcludeSemantics(child: pill),
);
}
}
RegExp _buildPrefixPattern({
required String prefix,
required Iterable<String> knownNames,
required String genericTokenPattern,
}) {
final names =
knownNames
.map((name) => name.trim())
.where((name) => name.isNotEmpty)
.toSet()
.toList()
..sort((a, b) => b.length.compareTo(a.length));
final escapedPrefix = RegExp.escape(prefix);
const leadingBoundary = r'(?<![\w./:-])';
const trailingBoundary = r'(?=$|[\s,;.!?:)\]}])';
if (names.isEmpty) {
return RegExp(
'$leadingBoundary$escapedPrefix(?:$genericTokenPattern)$trailingBoundary',
caseSensitive: false,
multiLine: true,
);
}
final knownAlternatives = names.map(RegExp.escape).join('|');
return RegExp(
'$leadingBoundary$escapedPrefix(?:(?:$knownAlternatives)$trailingBoundary|(?:$genericTokenPattern)$trailingBoundary)',
caseSensitive: false,
multiLine: true,
);
}