diff --git a/web/src/copy_and_paste.ts b/web/src/copy_and_paste.ts index 051403bd5a..892444a053 100644 --- a/web/src/copy_and_paste.ts +++ b/web/src/copy_and_paste.ts @@ -666,18 +666,21 @@ export function try_stream_topic_syntax_text(text: string): string | null { return null; } + // Now we're sure that the URL is a valid stream topic URL. + // But the produced #**stream>topic** syntax could be broken. + const stream = stream_data.get_sub_by_id(stream_topic.stream_id); assert(stream !== undefined); const stream_name = stream.name; if (topic_link_util.will_produce_broken_stream_topic_link(stream_name)) { - return null; + return topic_link_util.get_fallback_markdown_link(stream_name, stream_topic.topic_name); } if ( stream_topic.topic_name !== undefined && topic_link_util.will_produce_broken_stream_topic_link(stream_topic.topic_name) ) { - return null; + return topic_link_util.get_fallback_markdown_link(stream_name, stream_topic.topic_name); } let syntax_text = "#**" + stream_name; diff --git a/web/tests/copy_and_paste.test.js b/web/tests/copy_and_paste.test.js index 449151bec7..98674b0aaf 100644 --- a/web/tests/copy_and_paste.test.js +++ b/web/tests/copy_and_paste.test.js @@ -12,6 +12,10 @@ stream_data.add_sub({ stream_id: 4, name: "Rome", }); +stream_data.add_sub({ + stream_id: 5, + name: "Romeo`s lair", +}); run_test("try_stream_topic_syntax_text", () => { const test_cases = [ @@ -41,11 +45,28 @@ run_test("try_stream_topic_syntax_text", () => { ["http://zulip.zulipdev.com/#narrow/topic/cheese"], ["http://zulip.zulipdev.com/#narrow/topic/pizza/stream/Rome"], - // characters which are known to produce broken #**stream>topic** urls. - ["http://zulip.zulipdev.com/#narrow/channel/4-Rome/topic/100.25.20profits.60"], - ["http://zulip.zulipdev.com/#narrow/channel/4-Rome/topic/100.25.20*profits"], - ["http://zulip.zulipdev.com/#narrow/channel/4-Rome/topic/.24.24 100.25.20profits"], - ["http://zulip.zulipdev.com/#narrow/channel/4-Rome/topic/>100.25.20profits"], + // When a url containing characters which are known to produce broken + // #**stream>topic** urls is pasted, a normal markdown link syntax is produced. + [ + "http://zulip.zulipdev.com/#narrow/stream/4-Rome/topic/100.25.20profits.60", + "[#Rome>100% profits`](#narrow/channel/4-Rome/topic/100.25.20profits.60)", + ], + [ + "http://zulip.zulipdev.com/#narrow/stream/4-Rome/topic/100.25.20*profits", + "[#Rome>100% *profits](#narrow/channel/4-Rome/topic/100.25.20*profits)", + ], + [ + "http://zulip.zulipdev.com/#narrow/stream/4-Rome/topic/.24.24 100.25.20profits", + "[#Rome>$$ 100% profits](#narrow/channel/4-Rome/topic/.24.24.20100.25.20profits)", + ], + [ + "http://zulip.zulipdev.com/#narrow/stream/4-Rome/topic/>100.25.20profits", + "[#Rome>>100% profits](#narrow/channel/4-Rome/topic/.3E100.25.20profits)", + ], + [ + "http://zulip.zulipdev.com/#narrow/stream/5-Romeo.60s-lair/topic/normal", + "[#Romeo`s lair>normal](#narrow/channel/5-Romeo.60s-lair/topic/normal)", + ], ]; for (const test_case of test_cases) {