From 32f9ee7a62cdeb9cca5b694dc0fdb805dbcbff87 Mon Sep 17 00:00:00 2001 From: Kislay Verma Date: Sat, 25 Jan 2025 17:21:02 +0530 Subject: [PATCH] topic_link_util: Add `[` and `]` as characters to escape. These characters cause the fallback markdown links produced (#30071) to be broken. Broken links are not produced when these are present in `#**channel>topic**` syntax. It is only a problem with the fallback markdown links. --- web/src/topic_link_util.ts | 6 +++++- web/tests/topic_link_util.test.cjs | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/web/src/topic_link_util.ts b/web/src/topic_link_util.ts index 0fb484a33f..06b7778a7e 100644 --- a/web/src/topic_link_util.ts +++ b/web/src/topic_link_util.ts @@ -4,7 +4,7 @@ import * as internal_url from "../shared/src/internal_url.ts"; import * as stream_data from "./stream_data.ts"; -const invalid_stream_topic_regex = /[`>*&]|(\$\$)/g; +const invalid_stream_topic_regex = /[`>*&[\]]|(\$\$)/g; export function will_produce_broken_stream_topic_link(word: string): boolean { return invalid_stream_topic_regex.test(word); @@ -22,6 +22,10 @@ export function escape_invalid_stream_topic_characters(text: string): string { return "&"; case "$$": return "$$"; + case "[": + return "["; + case "]": + return "]"; default: return text; } diff --git a/web/tests/topic_link_util.test.cjs b/web/tests/topic_link_util.test.cjs index 6e2f46f7ba..60a823a930 100644 --- a/web/tests/topic_link_util.test.cjs +++ b/web/tests/topic_link_util.test.cjs @@ -32,9 +32,18 @@ const dollar_stream = { type: "stream", }; +const markdown_stream = { + name: "Markdown [md]", + description: "markdown", + stream_id: 7, + subscribed: true, + type: "stream", +}; + stream_data.add_sub(sweden_stream); stream_data.add_sub(denmark_stream); stream_data.add_sub(dollar_stream); +stream_data.add_sub(markdown_stream); run_test("stream_topic_link_syntax_test", () => { assert.equal( @@ -78,11 +87,24 @@ run_test("stream_topic_link_syntax_test", () => { topic_link_util.get_fallback_markdown_link("$$MONEY$$"), "[#$$MONEY$$](#narrow/channel/6-.24.24MONEY.24.24)", ); + assert.equal( + topic_link_util.get_fallback_markdown_link("Markdown [md]"), + "[#Markdown [md]](#narrow/channel/7-Markdown-.5Bmd.5D)", + ); assert.equal( topic_link_util.get_stream_topic_link_syntax("Sweden", "&ab"), "[#Sweden > &ab](#narrow/channel/1-Sweden/topic/.26ab)", ); + assert.equal( + topic_link_util.get_stream_topic_link_syntax("Sweden", "&ab]"), + "[#Sweden > &ab]](#narrow/channel/1-Sweden/topic/.26ab.5D)", + ); + + assert.equal( + topic_link_util.get_stream_topic_link_syntax("Sweden", "&a[b"), + "[#Sweden > &a[b](#narrow/channel/1-Sweden/topic/.26a.5Bb)", + ); // Only for full coverage of the module. assert.equal(topic_link_util.escape_invalid_stream_topic_characters("Sweden"), "Sweden");