Skip to content

Commit 03c7c5a

Browse files
committed
internal_links: Always include a "/" after hostname
Fixes: #845 Signed-off-by: Zixuan James Li <[email protected]>
1 parent 643b902 commit 03c7c5a

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/model/internal_link.dart

+8-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,14 @@ 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,
103+
// and if used in a Zulip message does not get automatically linkified.
104+
result = result.replace(path: '/');
105+
}
106+
return result;
100107
}
101108

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

test/model/internal_link_test.dart

+16
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,22 @@ void main() {
127127
'#narrow/dm/1,2-dm/near/12345',
128128
'#narrow/pm-with/1,2-pm/near/12345');
129129
});
130+
131+
test('normalize links to always include a "/" after hostname', () {
132+
String narrowLinkFor({required String realmUrl}) {
133+
final store = eg.store(account: eg.selfAccount.copyWith(realmUrl: Uri.parse(realmUrl)));
134+
return narrowLink(store, const CombinedFeedNarrow()).toString();
135+
}
136+
137+
check(narrowLinkFor(realmUrl: 'http://chat.example.com'))
138+
.equals( 'http://chat.example.com/#narrow');
139+
check(narrowLinkFor(realmUrl: 'http://chat.example.com/'))
140+
.equals( 'http://chat.example.com/#narrow');
141+
check(narrowLinkFor(realmUrl: 'http://chat.example.com/path'))
142+
.equals( 'http://chat.example.com/path#narrow');
143+
check(narrowLinkFor(realmUrl: 'http://chat.example.com/path/'))
144+
.equals( 'http://chat.example.com/path/#narrow');
145+
});
130146
});
131147

132148
final realmUrl = Uri.parse('https://example.com/');

0 commit comments

Comments
 (0)