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.

topic_link_util: Extract get_topic_link_content and helpers.

Vishesh Singh6619b3b58962
diff --git a/web/src/topic_link_util.ts b/web/src/topic_link_util.ts
index 06b7778a7ec..cc102272b78 100644
--- a/web/src/topic_link_util.ts
+++ b/web/src/topic_link_util.ts
@@ -35,11 +35,11 @@ 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(
+export function get_topic_link_content(
stream_name: string,
topic_name?: string,
message_id?: string,
-): string {
+): {text: string; url: string} {
const stream = stream_data.get_sub(stream_name);
const stream_id = stream?.stream_id;
assert(stream_id !== undefined);
@@ -51,11 +51,39 @@ export function get_fallback_markdown_link(
() => stream_name,
);
if (message_id !== undefined) {
- return `[#${escape(stream_name)} > ${escape(topic_name)} @ 💬](${stream_topic_url}/near/${message_id})`;
+ return {
+ text: `#${escape(stream_name)} > ${escape(topic_name)} @ 💬`,
+ url: `${stream_topic_url}/near/${message_id}`,
+ };
}
- return `[#${escape(stream_name)} > ${escape(topic_name)}](${stream_topic_url})`;
+ return {
+ text: `#${escape(stream_name)} > ${escape(topic_name)}`,
+ url: stream_topic_url,
+ };
}
- return `[#${escape(stream_name)}](${internal_url.by_stream_url(stream_id, () => stream_name)})`;
+ return {
+ text: `#${escape(stream_name)}`,
+ url: internal_url.by_stream_url(stream_id, () => stream_name),
+ };
+}
+
+export function as_markdown_link_syntax(text: string, url: string): string {
+ return `[${text}](${url})`;
+}
+
+export function as_html_link_syntax_unsafe(text: string, url: string): string {
+ // The caller is responsible for making sure that the `text`
+ // parameter is properly escaped.
+ return `<a href="${url}">${text}</a>`;
+}
+
+export function get_fallback_markdown_link(
+ stream_name: string,
+ topic_name?: string,
+ message_id?: string,
+): string {
+ const {text, url} = get_topic_link_content(stream_name, topic_name, message_id);
+ return as_markdown_link_syntax(text, url);
}
export function get_stream_topic_link_syntax(stream_name: string, topic_name: string): string {
diff --git a/web/tests/topic_link_util.test.cjs b/web/tests/topic_link_util.test.cjs
index 60a823a930f..abeab0e22b4 100644
--- a/web/tests/topic_link_util.test.cjs
+++ b/web/tests/topic_link_util.test.cjs
@@ -108,4 +108,8 @@ run_test("stream_topic_link_syntax_test", () => {
// Only for full coverage of the module.
assert.equal(topic_link_util.escape_invalid_stream_topic_characters("Sweden"), "Sweden");
+ assert.equal(
+ topic_link_util.as_html_link_syntax_unsafe("test", "example.com"),
+ '<a href="example.com">test</a>',
+ );
});