Skip to content

Commit fd91f72

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

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-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

+17
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,23 @@ 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(
134+
account: eg.account(user: eg.selfUser, realmUrl: Uri.parse(realmUrl)));
135+
return narrowLink(store, const CombinedFeedNarrow()).toString();
136+
}
137+
138+
check(narrowLinkFor(realmUrl: 'http://chat.example.com'))
139+
.equals( 'http://chat.example.com/#narrow');
140+
check(narrowLinkFor(realmUrl: 'http://chat.example.com/'))
141+
.equals( 'http://chat.example.com/#narrow');
142+
check(narrowLinkFor(realmUrl: 'http://chat.example.com/path'))
143+
.equals( 'http://chat.example.com/path#narrow');
144+
check(narrowLinkFor(realmUrl: 'http://chat.example.com/path/'))
145+
.equals( 'http://chat.example.com/path/#narrow');
146+
});
130147
});
131148

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

0 commit comments

Comments
 (0)