topic_link_util: Add support for empty topics in the frontend.

Though the frontend module generated correct urls for
empty topics, it would sometime generate incorrect
link text in the fallback md link syntax. (eg, if
empty string was provided as the topic name).

This commit fixes that.
This commit is contained in:
Kislay Verma
2025-05-21 13:19:44 +05:30
committed by Tim Abbott
parent 2a120d2717
commit 1ece5ad15e
2 changed files with 24 additions and 2 deletions

View File

@@ -4,6 +4,7 @@ import assert from "minimalistic-assert";
import * as hash_util from "./hash_util.ts";
import * as stream_data from "./stream_data.ts";
import * as util from "./util.ts";
const invalid_stream_topic_regex = /[`>*&[\]]|(\$\$)/g;
@@ -64,14 +65,15 @@ export function get_topic_link_content(
const escape = html_escape_markdown_syntax_characters;
if (topic_name !== undefined) {
const stream_topic_url = hash_util.by_stream_topic_url(stream_id, topic_name);
const topic_display_name = util.get_final_topic_display_name(topic_name);
if (message_id !== undefined) {
return {
text: `#${escape(stream_name)} > ${escape(topic_name)} @ 💬`,
text: `#${escape(stream_name)} > ${escape(topic_display_name)} @ 💬`,
url: `${stream_topic_url}/near/${message_id}`,
};
}
return {
text: `#${escape(stream_name)} > ${escape(topic_name)}`,
text: `#${escape(stream_name)} > ${escape(topic_display_name)}`,
url: stream_topic_url,
};
}

View File

@@ -13,6 +13,9 @@ mock_esm("../src/user_settings", {
web_channel_default_view: settings_config.web_channel_default_view_values.channel_feed.code,
},
});
mock_esm("../src/state_data", {
realm: {realm_empty_topic_display_name: "general chat"},
});
const sweden_stream = {
name: "Sweden",
@@ -125,6 +128,23 @@ run_test("stream_topic_link_syntax_test", () => {
"[#Sweden > &a[b](#narrow/channel/1-Sweden/topic/.26a.5Bb)",
);
assert.equal(topic_link_util.get_stream_topic_link_syntax("Sweden", ""), "#**Sweden>**");
assert.equal(
topic_link_util.get_stream_topic_link_syntax("$$MONEY$$", ""),
"[#$$MONEY$$ > translated: general chat](#narrow/channel/6-.24.24MONEY.24.24/topic/)",
);
assert.equal(
topic_link_util.get_fallback_markdown_link("Sweden", "abc", 123),
"[#Sweden > abc @ 💬](#narrow/channel/1-Sweden/topic/abc/near/123)",
);
assert.equal(
topic_link_util.get_fallback_markdown_link("Sweden", "", 123),
"[#Sweden > translated: general chat @ 💬](#narrow/channel/1-Sweden/topic//near/123)",
);
// Only for full coverage of the module.
assert.equal(topic_link_util.escape_invalid_stream_topic_characters("Sweden"), "Sweden");
assert.equal(