From 1ece5ad15e964711a366f8468c85e2b9c6aa9724 Mon Sep 17 00:00:00 2001 From: Kislay Verma Date: Wed, 21 May 2025 13:19:44 +0530 Subject: [PATCH] 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. --- web/src/topic_link_util.ts | 6 ++++-- web/tests/topic_link_util.test.cjs | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/web/src/topic_link_util.ts b/web/src/topic_link_util.ts index 4502ba74cc..cac23899de 100644 --- a/web/src/topic_link_util.ts +++ b/web/src/topic_link_util.ts @@ -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, }; } diff --git a/web/tests/topic_link_util.test.cjs b/web/tests/topic_link_util.test.cjs index d30faaf0aa..5492432793 100644 --- a/web/tests/topic_link_util.test.cjs +++ b/web/tests/topic_link_util.test.cjs @@ -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(