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: Percent-encode spaces in link URLs.

Shubham Padiab7674e6eff01
diff --git a/web/src/markdown.ts b/web/src/markdown.ts
index e8bac4daa62..43762fdcdb6 100644
--- a/web/src/markdown.ts
+++ b/web/src/markdown.ts
@@ -702,10 +702,13 @@ export function parse({
// class-specific highlighting.
renderer.code = (code: string): string => fenced_code.wrap_code(code) + "\n\n";
- // Prohibit empty links for some reason.
const old_link = renderer.link;
- renderer.link = (href: string, title: string, text: string): string =>
- old_link.call(renderer, href, title, text.trim() ? text : href);
+ renderer.link = (href: string, title: string, text: string): string => {
+ // The server percent-encodes spaces in link URLs; see sanitize_url.
+ href = href.replaceAll(" ", "%20");
+ // Prohibit empty links for some reason.
+ return old_link.call(renderer, href, title, text.trim() ? text : href);
+ };
// Put a newline after a <br> in the generated HTML to match Markdown
renderer.br = function () {
diff --git a/zerver/tests/fixtures/markdown_test_cases.json b/zerver/tests/fixtures/markdown_test_cases.json
index 88651f51fe6..44b9eec3700 100644
--- a/zerver/tests/fixtures/markdown_test_cases.json
+++ b/zerver/tests/fixtures/markdown_test_cases.json
@@ -492,6 +492,16 @@
"input": "[ ](https://github.com)",
"expected_output": "<p><a href=\"https://github.com\">https://github.com</a></p>"
},
+ {
+ "name": "link_with_space_in_url",
+ "input": "[foo](<http://example.com/a b>)",
+ "expected_output": "<p><a href=\"http://example.com/a%20b\">foo</a></p>"
+ },
+ {
+ "name": "link_with_space_in_url_and_empty_text",
+ "input": "[](<http://example.com/a b>)",
+ "expected_output": "<p><a href=\"http://example.com/a%20b\">http://example.com/a%20b</a></p>"
+ },
{
"name": "fragment_link",
"input": "[foo](http://zulip.testserver/#narrow/channel/1-Denmark)",