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: Add function to unescape html characters.

Kislay Verma10995ded0e6a
diff --git a/web/src/topic_link_util.ts b/web/src/topic_link_util.ts
index 7c9c41a8745..2b202dafdae 100644
--- a/web/src/topic_link_util.ts
+++ b/web/src/topic_link_util.ts
@@ -31,6 +31,23 @@ export function escape_invalid_stream_topic_characters(text: string): string {
}
}
+// This record should be kept in sync with the
+// escape_invalid_stream_topic_characters function.
+const escaped_to_original_mapping: Record<string, string> = {
+ "&#96;": "`",
+ "&gt;": ">",
+ "&#42;": "*",
+ "&amp;": "&",
+ "&#36;&#36;": "$$",
+ "&#91;": "[",
+ "&#93;": "]",
+};
+
+export function html_unescape_invalid_stream_topic_characters(text: string): string {
+ const unescape_regex = new RegExp(Object.keys(escaped_to_original_mapping).join("|"), "g");
+ return text.replaceAll(unescape_regex, (match) => escaped_to_original_mapping[match] ?? match);
+}
+
export function html_escape_markdown_syntax_characters(text: string): string {
return text.replaceAll(invalid_stream_topic_regex, escape_invalid_stream_topic_characters);
}
diff --git a/web/tests/topic_link_util.test.cjs b/web/tests/topic_link_util.test.cjs
index 8b2db84f058..36ea6d4981d 100644
--- a/web/tests/topic_link_util.test.cjs
+++ b/web/tests/topic_link_util.test.cjs
@@ -125,4 +125,9 @@ run_test("stream_topic_link_syntax_test", () => {
topic_link_util.as_html_link_syntax_unsafe("test", "example.com"),
'<a href="example.com">test</a>',
);
+
+ assert.equal(
+ topic_link_util.html_unescape_invalid_stream_topic_characters("&#36;&#36;MONEY&#36;&#36;"),
+ "$$MONEY$$",
+ );
});