Files
zulip/static/shared/js/internal_url.js
Austin Riba b39d47995d hash_util: Move stream uri functions to shared.
Moves hash_util.by_stream_uri and hash_util.by_stream_topic_uri to
internal_url so they can be used by external codebases. Due to these
functions being called in many places in the web codebase, wrappers
for both functions are left in hash_util in order to keep these
calls simple.

Also adds test for explicitly testing each function.
2022-03-01 18:14:31 -08:00

54 lines
1.7 KiB
JavaScript

const hashReplacements = new Map([
["%", "."],
["(", ".28"],
[")", ".29"],
[".", ".2E"],
]);
// Some browsers zealously URI-decode the contents of
// window.location.hash. So we hide our URI-encoding
// by replacing % with . (like MediaWiki).
export function encodeHashComponent(str) {
return encodeURIComponent(str).replace(/[%().]/g, (matched) => hashReplacements.get(matched));
}
export function decodeHashComponent(str) {
// This fails for URLs containing
// foo.foo or foo%foo due to our fault in special handling
// of such characters when encoding. This can also,
// fail independent of our fault.
// Here we let the calling code handle the exception.
return decodeURIComponent(str.replace(/\./g, "%"));
}
export function stream_id_to_slug(stream_id, maybe_get_stream_name) {
let name = maybe_get_stream_name(stream_id) || "unknown";
// The name part of the URL doesn't really matter, so we try to
// make it pretty.
name = name.replaceAll(" ", "-");
return stream_id + "-" + name;
}
export function encode_stream_id(stream_id, maybe_get_stream_name) {
// stream_id_to_slug appends the stream name, but it does not do the
// URI encoding piece.
const slug = stream_id_to_slug(stream_id, maybe_get_stream_name);
return encodeHashComponent(slug);
}
export function by_stream_uri(stream_id, maybe_get_stream_name) {
return "#narrow/stream/" + encode_stream_id(stream_id, maybe_get_stream_name);
}
export function by_stream_topic_uri(stream_id, topic, maybe_get_stream_name) {
return (
"#narrow/stream/" +
encode_stream_id(stream_id, maybe_get_stream_name) +
"/topic/" +
encodeHashComponent(topic)
);
}