Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

composebox_typeahead: Avoid generating broken stream-topic links.

Kislay Verma84cd835a8cd4
diff --git a/web/src/composebox_typeahead.ts b/web/src/composebox_typeahead.ts
index a0812384a0d..84da4884d39 100644
--- a/web/src/composebox_typeahead.ts
+++ b/web/src/composebox_typeahead.ts
@@ -32,6 +32,7 @@ import type {StreamPillData} from "./stream_pill";
import * as stream_topic_history from "./stream_topic_history";
import * as stream_topic_history_util from "./stream_topic_history_util";
import * as timerender from "./timerender";
+import * as topic_link_util from "./topic_link_util";
import * as typeahead_helper from "./typeahead_helper";
import type {UserOrMentionPillData} from "./typeahead_helper";
import type {UserGroupPillData} from "./user_group_pill";
@@ -1088,14 +1089,23 @@ export function content_typeahead_selected(
if (beginning.endsWith("#*")) {
beginning = beginning.slice(0, -2);
}
- beginning += "#**" + item.name;
if (event && event.key === ">") {
// Normally, one accepts typeahead with `Tab` or `Enter`, but when completing
// stream typeahead, we allow `>`, the delimiter for stream+topic mentions,
// as a completion that automatically sets up stream+topic typeahead for you.
- beginning += ">";
+
+ // Even if the stream name produces a broken link, we'll go with the #** syntax
+ // here since we are dealing with it along with the topic name later.
+ beginning += "#**" + item.name + ">";
} else {
- beginning += "** ";
+ // for stream links, we use markdown link syntax if the #**stream** syntax
+ // will generate a broken url.
+ if (topic_link_util.will_produce_broken_stream_topic_link(item.name)) {
+ // use markdown link syntax
+ beginning += topic_link_util.get_fallback_markdown_link(item.name);
+ } else {
+ beginning += "#**" + item.name + "** ";
+ }
}
compose_validate.warn_if_private_stream_is_linked(item, $textbox);
break;
@@ -1132,11 +1142,19 @@ export function content_typeahead_selected(
break;
}
case "topic_list": {
- // Stream + topic mention typeahead; close the stream+topic mention syntax
- // with the topic and the final **. Note that token.length can be 0
- // if we are completing from `**streamname>`.
- const start = beginning.length - token.length;
- beginning = beginning.slice(0, start) + item.topic + "** ";
+ // Stream + topic mention typeahead; close the stream+topic mention syntax with
+ // the topic and the final ** or replace it with markdown link syntax if topic name
+ // will cause encoding issues.
+ // "beginning" contains all the text before the cursor, so we use lastIndexOf to
+ // avoid any other stream+topic mentions in the message.
+ const syntax_start_index = beginning.lastIndexOf("#**");
+ beginning =
+ beginning.slice(0, syntax_start_index) +
+ topic_link_util.get_stream_topic_link_syntax(
+ beginning.slice(syntax_start_index),
+ item.topic,
+ ) +
+ " ";
break;
}
case "time_jump": {
diff --git a/web/src/topic_link_util.ts b/web/src/topic_link_util.ts
new file mode 100644
index 00000000000..f56c9146228
--- /dev/null
+++ b/web/src/topic_link_util.ts
@@ -0,0 +1,63 @@
+import assert from "minimalistic-assert";
+
+import * as internal_url from "../shared/src/internal_url";
+
+import * as stream_data from "./stream_data";
+
+const invalid_stream_topic_regex = /[*>`]|(\$\$)/g;
+
+export function will_produce_broken_stream_topic_link(word: string): boolean {
+ return invalid_stream_topic_regex.test(word);
+}
+
+function get_stream_name_from_topic_link_syntax(syntax: string): string {
+ const start = syntax.indexOf("#**");
+ const end = syntax.lastIndexOf(">");
+ return syntax.slice(start + 3, end);
+}
+
+export function escape_invalid_stream_topic_characters(text: string): string {
+ switch (text) {
+ case "`":
+ return "`";
+ case ">":
+ return ">";
+ case "*":
+ return "*";
+ case "$$":
+ return "$$";
+ default:
+ return text;
+ }
+}
+
+export function html_escape_markdown_syntax_characters(text: string): string {
+ return text.replaceAll(invalid_stream_topic_regex, escape_invalid_stream_topic_characters);
+}
+
+export function get_fallback_markdown_link(stream_name: string, topic_name?: string): string {
+ const stream = stream_data.get_sub(stream_name);
+ const stream_id = stream?.stream_id;
+ assert(stream_id !== undefined);
+ const escape = html_escape_markdown_syntax_characters;
+ if (topic_name !== undefined) {
+ return `[#${escape(stream_name)}>${escape(topic_name)}](${internal_url.by_stream_topic_url(stream_id, topic_name, () => stream_name)})`;
+ }
+ return `[#${escape(stream_name)}](${internal_url.by_stream_url(stream_id, () => stream_name)})`;
+}
+
+export function get_stream_topic_link_syntax(
+ typed_syntax_text: string,
+ topic_name: string,
+): string {
+ const stream_name = get_stream_name_from_topic_link_syntax(typed_syntax_text);
+ // If the topic name is such that it will generate an invalid #**stream>topic** syntax,
+ // we revert to generating the normal markdown syntax for a link.
+ if (
+ will_produce_broken_stream_topic_link(topic_name) ||
+ will_produce_broken_stream_topic_link(stream_name)
+ ) {
+ return get_fallback_markdown_link(stream_name, topic_name);
+ }
+ return `#**${stream_name}>${topic_name}**`;
+}
diff --git a/web/tests/topic_link_util.test.js b/web/tests/topic_link_util.test.js
new file mode 100644
index 00000000000..9c537a5b0d3
--- /dev/null
+++ b/web/tests/topic_link_util.test.js
@@ -0,0 +1,88 @@
+"use strict";
+
+const {strict: assert} = require("assert");
+
+const {zrequire} = require("./lib/namespace");
+const {run_test} = require("./lib/test");
+
+const topic_link_util = zrequire("topic_link_util");
+const stream_data = zrequire("stream_data");
+
+const sweden_stream = {
+ name: "Sweden",
+ description: "Cold, mountains and home decor.",
+ stream_id: 1,
+ subscribed: true,
+ type: "stream",
+};
+
+const denmark_stream = {
+ name: "Denmark",
+ description: "Vikings and boats, in a serene and cold weather.",
+ stream_id: 2,
+ subscribed: true,
+ type: "stream",
+};
+
+const dollar_stream = {
+ name: "$$MONEY$$",
+ description: "Money money money",
+ stream_id: 6,
+ subscribed: true,
+ type: "stream",
+};
+
+stream_data.add_sub(sweden_stream);
+stream_data.add_sub(denmark_stream);
+stream_data.add_sub(dollar_stream);
+
+run_test("stream_topic_link_syntax_test", () => {
+ assert.equal(
+ topic_link_util.get_stream_topic_link_syntax("#**Sweden>", "topic"),
+ "#**Sweden>topic**",
+ );
+ assert.equal(
+ topic_link_util.get_stream_topic_link_syntax("#**Sweden>to", "topic"),
+ "#**Sweden>topic**",
+ );
+ assert.equal(
+ topic_link_util.get_stream_topic_link_syntax("#**Sweden>t", "test `test` test"),
+ "[#Sweden>test `test` test](#narrow/stream/1-Sweden/topic/test.20.60test.60.20test)",
+ );
+ assert.equal(
+ topic_link_util.get_stream_topic_link_syntax("#**Denmark>t", "test `test` test`s"),
+ "[#Denmark>test `test` test`s](#narrow/stream/2-Denmark/topic/test.20.60test.60.20test.60s)",
+ );
+ assert.equal(
+ topic_link_util.get_stream_topic_link_syntax("#**Sweden>typeah", "error due to *"),
+ "[#Sweden>error due to *](#narrow/stream/1-Sweden/topic/error.20due.20to.20*)",
+ );
+ assert.equal(
+ topic_link_util.get_stream_topic_link_syntax("#**Sweden>t", "*asterisk"),
+ "[#Sweden>*asterisk](#narrow/stream/1-Sweden/topic/*asterisk)",
+ );
+ assert.equal(
+ topic_link_util.get_stream_topic_link_syntax("#**Sweden>gibberish", "greaterthan>"),
+ "[#Sweden>greaterthan>](#narrow/stream/1-Sweden/topic/greaterthan.3E)",
+ );
+ assert.equal(
+ topic_link_util.get_stream_topic_link_syntax("#**$$MONEY$$>t", "dollar"),
+ "[#$$MONEY$$>dollar](#narrow/stream/6-.24.24MONEY.24.24/topic/dollar)",
+ );
+ assert.equal(
+ topic_link_util.get_stream_topic_link_syntax("#**Sweden>t", "swe$$dish"),
+ "[#Sweden>swe$$dish](#narrow/stream/1-Sweden/topic/swe.24.24dish)",
+ );
+ assert.equal(
+ topic_link_util.get_fallback_markdown_link("Sweden"),
+ "[#Sweden](#narrow/stream/1-Sweden)",
+ );
+
+ assert.equal(
+ topic_link_util.get_fallback_markdown_link("$$MONEY$$"),
+ "[#$$MONEY$$](#narrow/stream/6-.24.24MONEY.24.24)",
+ );
+
+ // Only for full coverage of the module.
+ assert.equal(topic_link_util.escape_invalid_stream_topic_characters("Sweden"), "Sweden");
+});