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.

markdown: Match topic-link rendering in the web app local echo.

Shubham Padiac367b8792892
diff --git a/web/src/markdown.ts b/web/src/markdown.ts
index 43762fdcdb6..bf23cf93845 100644
--- a/web/src/markdown.ts
+++ b/web/src/markdown.ts
@@ -79,6 +79,7 @@ export type MarkdownHelpers = {
// stream hashes
get_stream_by_name: (stream_name: string) => {stream_id: number; name: string} | undefined;
+ is_empty_topic_only_channel: (stream_id: number) => boolean;
stream_hash: (stream_id: number) => string;
stream_topic_hash: (stream_id: number, topic: string) => string;
@@ -602,6 +603,7 @@ function handleStreamTopic({
stream_name,
topic,
get_stream_by_name,
+ is_empty_topic_only_channel,
stream_topic_hash,
}: {
stream_name: string;
@@ -612,12 +614,18 @@ function handleStreamTopic({
name: string;
}
| undefined;
+ is_empty_topic_only_channel: (stream_id: number) => boolean;
stream_topic_hash: (stream_id: number, topic: string) => string;
}): string | undefined {
const stream = get_stream_by_name(stream_name);
if (stream === undefined) {
return undefined;
}
+ if (topic !== "" && is_empty_topic_only_channel(stream.stream_id)) {
+ // This channel only allows the empty topic, so a non-empty topic
+ // can never exist in it; don't linkify it.
+ return undefined;
+ }
const href = stream_topic_hash(stream.stream_id, topic);
const topic_display_name_html = _.escape(util.get_final_topic_display_name(topic));
return render_topic_link({
@@ -634,6 +642,7 @@ function handleStreamTopicMessage({
topic,
message_id,
get_stream_by_name,
+ is_empty_topic_only_channel,
stream_topic_hash,
}: {
stream_name: string;
@@ -645,12 +654,18 @@ function handleStreamTopicMessage({
name: string;
}
| undefined;
+ is_empty_topic_only_channel: (stream_id: number) => boolean;
stream_topic_hash: (stream_id: number, topic: string) => string;
}): string | undefined {
const stream = get_stream_by_name(stream_name);
if (stream === undefined) {
return undefined;
}
+ if (topic !== "" && is_empty_topic_only_channel(stream.stream_id)) {
+ // This channel only allows the empty topic, so a non-empty topic
+ // can never exist in it; don't linkify it.
+ return undefined;
+ }
const href = stream_topic_hash(stream.stream_id, topic) + "/near/" + message_id;
const topic_display_name_html = _.escape(util.get_final_topic_display_name(topic));
return render_channel_message_link({
@@ -767,6 +782,7 @@ export function parse({
stream_name,
topic,
get_stream_by_name: helper_config.get_stream_by_name,
+ is_empty_topic_only_channel: helper_config.is_empty_topic_only_channel,
stream_topic_hash: helper_config.stream_topic_hash,
});
}
@@ -781,6 +797,7 @@ export function parse({
topic,
message_id,
get_stream_by_name: helper_config.get_stream_by_name,
+ is_empty_topic_only_channel: helper_config.is_empty_topic_only_channel,
stream_topic_hash: helper_config.stream_topic_hash,
});
}
diff --git a/web/src/markdown_config.ts b/web/src/markdown_config.ts
index 08463bceea9..0c0feac7089 100644
--- a/web/src/markdown_config.ts
+++ b/web/src/markdown_config.ts
@@ -76,6 +76,7 @@ export const get_helpers = (): MarkdownHelpers => ({
// stream hashes
get_stream_by_name: (name) => stream(stream_data.get_sub(name)),
+ is_empty_topic_only_channel: stream_data.is_empty_topic_only_channel,
stream_hash: hash_util.channel_url_by_user_setting,
stream_topic_hash: hash_util.by_stream_topic_url,
diff --git a/web/tests/markdown_parse.test.cjs b/web/tests/markdown_parse.test.cjs
index f46d937ce46..9dd29395870 100644
--- a/web/tests/markdown_parse.test.cjs
+++ b/web/tests/markdown_parse.test.cjs
@@ -5,11 +5,15 @@ const assert = require("node:assert/strict");
const {RE2JS} = require("re2js");
const Template = require("uri-template-lite");
+const {make_realm} = require("./lib/example_realm.cjs");
const {zrequire} = require("./lib/namespace.cjs");
const {run_test} = require("./lib/test.cjs");
const markdown = zrequire("markdown");
const linkifiers = zrequire("linkifiers");
+const {set_realm} = zrequire("state_data");
+
+set_realm(make_realm({realm_empty_topic_display_name: "general chat"}));
const my_id = 101;
@@ -67,12 +71,24 @@ const social = {
name: "social",
};
-const sub_map = new Map([[social.name, social]]);
+const empty_topic_only_channel = {
+ stream_id: 302,
+ name: "empty_topic_only_channel",
+};
+
+const sub_map = new Map([
+ [social.name, social],
+ [empty_topic_only_channel.name, empty_topic_only_channel],
+]);
function get_stream_by_name(name) {
return sub_map.get(name);
}
+function is_empty_topic_only_channel(stream_id) {
+ return stream_id === empty_topic_only_channel.stream_id;
+}
+
function stream_hash(stream_id) {
return `stream-${stream_id}`;
}
@@ -146,6 +162,7 @@ const helper_config = {
// stream hashes
get_stream_by_name,
+ is_empty_topic_only_channel,
stream_hash,
stream_topic_hash,
@@ -207,6 +224,31 @@ run_test("stream links", () => {
);
});
+run_test("empty-topic-only channel topic links", () => {
+ // A channel that only allows the empty topic can never contain a
+ // non-empty topic, so a non-empty topic link is left as plain text
+ // rather than linkified.
+ assert_parse(
+ "#**empty_topic_only_channel>lunch**",
+ "<p>#**empty_topic_only_channel&gt;lunch**</p>",
+ );
+ assert_parse(
+ "#**empty_topic_only_channel>lunch@12**",
+ "<p>#**empty_topic_only_channel&gt;lunch@12**</p>",
+ );
+ // The empty topic is still linkified, since it's the one topic
+ // such a channel can contain.
+ assert_parse(
+ "#**empty_topic_only_channel>**",
+ '<p><a class="stream-topic" data-stream-id="302" href="stream-302-topic-">#empty_topic_only_channel &gt; <span class="empty-topic-display">translated: general chat</span></a></p>',
+ );
+ // The channel link itself is still linkified.
+ assert_parse(
+ "#**empty_topic_only_channel**",
+ '<p><a class="stream" data-stream-id="302" href="/stream-302">#empty_topic_only_channel</a></p>',
+ );
+});
+
run_test("emojis", () => {
assert_parse(
"yup :)",