Skip to content

Commit

Permalink
internal_link: Add tryResolveOnRealmUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
sirpengi committed Oct 12, 2023
1 parent 2cf1b36 commit 9a5ec69
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
11 changes: 11 additions & 0 deletions lib/model/internal_link.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,14 @@ Uri narrowLink(PerAccountStore store, Narrow narrow, {int? nearMessageId}) {

return store.account.realmUrl.replace(fragment: fragment.toString());
}

/// Create a new `Uri` object in relation to a given realmUrl.
///
/// Returns `null` if `urlString` could not be parsed as a `Uri`.
Uri? tryResolveOnRealmUrl(String urlString, Uri realmUrl) {
try {
return realmUrl.resolve(urlString);
} on FormatException {
return null;
}
}
8 changes: 3 additions & 5 deletions lib/widgets/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import '../api/core.dart';
import '../api/model/model.dart';
import '../model/binding.dart';
import '../model/content.dart';
import '../model/internal_link.dart';
import '../model/store.dart';
import 'code_block.dart';
import 'dialog.dart';
Expand Down Expand Up @@ -661,12 +662,9 @@ void _launchUrl(BuildContext context, String urlString) async {
}

final store = PerAccountStoreWidget.of(context);
final Uri url;
try {
url = store.account.realmUrl.resolve(urlString);
} on FormatException { // TODO(log)
final url = tryResolveOnRealmUrl(urlString, store.account.realmUrl);
if (url == null) { // TODO(log)
await showError(context, null);
if (!context.mounted) return; // TODO(dart): redundant for sake of lint
return;
}

Expand Down

0 comments on commit 9a5ec69

Please sign in to comment.