Skip to content

Commit a198c84

Browse files
PIG208gnprice
authored andcommitted
internal_links: Always include a "/" after hostname
Fixes: #845 Signed-off-by: Zixuan James Li <[email protected]>
1 parent f41e98b commit a198c84

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/model/internal_link.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,13 @@ Uri narrowLink(PerAccountStore store, Narrow narrow, {int? nearMessageId}) {
9696
fragment.write('/near/$nearMessageId');
9797
}
9898

99-
return store.realmUrl.replace(fragment: fragment.toString());
99+
Uri result = store.realmUrl.replace(fragment: fragment.toString());
100+
if (result.path.isEmpty) {
101+
// Always ensure that there is a '/' right after the hostname.
102+
// A generated URL without '/' looks odd and does not linkify.
103+
result = result.replace(path: '/');
104+
}
105+
return result;
100106
}
101107

102108
/// A [Narrow] from a given URL, on `store`'s realm.

test/model/compose_test.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,28 @@ hello
314314
'#narrow/dm/1,2-dm/near/12345',
315315
'#narrow/pm-with/1,2-pm/near/12345');
316316
});
317+
318+
test('normalize links to always include a "/" after hostname', () {
319+
void checkGeneratedLink({required String realmUrl, required String expectedLink}) {
320+
final account = eg.selfAccount.copyWith(realmUrl: Uri.parse(realmUrl));
321+
final store = eg.store(account: account);
322+
check(narrowLink(store, const CombinedFeedNarrow()).toString())
323+
.equals(expectedLink);
324+
}
325+
326+
checkGeneratedLink(
327+
realmUrl: 'http://chat.example.com',
328+
expectedLink: 'http://chat.example.com/#narrow');
329+
checkGeneratedLink(
330+
realmUrl: 'http://chat.example.com/',
331+
expectedLink: 'http://chat.example.com/#narrow');
332+
checkGeneratedLink(
333+
realmUrl: 'http://chat.example.com/path',
334+
expectedLink: 'http://chat.example.com/path#narrow');
335+
checkGeneratedLink(
336+
realmUrl: 'http://chat.example.com/path/',
337+
expectedLink: 'http://chat.example.com/path/#narrow');
338+
});
317339
});
318340

319341
group('mention', () {

0 commit comments

Comments
 (0)