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
This commit is contained in:
Kislay Udbhav Verma 2024-10-09 00:47:19 +05:30 committed by Tim Abbott
parent da4443f392
commit 327647f4f8
2 changed files with 31 additions and 7 deletions

View File

@ -666,18 +666,21 @@ export function try_stream_topic_syntax_text(text: string): string | null {
return 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); const stream = stream_data.get_sub_by_id(stream_topic.stream_id);
assert(stream !== undefined); assert(stream !== undefined);
const stream_name = stream.name; const stream_name = stream.name;
if (topic_link_util.will_produce_broken_stream_topic_link(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 ( if (
stream_topic.topic_name !== undefined && stream_topic.topic_name !== undefined &&
topic_link_util.will_produce_broken_stream_topic_link(stream_topic.topic_name) 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; let syntax_text = "#**" + stream_name;

View File

@ -12,6 +12,10 @@ stream_data.add_sub({
stream_id: 4, stream_id: 4,
name: "Rome", name: "Rome",
}); });
stream_data.add_sub({
stream_id: 5,
name: "Romeo`s lair",
});
run_test("try_stream_topic_syntax_text", () => { run_test("try_stream_topic_syntax_text", () => {
const test_cases = [ 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/cheese"],
["http://zulip.zulipdev.com/#narrow/topic/pizza/stream/Rome"], ["http://zulip.zulipdev.com/#narrow/topic/pizza/stream/Rome"],
// characters which are known to produce broken #**stream>topic** urls. // When a url containing characters which are known to produce broken
["http://zulip.zulipdev.com/#narrow/channel/4-Rome/topic/100.25.20profits.60"], // #**stream>topic** urls is pasted, a normal markdown link syntax is produced.
["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/stream/4-Rome/topic/100.25.20profits.60",
["http://zulip.zulipdev.com/#narrow/channel/4-Rome/topic/>100.25.20profits"], "[#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) { for (const test_case of test_cases) {