mirror of
https://github.com/block/buzz.git
synced 2026-11-24 18:21:59 +00:00
Adds a pure Dart function, `notificationBody`, that turns a message's Markdown content into the plain-text notification body defined by [ADR 0002](https://github.com/block/buzz/blob/main/docs/adr/0002-notification-message-text.md). It is a port of the Swift `BuzzPushNotificationResolver.previewBody` in `BuzzPushKit`, which today is the only implementation. Android notifications will be built from the app's Dart code, as in the [Android push design](https://github.com/block/buzz/pull/9555), and will reuse this function so the body reads the same on both platforms. Nothing calls it yet and the Swift side is unchanged. Notes on the port: - The cap stays at 256 UTF-16 code units, the same as iOS (the fixtures set `maxLength` to 256). - Dart's regex engine differs from the ICU one the Swift code uses (`\s`, `.` and `$`), so the patterns spell those out to keep identical results. Trimming also matches Swift's `whitespacesAndNewlines`, which trims U+200B and keeps U+FEFF, where Dart's `trim` does the opposite for both. - Truncation counts whole characters (extended grapheme clusters) via `package:characters`. ### Validation - A table-driven test runs every case in `test-fixtures/notification-body.json` (47 cases) and checks the fixture's `maxLength` against the function's cap. - Added tests: 8 rows in the shared fixture pin the ICU details the port must match. U+000B and U+0085 beside a marker keep it, U+000B and U+000C inside a run keep its markers, U+200B is trimmed at the edges and doesn't count toward the cap, and U+FEFF at the edges is kept. The Swift fixture test runs them too and passes them unchanged. - Proved falsifiable: with code-span and fence handling disabled, 18 fixture cases fail; restored, all pass. - `just mobile-install mobile-check mobile-test` passes. ### Regressions None: new code that nothing calls yet. The Swift formatter is untouched. Its fixture test now also runs the 8 new rows and passes them unchanged. --------- Signed-off-by: Tom Brow <tomb@squareup.com> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
121 lines
4.7 KiB
Dart
121 lines
4.7 KiB
Dart
import 'package:characters/characters.dart';
|
|
|
|
/// Longest notification body, in UTF-16 code units. iOS shows at most this
|
|
/// much of a body, so every platform's body fits in it. See
|
|
/// docs/adr/0002-notification-message-text.md.
|
|
const int notificationBodyLimit = 256;
|
|
|
|
/// Most of a message's content, in UTF-16 code units, that the body is built
|
|
/// from. The regexes below can take quadratic time on unclosed markup and
|
|
/// content can be up to 256 KB, so a long message is cut first.
|
|
const int notificationBodyInputLimit = 4096;
|
|
|
|
/// Plain-text notification body for a Markdown message, as ADR 0002 defines it
|
|
/// and `test-fixtures/notification-body.json` checks it. It is a port of
|
|
/// `BuzzPushNotificationResolver.previewBody` in `BuzzPushKit`: change the two
|
|
/// together.
|
|
///
|
|
/// Code and URLs stay as written, line breaks are kept, and the Markdown
|
|
/// syntax for code, links, images, bold and strikethrough is removed. Runs of
|
|
/// spaces collapse outside code. A body over [notificationBodyLimit] code
|
|
/// units, or built from content cut at [notificationBodyInputLimit], keeps
|
|
/// whole characters up to one unit short of the limit and ends with "…".
|
|
String notificationBody(String content) {
|
|
var input = content;
|
|
final cut = input.length > notificationBodyInputLimit;
|
|
if (cut) {
|
|
final kept = StringBuffer();
|
|
var units = 0;
|
|
for (final character in input.characters) {
|
|
units += character.length;
|
|
if (units > notificationBodyInputLimit) break;
|
|
kept.write(character);
|
|
}
|
|
input = kept.toString();
|
|
}
|
|
input = input
|
|
.replaceAll('\r\n', '\n')
|
|
.replaceAll(_codeOpen, '')
|
|
.replaceAll(_codeClose, '');
|
|
|
|
// Swap each code span for a placeholder while the prose rules run over the
|
|
// whole text, so a link label or a bold run can contain code.
|
|
final codes = <String>[];
|
|
final prose = StringBuffer();
|
|
var position = 0;
|
|
for (final match in _code.allMatches(input)) {
|
|
prose.write(input.substring(position, match.start));
|
|
final body = match.group(1) ?? match.group(3) ?? match.group(4)!;
|
|
prose
|
|
..write(_codeOpen)
|
|
..write(codes.length)
|
|
..write(_codeClose);
|
|
codes.add(body);
|
|
position = match.end;
|
|
}
|
|
prose.write(input.substring(position));
|
|
|
|
final stripped = _stripProse(prose.toString());
|
|
var result = stripped.replaceAllMapped(
|
|
_codePlaceholder,
|
|
(match) => codes[int.parse(match.group(1)!)],
|
|
);
|
|
result = result
|
|
.replaceAll(RegExp(r'[ \t]+\n'), '\n')
|
|
.replaceAll(RegExp(r'\n{3,}'), '\n\n');
|
|
result = _trim(result);
|
|
if (!cut && result.length <= notificationBodyLimit) return result;
|
|
|
|
final kept = StringBuffer();
|
|
var units = 0;
|
|
for (final character in result.characters) {
|
|
units += character.length;
|
|
if (units >= notificationBodyLimit) break;
|
|
kept.write(character);
|
|
}
|
|
return '${_trim(kept.toString())}…';
|
|
}
|
|
|
|
// The patterns below keep ICU regex semantics, which the iOS formatter uses,
|
|
// not Dart's. ICU's `\s` is `\p{White_Space}`, listed here, and its `.`
|
|
// excludes every line terminator: U+000A to U+000D, U+0085, U+2028, U+2029.
|
|
const _space =
|
|
r'\t-\r \u0085\u00A0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000';
|
|
const _notLineEnd = r'[^\n-\r\u0085\u2028\u2029]';
|
|
|
|
/// A fenced span on one line, a fenced code block, or an inline code span. A
|
|
/// block's opening line (a fence of three or more backticks, then an optional
|
|
/// language) is dropped, and the block closes only at a fence at least as long
|
|
/// that ends its line, so backticks inside the code (a "```" string literal)
|
|
/// don't end it. A fence with no closing fence runs to the end of the text, as
|
|
/// in CommonMark, which also covers a block cut by [notificationBodyInputLimit].
|
|
/// Group 2 is the block's fence; the others hold code.
|
|
final _code = RegExp(
|
|
r'```([^\n]*?)(?:```|$)'
|
|
r'|(`{3,})[^\n]*\n([\s\S]*?)(?:\n?\2`*[ \t]*(?=\n|$)|$)'
|
|
r'|`([^`\n]*)`',
|
|
);
|
|
|
|
/// Stands in for a code span while the prose rules run. Private-use
|
|
/// characters, removed from the content first, so they can't collide.
|
|
const _codeOpen = '\uE000';
|
|
const _codeClose = '\uE001';
|
|
final _codePlaceholder = RegExp('$_codeOpen([0-9]+)$_codeClose');
|
|
|
|
final _link = RegExp(r'!?\[([^\]]*)\]\([^)]*\)');
|
|
final _emphasis = RegExp(
|
|
'(\\*\\*|~~)([^$_space](?:$_notLineEnd*?[^$_space])??)\\1',
|
|
);
|
|
final _spaces = RegExp(r'[ \t]+');
|
|
|
|
String _stripProse(String prose) => prose
|
|
.replaceAllMapped(_link, (match) => match.group(1)!)
|
|
.replaceAllMapped(_emphasis, (match) => match.group(2)!)
|
|
.replaceAll(_spaces, ' ');
|
|
|
|
/// Trims the characters Swift's `whitespacesAndNewlines` does: ICU's
|
|
/// whitespace and U+200B. Unlike Dart's `String.trim`, it leaves U+FEFF alone.
|
|
final _edgeWhitespace = RegExp('^[$_space\\u200B]+|[$_space\\u200B]+\$');
|
|
|
|
String _trim(String text) => text.replaceAll(_edgeWhitespace, '');
|