Skip to content

Commit

Permalink
compose: Implement inlineLink, for Markdown "inline link" syntax
Browse files Browse the repository at this point in the history
And use it in the one place where we've been creating inline links.

This is a small amount of code, but drawing it out into a helper
gives a convenient place to write down its shortcomings.

We'll also use this for zulip#116 quote-and-reply, coming up.
  • Loading branch information
chrisbobbe committed Jun 17, 2023
1 parent cd46e8d commit a44f68f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
19 changes: 19 additions & 0 deletions lib/model/compose.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,22 @@ Uri narrowLink(PerAccountStore store, Narrow narrow, {int? nearMessageId}) {
String mention(User user, {bool silent = false}) {
return '@${silent ? '_' : ''}**${user.fullName}|${user.userId}**';
}

/// https://spec.commonmark.org/0.30/#inline-link
///
/// The "link text" is made by enclosing [visibleText] in square brackets.
/// If [visibleText] has unexpected features, such as square brackets, the
/// result may be surprising.
///
/// The part between "(" and ")" is just a "link destination" (no "link title").
/// That destination is simply the stringified [destination], if provided.
/// If that has parentheses in it, the result may be surprising.
// TODO (?) try harder to guarantee output that creates an inline link,
// and in particular, the intended one. Tricky in part because that depends on
// nearby content in a line. From the spec:
// > Backtick code spans, autolinks, and raw HTML tags bind more tightly
// > than the brackets in link text. Thus, for example, [foo`]` could not be
// > a link text, since the second ] is part of a code span.
String inlineLink(String visibleText, Uri? destination) {
return '[$visibleText](${destination?.toString() ?? ''})';
}
7 changes: 4 additions & 3 deletions lib/widgets/compose_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:image_picker/image_picker.dart';

import '../api/route/messages.dart';
import '../model/autocomplete.dart';
import '../model/compose.dart';
import '../model/narrow.dart';
import 'dialog.dart';
import 'store.dart';
Expand Down Expand Up @@ -135,7 +136,7 @@ class ComposeContentController extends ComposeController<ContentValidationError>
int registerUploadStart(String filename) {
final tag = _nextUploadTag;
_nextUploadTag += 1;
final placeholder = '[Uploading $filename...]()'; // TODO(i18n)
final placeholder = inlineLink('Uploading $filename...', null); // TODO(i18n)
_uploads[tag] = (filename: filename, placeholder: placeholder);
notifyListeners(); // _uploads change could affect validationErrors
value = value.replaced(_insertionIndex(), '$placeholder\n\n');
Expand All @@ -158,8 +159,8 @@ class ComposeContentController extends ComposeController<ContentValidationError>
value = value.replaced(
replacementRange,
url == null
? '[Failed to upload file: $filename]()' // TODO(i18n)
: '[$filename](${url.toString()})');
? inlineLink('Failed to upload file: $filename', null) // TODO(i18n)
: inlineLink(filename, url));
_uploads.remove(tag);
notifyListeners(); // _uploads change could affect validationErrors
}
Expand Down
5 changes: 5 additions & 0 deletions test/model/compose_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,9 @@ hello
check(mention(user, silent: false)).equals('@**Full Name|123**');
check(mention(user, silent: true)).equals('@_**Full Name|123**');
});

group('inlineLink', () {
check(inlineLink('CZO', Uri.parse('https://chat.zulip.org/'))).equals('[CZO](https://chat.zulip.org/)');
check(inlineLink('Uploading file.txt…', null)).equals('[Uploading file.txt…]()');
});
}

0 comments on commit a44f68f

Please sign in to comment.