Skip to content

Commit

Permalink
internal_links: Always include a "/" after hostname
Browse files Browse the repository at this point in the history
Fixes: #845

Signed-off-by: Zixuan James Li <[email protected]>
  • Loading branch information
PIG208 committed Nov 13, 2024
1 parent b4b0932 commit 05bb739
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/model/internal_link.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,14 @@ Uri narrowLink(PerAccountStore store, Narrow narrow, {int? nearMessageId}) {
fragment.write('/near/$nearMessageId');
}

return store.realmUrl.replace(fragment: fragment.toString());
Uri result = store.realmUrl.replace(fragment: fragment.toString());
if (result.path.isEmpty) {
// Always ensure that there is a '/' right after the hostname.
// A generated URL without '/' looks odd and does not linkify.
result = result.replace(path: '/');
}
assert(result.path.startsWith('/'));
return result;
}

/// A [Narrow] from a given URL, on `store`'s realm.
Expand Down
22 changes: 22 additions & 0 deletions test/model/compose_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,28 @@ hello
'#narrow/dm/1,2-dm/near/12345',
'#narrow/pm-with/1,2-pm/near/12345');
});

test('normalize links to always include a "/" after hostname', () {
void checkGeneratedLink({required String realmUrl, required String expectedLink}) {
final account = eg.selfAccount.copyWith(realmUrl: Uri.parse(realmUrl));
final store = eg.store(account: account);
check(narrowLink(store, const CombinedFeedNarrow()))
.equals(Uri.parse(expectedLink));
}

checkGeneratedLink(
realmUrl: 'http://chat.example.com',
expectedLink: 'http://chat.example.com/#narrow');
checkGeneratedLink(
realmUrl: 'http://chat.example.com/',
expectedLink: 'http://chat.example.com/#narrow');
checkGeneratedLink(
realmUrl: 'http://chat.example.com/path',
expectedLink: 'http://chat.example.com/path#narrow');
checkGeneratedLink(
realmUrl: 'http://chat.example.com/path/',
expectedLink: 'http://chat.example.com/path/#narrow');
});
});

group('mention', () {
Expand Down

0 comments on commit 05bb739

Please sign in to comment.