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.

marked: Keep balanced parentheses in link URLs.

Shubham Padiad7c57ed15b82
diff --git a/web/third/marked/lib/marked.cjs b/web/third/marked/lib/marked.cjs
index 973f383808f..c5654ae01bb 100644
--- a/web/third/marked/lib/marked.cjs
+++ b/web/third/marked/lib/marked.cjs
@@ -718,6 +718,7 @@ InlineLexer.prototype.output = function(src) {
// link
if (cap = this.rules.link.exec(src)) {
+ cap = this.reparseLinkHref(src, cap);
src = src.substring(cap[0].length);
this.inLink = true;
out += this.outputLink(cap, {
@@ -867,6 +868,68 @@ InlineLexer.prototype.output = function(src) {
* Compile Link
*/
+// An optional quoted title, then the ")" that closes the link:
+// ) or "some title")
+var linkEnding = /^(?:\s+(['"])([\s\S]*?)\1)?\s*\)/;
+
+// The link regex stops the href at the first ")". That is wrong when
+// the URL itself contains parentheses, like http://a/b(c)d: the href
+// is cut at "c" and "d)" is left over as text.
+//
+// `cap` is the regex's match:
+// cap[0] the text the regex matched, from "[" to ")"
+// cap[1] the link text
+// cap[2] the href
+// cap[3] the title, or undefined
+// This returns an array with the same four entries. cap[0] now ends
+// at the ")" that really closes the link, and cap[2] holds the full
+// href. cap[1] is copied over unchanged. If the link doesn't look the
+// way we expect, `cap` is returned as is.
+//
+// This follows the server's markdown implementation. An href wrapped
+// in <> runs up to the ">". Otherwise, a quoted string followed by
+// ")" is the title and the href runs up to it; the title may itself
+// contain parentheses. With no title, the href runs up to the ")"
+// that balances the link's "(", even across spaces.
+InlineLexer.prototype.reparseLinkHref = function(src, cap) {
+ // Start just after "[text](", or "![text](" for images.
+ var index = (cap[0].charAt(0) === '!' ? 2 : 1) + cap[1].length + 2;
+ // Skip any whitespace before the href.
+ index += /^\s*/.exec(src.substring(index))[0].length;
+ var rest = src.substring(index);
+ var end, ending;
+ if (rest.charAt(0) === '<') {
+ var close = rest.indexOf('>');
+ if (close === -1) {
+ return cap;
+ }
+ ending = linkEnding.exec(rest.substring(close + 1));
+ if (!ending) {
+ return cap;
+ }
+ index += close + 1 + ending[0].length;
+ return [src.substring(0, index), cap[1], rest.substring(1, close), ending[2]];
+ }
+ // Find the ")" that has no matching "(" before it.
+ var unmatched = findClosingBracket(rest, '()');
+ // A quote after whitespace starts the title, as long as it comes
+ // before that ")". Parentheses inside the title don't count.
+ var quote = /\s['"]/.exec(rest);
+ if (quote && (unmatched === -1 || quote.index < unmatched)) {
+ ending = linkEnding.exec(rest.substring(quote.index));
+ if (ending) {
+ end = quote.index;
+ index += quote.index + ending[0].length;
+ return [src.substring(0, index), cap[1], rest.substring(0, end).replace(/\s+$/, ''), ending[2]];
+ }
+ }
+ if (unmatched === -1) {
+ return cap;
+ }
+ index += unmatched + 1;
+ return [src.substring(0, index), cap[1], rest.substring(0, unmatched).replace(/\s+$/, ''), undefined];
+};
+
InlineLexer.prototype.outputLink = function(cap, link) {
var href = escape(link.href)
, title = link.title ? escape(link.title) : null;
@@ -1387,6 +1450,27 @@ function unescape(html) {
});
}
+// Adapted from marked v0.6.2 (https://github.com/markedjs/marked/pull/1476).
+// Unlike upstream, backslash-escaped brackets are not skipped, to
+// match the server's markdown implementation.
+function findClosingBracket(str, b) {
+ if (str.indexOf(b[1]) === -1) {
+ return -1;
+ }
+ var level = 0;
+ for (var i = 0; i < str.length; i++) {
+ if (str[i] === b[0]) {
+ level++;
+ } else if (str[i] === b[1]) {
+ level--;
+ if (level < 0) {
+ return i;
+ }
+ }
+ }
+ return -1;
+}
+
function replace(regex, opt) {
regex = regex.source;
opt = opt || '';
diff --git a/zerver/tests/fixtures/markdown_test_cases.json b/zerver/tests/fixtures/markdown_test_cases.json
index 44b9eec3700..4e8261ab464 100644
--- a/zerver/tests/fixtures/markdown_test_cases.json
+++ b/zerver/tests/fixtures/markdown_test_cases.json
@@ -502,6 +502,94 @@
"input": "[](<http://example.com/a b>)",
"expected_output": "<p><a href=\"http://example.com/a%20b\">http://example.com/a%20b</a></p>"
},
+ {
+ "name": "link_with_parentheses_in_url",
+ "input": "[this issue](https://github.com/zulip/zulip/issues?q=is%3Aissue%20state%3Aopen%20label%3A%22area%3A%20markdown%20(embeds%2Fpreviews)%22)",
+ "expected_output": "<p><a href=\"https://github.com/zulip/zulip/issues?q=is%3Aissue%20state%3Aopen%20label%3A%22area%3A%20markdown%20(embeds%2Fpreviews)%22\">this issue</a></p>"
+ },
+ {
+ "name": "link_with_nested_parentheses_in_url",
+ "input": "[foo](http://example.com/a(b(c)d)e)",
+ "expected_output": "<p><a href=\"http://example.com/a(b(c)d)e\">foo</a></p>"
+ },
+ {
+ "name": "link_with_parentheses_in_url_and_title",
+ "input": "[foo](http://example.com/a(b) \"my title\")",
+ "expected_output": "<p><a href=\"http://example.com/a(b)\" title=\"my title\">foo</a></p>"
+ },
+ {
+ "name": "link_with_unbalanced_parenthesis_in_title",
+ "input": "[foo](http://example.com/a \"my title)\")",
+ "expected_output": "<p><a href=\"http://example.com/a\" title=\"my title)\">foo</a></p>"
+ },
+ {
+ "name": "link_with_parenthesis_in_url_before_title",
+ "input": "[foo](http://example.com/a(b \"my title\") c)",
+ "expected_output": "<p><a href=\"http://example.com/a(b\" title=\"my title\">foo</a> c)</p>"
+ },
+ {
+ "name": "link_with_parenthesis_in_title",
+ "input": "[foo](http://example.com/a \"my (title)\")",
+ "expected_output": "<p><a href=\"http://example.com/a\" title=\"my (title)\">foo</a></p>"
+ },
+ {
+ "name": "link_with_escaped_parenthesis_in_url",
+ "input": "[foo](http://example.com/a\\)b)",
+ "expected_output": "<p><a href=\"http://example.com/a\\\">foo</a>b)</p>"
+ },
+ {
+ "name": "image_with_parentheses_in_url",
+ "input": "![foo](http://example.com/a(b)c \"my title\")",
+ "expected_output": "<p>![foo](<a href=\"http://example.com/a(b)c\">http://example.com/a(b)c</a> \"my title\")</p>",
+ "marked_expected_output": "<p><img src=\"http://example.com/a(b)c\" alt=\"foo\" title=\"my title\"></p>"
+ },
+ {
+ "name": "link_with_unbalanced_parenthesis_in_url",
+ "input": "[foo](http://example.com/a(b)",
+ "expected_output": "<p>[foo](<a href=\"http://example.com/a(b)\">http://example.com/a(b)</a></p>",
+ "marked_expected_output": "<p><a href=\"http://example.com/a(b\">foo</a></p>"
+ },
+ {
+ "name": "link_with_quote_and_parentheses_in_url",
+ "input": "[film](https://en.wikipedia.org/wiki/Ocean's_Eleven_(2001_film))",
+ "expected_output": "<p><a href=\"https://en.wikipedia.org/wiki/Ocean's_Eleven_(2001_film\">film</a>)</p>",
+ "marked_expected_output": "<p><a href=\"https://en.wikipedia.org/wiki/Ocean's_Eleven_(2001_film)\">film</a></p>"
+ },
+ {
+ "name": "link_with_parentheses_in_url_and_single_quoted_title",
+ "input": "[foo](http://example.com/a(b)c 'my title')",
+ "expected_output": "<p><a href=\"http://example.com/a(b)c\" title=\"my title\">foo</a></p>"
+ },
+ {
+ "name": "link_with_parentheses_and_space_in_url",
+ "input": "[foo](http://example.com/a(b c)d)",
+ "expected_output": "<p><a href=\"http://example.com/a(b%20c)d\">foo</a></p>"
+ },
+ {
+ "name": "link_with_space_in_url_and_title",
+ "input": "[foo](http://example.com/a b \"my title\")",
+ "expected_output": "<p><a href=\"http://example.com/a%20b\" title=\"my title\">foo</a></p>"
+ },
+ {
+ "name": "link_with_quotes_in_url",
+ "input": "[foo](http://example.com/a \"b\" c)",
+ "expected_output": "<p><a href=\"http://example.com/a%20&quot;b&quot;%20c\">foo</a></p>"
+ },
+ {
+ "name": "link_with_parentheses_in_angle_bracket_url",
+ "input": "[foo](<http://example.com/a(b)c>)",
+ "expected_output": "<p><a href=\"http://example.com/a(b)c\">foo</a></p>"
+ },
+ {
+ "name": "link_with_parentheses_and_space_in_angle_bracket_url_and_title",
+ "input": "[foo](<http://example.com/a(b c)d> \"my title\")",
+ "expected_output": "<p><a href=\"http://example.com/a(b%20c)d\" title=\"my title\">foo</a></p>"
+ },
+ {
+ "name": "link_with_unbalanced_parenthesis_in_angle_bracket_url",
+ "input": "[foo](<http://example.com/a(b>)",
+ "expected_output": "<p><a href=\"http://example.com/a(b\">foo</a></p>"
+ },
{
"name": "fragment_link",
"input": "[foo](http://zulip.testserver/#narrow/channel/1-Denmark)",