Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
markdown: Use handlebar to construct HTML that requires escaping.
Prakhar Pratyush375952580713
diff --git a/web/src/markdown.ts b/web/src/markdown.tsindex 0c4572a1b2d..e8d5ae9d522 100644--- a/web/src/markdown.ts+++ b/web/src/markdown.ts@@ -5,6 +5,8 @@ import assert from "minimalistic-assert";import type {Template} from "url-template";import * as fenced_code from "../shared/src/fenced_code.ts";+import render_channel_message_link from "../templates/channel_message_link.hbs";+import render_topic_link from "../templates/topic_link.hbs";import marked from "../third/marked/lib/marked.cjs";import type {LinkifierMatch, ParseOptions, RegExpOrStub} from "../third/marked/lib/marked.cjs";@@ -641,10 +643,12 @@ function handleStreamTopic({return undefined;}const href = stream_topic_hash(stream.stream_id, topic);- const text = `#${stream.name} > ${topic}`;- return `<a class="stream-topic" data-stream-id="${_.escape(- stream.stream_id.toString(),- )}" href="/${_.escape(href)}">${_.escape(text)}</a>`;+ return render_topic_link({+ channel_id: stream.stream_id,+ channel_name: stream.name,+ topic_display_name: topic,+ href,+ });}function handleStreamTopicMessage({@@ -670,8 +674,11 @@ function handleStreamTopicMessage({return undefined;}const href = stream_topic_hash(stream.stream_id, topic) + "/near/" + message_id;- const text = `#${stream.name} > ${topic} @ 💬`;- return `<a class="message-link" href="/${_.escape(href)}">${_.escape(text)}</a>`;+ return render_channel_message_link({+ channel_name: stream.name,+ topic_display_name: topic,+ href,+ });}function handleTex(tex: string, fullmatch: string): string {diff --git a/web/templates/channel_message_link.hbs b/web/templates/channel_message_link.hbsnew file mode 100644index 00000000000..e4c7ba753f2--- /dev/null+++ b/web/templates/channel_message_link.hbs@@ -0,0 +1,2 @@+<a class="message-link" href="{{href}}">#{{channel_name}} > {{topic_display_name}} @ 💬</a>+{{~!-- squash whitespace --~}}diff --git a/web/templates/topic_link.hbs b/web/templates/topic_link.hbsnew file mode 100644index 00000000000..300d37ffcf5--- /dev/null+++ b/web/templates/topic_link.hbs@@ -0,0 +1,2 @@+<a class="stream-topic" data-stream-id="{{channel_id}}" href="{{href}}">#{{channel_name}} > {{topic_display_name}}</a>+{{~!-- squash whitespace --~}}diff --git a/web/tests/markdown.test.cjs b/web/tests/markdown.test.cjsindex dc999e27765..90f854ffae8 100644--- a/web/tests/markdown.test.cjs+++ b/web/tests/markdown.test.cjs@@ -409,12 +409,12 @@ test("marked", ({override}) => {{input: "This is a #**Denmark>some topic** stream_topic link",expected:- '<p>This is a <a class="stream-topic" data-stream-id="1" href="/#narrow/channel/1-Denmark/topic/some.20topic">#Denmark > some topic</a> stream_topic link</p>',+ '<p>This is a <a class="stream-topic" data-stream-id="1" href="#narrow/channel/1-Denmark/topic/some.20topic">#Denmark > some topic</a> stream_topic link</p>',},{input: "This has two links: #**Denmark>some topic** and #**social>other topic**.",expected:- '<p>This has two links: <a class="stream-topic" data-stream-id="1" href="/#narrow/channel/1-Denmark/topic/some.20topic">#Denmark > some topic</a> and <a class="stream-topic" data-stream-id="2" href="/#narrow/channel/2-social/topic/other.20topic">#social > other topic</a>.</p>',+ '<p>This has two links: <a class="stream-topic" data-stream-id="1" href="#narrow/channel/1-Denmark/topic/some.20topic">#Denmark > some topic</a> and <a class="stream-topic" data-stream-id="2" href="#narrow/channel/2-social/topic/other.20topic">#social > other topic</a>.</p>',},{input: "This is not a #**Denmark>** stream_topic link",@@ -423,7 +423,7 @@ test("marked", ({override}) => {{input: "Look at #**Denmark>message_link@100**",expected:- '<p>Look at <a class="message-link" href="/#narrow/channel/1-Denmark/topic/message_link/near/100">#Denmark > message_link @ 💬</a></p>',+ '<p>Look at <a class="message-link" href="#narrow/channel/1-Denmark/topic/message_link/near/100">#Denmark > message_link @ 💬</a></p>',},{input: "Look at #**Unknown>message_link@100**",@@ -633,7 +633,7 @@ test("marked", ({override}) => {{input: "#**Bobby <h1>Tables</h1>**",expected:- '<p><a class="stream-topic" data-stream-id="4" href="/#narrow/channel/4-Bobby-.3Ch1/topic/Tables.3C.2Fh1.3E">#Bobby <h1 > Tables</h1></a></p>',+ '<p><a class="stream-topic" data-stream-id="4" href="#narrow/channel/4-Bobby-.3Ch1/topic/Tables.3C.2Fh1.3E">#Bobby <h1 > Tables</h1></a></p>',},{input: "#**& & &amp;**",@@ -643,7 +643,7 @@ test("marked", ({override}) => {{input: "#**& & &amp;>& & &amp;**",expected:- '<p><a class="stream-topic" data-stream-id="5" href="/#narrow/channel/5-.26-.26-.26amp.3B/topic/.26.20.26.20.26amp.3B">#& & &amp; > & & &amp;</a></p>',+ '<p><a class="stream-topic" data-stream-id="5" href="#narrow/channel/5-.26-.26-.26amp.3B/topic/.26.20.26.20.26amp.3B">#& & &amp; > & & &amp;</a></p>',},];diff --git a/web/tests/markdown_parse.test.cjs b/web/tests/markdown_parse.test.cjsindex 83098abf30a..872fcafb457 100644--- a/web/tests/markdown_parse.test.cjs+++ b/web/tests/markdown_parse.test.cjs@@ -196,7 +196,7 @@ run_test("stream links", () => {assert_parse("#**social>lunch**",- '<p><a class="stream-topic" data-stream-id="301" href="/stream-301-topic-lunch">#social > lunch</a></p>',+ '<p><a class="stream-topic" data-stream-id="301" href="stream-301-topic-lunch">#social > lunch</a></p>',);});