From 327647f4f8e7449f00ffbebb4102b31b7b660919 Mon Sep 17 00:00:00 2001 From: Kislay Udbhav Verma Date: Wed, 9 Oct 2024 00:47:19 +0530 Subject: [PATCH] copy_and_paste: Paste fallback md link if syntax link will be broken. If we paste a stream-topic URL that can be formatted as per #29302, we now generate a normal markdown link if the stream topic syntax could result in a broken link. Fixes #31904 --- web/src/copy_and_paste.ts | 7 +++++-- web/tests/copy_and_paste.test.js | 31 ++++++++++++++++++++++++++----- 2 files changed, 31 insertions(+), 7 deletions(-) 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) {