Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
alert_words: Highlight alert words in channel/topic links.
Amit Patilafb960c04137
diff --git a/web/src/markdown.ts b/web/src/markdown.tsindex a76382e0a9a..1363ac297d8 100644--- a/web/src/markdown.ts+++ b/web/src/markdown.ts@@ -632,10 +632,11 @@ function handleStreamTopic({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({channel_id: stream.stream_id,channel_name: stream.name,- topic_display_name: util.get_final_topic_display_name(topic),+ topic_display_name_html,is_empty_string_topic: topic === "",href,});@@ -664,9 +665,10 @@ function handleStreamTopicMessage({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({channel_name: stream.name,- topic_display_name: util.get_final_topic_display_name(topic),+ topic_display_name_html,is_empty_string_topic: topic === "",href,});diff --git a/web/src/rendered_markdown.ts b/web/src/rendered_markdown.tsindex ccd30da4017..d8f407bdffb 100644--- a/web/src/rendered_markdown.ts+++ b/web/src/rendered_markdown.ts@@ -1,6 +1,7 @@import ClipboardJS from "clipboard";import {isValid, parseISO} from "date-fns";import $ from "jquery";+import _ from "lodash";import assert from "minimalistic-assert";import render_channel_message_link from "../templates/channel_message_link.hbs";@@ -11,6 +12,7 @@ import render_markdown_timestamp from "../templates/markdown_timestamp.hbs";import render_mention_content_wrapper from "../templates/mention_content_wrapper.hbs";import render_topic_link from "../templates/topic_link.hbs";+import * as alert_words from "./alert_words.ts";import * as blueslip from "./blueslip.ts";import {show_copied_confirmation} from "./copied_tooltip.ts";import * as hash_util from "./hash_util.ts";@@ -77,12 +79,17 @@ export function update_topic_or_message_link_element(stream_info: StreamInfo,topic_name: string,narrow_url: string,+ message?: Message,): void {- const topic_display_name = util.get_final_topic_display_name(topic_name);+ let topic_display_name = _.escape(util.get_final_topic_display_name(topic_name));++ if (message?.alerted) {+ topic_display_name = alert_words.highlight_alert_words(topic_display_name);+ }const context = {channel_name: stream_info.name,- topic_display_name,+ topic_display_name_html: topic_display_name,is_empty_string_topic: topic_name === "",href: narrow_url,};@@ -288,11 +295,13 @@ export const update_elements = ($content: JQuery): void => {// TODO: Ideally, we should NOT skip this if only topic is highlighted,// but we are doing so currently.assert(channel_topic.topic_name !== undefined);+ message ??= get_message_for_message_content($content);update_topic_or_message_link_element($(this),sub,channel_topic.topic_name,narrow_url,+ message,);}});diff --git a/web/templates/channel_message_link.hbs b/web/templates/channel_message_link.hbsindex f4f4b27afe9..74283ecd84a 100644--- a/web/templates/channel_message_link.hbs+++ b/web/templates/channel_message_link.hbs@@ -5,7 +5,7 @@{{~> decorated_channel_name stream=stream inline_with_text=true show_colored_icon=false}}{{~else~}}#{{channel_name}}- {{~/if}} > <span class="empty-topic-display">{{topic_display_name}}</span> @ 💬+ {{~/if}} > <span class="empty-topic-display">{{{topic_display_name_html}}}</span> @ 💬{{~!-- squash whitespace --~}}</a>{{~else}}@@ -15,7 +15,7 @@{{~> decorated_channel_name stream=stream inline_with_text=true show_colored_icon=false}}{{~else~}}#{{channel_name}}- {{~/if}} > {{topic_display_name}} @ 💬+ {{~/if}} > {{{topic_display_name_html}}} @ 💬{{~!-- squash whitespace --~}}</a>{{~/if}}diff --git a/web/templates/topic_link.hbs b/web/templates/topic_link.hbsindex 9db585d8c97..16e18ede57a 100644--- a/web/templates/topic_link.hbs+++ b/web/templates/topic_link.hbs@@ -5,7 +5,7 @@{{~> decorated_channel_name stream=stream inline_with_text=true show_colored_icon=false}}{{~else~}}#{{channel_name}}- {{~/if}} > <span class="empty-topic-display">{{topic_display_name}}</span>+ {{~/if}} > <span class="empty-topic-display">{{{topic_display_name_html}}}</span>{{~!-- squash whitespace --~}}</a>{{~else}}@@ -15,7 +15,7 @@{{~> decorated_channel_name stream=stream inline_with_text=true show_colored_icon=false}}{{~else~}}#{{channel_name}}- {{~/if}} > {{topic_display_name}}+ {{~/if}} > {{{topic_display_name_html}}}{{~!-- squash whitespace --~}}</a>{{~/if}}diff --git a/web/tests/rendered_markdown.test.cjs b/web/tests/rendered_markdown.test.cjsindex 80816e03f45..37af96d8df7 100644--- a/web/tests/rendered_markdown.test.cjs+++ b/web/tests/rendered_markdown.test.cjs@@ -29,6 +29,7 @@ mock_cjs("clipboard", Clipboard);const realm_playground = mock_esm("../src/realm_playground");const copied_tooltip = mock_esm("../src/copied_tooltip");+const alert_words = zrequire("alert_words");const rm = zrequire("rendered_markdown");const people = zrequire("people");const user_groups = zrequire("user_groups");@@ -472,13 +473,102 @@ run_test("stream-links", ({mock_template}) => {channel_id: stream.stream_id,stream,channel_name: stream.name,- topic_display_name: "topic name > still the topic name",+ topic_display_name_html: "topic name > still the topic name",is_empty_string_topic: false,href: `/#narrow/channel/${stream.stream_id}-random/topic/topic.20name.20.3E.20still.20the.20topic.20name`,});assert.ok(!topic_link_rendered_html.includes("empty-topic-display"));});+run_test("stream-links alert words", ({mock_template}) => {+ // Setup+ const $content = get_content_element();+ const $stream = $.create("a.stream");+ $stream.set_find_results(".highlight", []);+ $stream.attr("data-stream-id", stream.stream_id);++ const $stream_topic = $.create("a.stream-topic");+ $stream_topic.set_find_results(".highlight", []);+ $stream_topic.attr(+ "href",+ `/#narrow/channel/${stream.stream_id}-test/topic/important.20alert.20topic`,+ );++ $stream_topic[0].replaceWith = noop;+ $stream_topic.addClass("stream-topic");+ $stream_topic.text("#test alert > important alert topic");++ $content.set_find_results("a.stream", $stream);+ $content.set_find_results("a.stream-topic, a.message-link", $stream_topic);++ let stream_name_context;+ mock_template("decorated_channel_name.hbs", true, (data, html) => {+ stream_name_context = data;+ return html;+ });++ let topic_link_context;+ mock_template("topic_link.hbs", true, (data, html) => {+ topic_link_context = data;+ return html;+ });++ const message = {alerted: true};+ set_message_for_message_content($content, message);+ alert_words.set_words(["alert"]);++ rm.update_elements($content);++ // Verify decorated_channel_name was called with the correct stream.+ assert.ok(stream_name_context, "decorated_channel_name should be called");+ assert.equal(stream_name_context.stream.stream_id, stream.stream_id);+ assert.equal(stream_name_context.stream.name, stream.name);++ assert.deepEqual(topic_link_context, {+ channel_id: stream.stream_id,+ stream,+ channel_name: stream.name,+ topic_display_name_html: "important <span class='alert-word'>alert</span> topic",+ is_empty_string_topic: false,+ href: `/#narrow/channel/${stream.stream_id}-test/topic/important.20alert.20topic`,+ });++ alert_words.set_words([]);+});++run_test("message-link alert words", ({mock_template}) => {+ // Setup+ const $content = get_content_element();+ const $message_link = $.create("a.message-link(alert)");+ $message_link.set_find_results(".highlight", []);+ $message_link.attr("href", `/#narrow/channel/${stream.stream_id}-random/topic/alert/near/123`);+ $message_link.addClass("message-link");+ $message_link[0].replaceWith = noop;+ $content.set_find_results("a.stream-topic, a.message-link", $message_link);++ let channel_message_link_context;+ mock_template("channel_message_link.hbs", true, (data, html) => {+ channel_message_link_context = data;+ return html;+ });++ const message = {alerted: true};+ set_message_for_message_content($content, message);+ alert_words.set_words(["alert"]);++ rm.update_elements($content);++ assert.deepEqual(channel_message_link_context, {+ channel_name: stream.name,+ topic_display_name_html: "<span class='alert-word'>alert</span>",+ is_empty_string_topic: false,+ href: `/#narrow/channel/${stream.stream_id}-random/topic/alert/near/123`,+ stream,+ });++ alert_words.set_words([]);+});+run_test("topic-link (empty string topic)", ({mock_template}) => {// Setupconst $content = get_content_element();@@ -508,7 +598,7 @@ run_test("topic-link (empty string topic)", ({mock_template}) => {channel_id: stream.stream_id,stream,channel_name: stream.name,- topic_display_name: `translated: ${REALM_EMPTY_TOPIC_DISPLAY_NAME}`,+ topic_display_name_html: `translated: ${REALM_EMPTY_TOPIC_DISPLAY_NAME}`,is_empty_string_topic: true,href: `/#narrow/channel/${stream.stream_id}-random/topic/`,});@@ -547,7 +637,7 @@ run_test("message-links", ({mock_template}) => {// Final assertsassert.deepEqual(channel_message_link_context, {channel_name: stream.name,- topic_display_name: `translated: ${REALM_EMPTY_TOPIC_DISPLAY_NAME}`,+ topic_display_name_html: `translated: ${REALM_EMPTY_TOPIC_DISPLAY_NAME}`,is_empty_string_topic: true,href: `/#narrow/channel/${stream.stream_id}-test/topic//near/123`,stream,