@@ -225,26 +225,39 @@ final _emphasis = [
225225 RegExp (r'<u>(.+?)</u>' , caseSensitive: false ),
226226];
227227
228- /// Unwraps emphasis and resolves backslash escapes. An escaped character is
229- /// held as a private-use stand-in while delimiters are matched, so `\*x\*`
230- /// stays `*x*` .
231- String _unwrapEmphasis (String text) {
232- if (! text.contains (_escapePrefix) && ! _hasDelimiter.hasMatch (text)) {
233- return text;
234- }
235- var result = text.replaceAllMapped (
236- _escape,
237- (m) => String .fromCharCode (_escapeBase + m[1 ]! .codeUnitAt (0 )),
238- );
228+ /// Unwraps emphasis in [shielded] text.
229+ String _unwrapEmphasis (String shielded) {
230+ if (! _hasDelimiter.hasMatch (shielded)) return shielded;
231+ var result = shielded;
239232 for (final pattern in _emphasis) {
240233 result = result.replaceAllMapped (pattern, (m) => m[1 ]! );
241234 }
242- return result.replaceAllMapped (
243- _escaped,
244- (m) => String .fromCharCode (m[0 ]! .codeUnitAt (0 ) - _escapeBase),
245- );
235+ return result;
246236}
247237
238+ /// Holds each backslash-escaped character as a private-use stand-in, so no
239+ /// link, emphasis, mention, emoji or channel syntax matches it: `\*x\*` stays
240+ /// `*x*` and `\@alice` stays text.
241+ String _shield (String text) => text.contains (_escapePrefix)
242+ ? text.replaceAllMapped (
243+ _escape,
244+ (m) => String .fromCharCode (_escapeBase + m[1 ]! .codeUnitAt (0 )),
245+ )
246+ : text;
247+
248+ /// The characters [_shield] held, as displayed.
249+ String _unshield (String text) => text.replaceAllMapped (
250+ _escaped,
251+ (m) => String .fromCharCode (m[0 ]! .codeUnitAt (0 ) - _escapeBase),
252+ );
253+
254+ /// The escapes [_shield] held, back in source form, for helpers that read
255+ /// Markdown.
256+ String _reescape (String text) => text.replaceAllMapped (
257+ _escaped,
258+ (m) => '\\ ${String .fromCharCode (m [0 ]!.codeUnitAt (0 ) - _escapeBase )}' ,
259+ );
260+
248261const _escapePrefix = r'\' ;
249262const _escapeBase = 0xE000 ;
250263final _hasDelimiter = RegExp (r'[*_~<]' );
@@ -341,14 +354,15 @@ class _PreviewBuilder {
341354 return end;
342355 }
343356
344- void _inline (String source) {
357+ void _inline (String text) {
358+ final source = _shield (text);
345359 var start = 0 ;
346360 for (final match in _link.allMatches (source)) {
347361 _tokens (_unwrapEmphasis (source.substring (start, match.start)));
348362 start = match.end;
349363 final isImage = match[1 ]! .isNotEmpty;
350- final label = match[2 ]! ;
351- final url = match[3 ]! ;
364+ final label = _reescape ( match[2 ]! ) ;
365+ final url = _reescape ( match[3 ]! ) ;
352366 final imeta = imetaByUrl[normalizeMarkdownDestination (url)];
353367 final kind =
354368 classifyMediaUrl (url, imeta: imeta) ??
@@ -364,7 +378,9 @@ class _PreviewBuilder {
364378 continue ;
365379 }
366380 _tokens (
367- _unwrapEmphasis (label.isEmpty ? url : decodeMarkdownLabelSyntax (label)),
381+ _unwrapEmphasis (
382+ _shield (label.isEmpty ? url : decodeMarkdownLabelSyntax (label)),
383+ ),
368384 );
369385 }
370386 _tokens (_unwrapEmphasis (source.substring (start)));
@@ -377,15 +393,15 @@ class _PreviewBuilder {
377393 return channelId.substring (0 , channelId.length < 8 ? channelId.length : 8 );
378394 }
379395
380- /// Plain text with mention, custom emoji and channel tokens.
396+ /// [_shield] ed plain text with mention, custom emoji and channel tokens.
381397 void _tokens (String source) {
382398 if (source.isEmpty) return ;
383399 final found = < ({int start, int end, PreviewPiece piece})> [];
384400 if (mentionBindings.isNotEmpty && source.contains ('@' )) {
385401 for (final range in mentionOccurrences (source, mentionBindings.keys)) {
386402 final piece = _mention (
387403 range.label,
388- source.substring (range.start + 1 , range.end),
404+ _unshield ( source.substring (range.start + 1 , range.end) ),
389405 );
390406 if (piece != null ) {
391407 found.add ((start: range.start, end: range.end, piece: piece));
@@ -417,11 +433,11 @@ class _PreviewBuilder {
417433 var cursor = 0 ;
418434 for (final token in found) {
419435 if (token.start < cursor) continue ;
420- text (source.substring (cursor, token.start));
436+ text (_unshield ( source.substring (cursor, token.start) ));
421437 add (token.piece);
422438 cursor = token.end;
423439 }
424- text (source.substring (cursor));
440+ text (_unshield ( source.substring (cursor) ));
425441 }
426442
427443 /// Mirrors the thread view's mention chip: an ambiguous label stays text.
0 commit comments