Skip to content

Commit f3f3f1f

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

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/model/internal_link.dart

Lines changed: 8 additions & 1 deletion
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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,4 +451,22 @@ void main() {
451451
});
452452
});
453453
});
454+
455+
group('narrowLink', () {
456+
test('normalize links to always include a "/" after hostname', () {
457+
String narrowLinkFor({required String realmUrl}) {
458+
final store = eg.store(account: eg.selfAccount.copyWith(realmUrl: Uri.parse(realmUrl)));
459+
return narrowLink(store, const CombinedFeedNarrow()).toString();
460+
}
461+
462+
check(narrowLinkFor(realmUrl: 'http://chat.example.com'))
463+
.equals( 'http://chat.example.com/#narrow');
464+
check(narrowLinkFor(realmUrl: 'http://chat.example.com/'))
465+
.equals( 'http://chat.example.com/#narrow');
466+
check(narrowLinkFor(realmUrl: 'http://chat.example.com/path'))
467+
.equals( 'http://chat.example.com/path#narrow');
468+
check(narrowLinkFor(realmUrl: 'http://chat.example.com/path/'))
469+
.equals( 'http://chat.example.com/path/#narrow');
470+
});
471+
});
454472
}

0 commit comments

Comments
 (0)