Skip to content

Commit

Permalink
Merge pull request #460 from orchitech/fix-link-parenthesis
Browse files Browse the repository at this point in the history
Escape parenthesis in link URLs. Fix #459.
  • Loading branch information
martincizek authored Apr 8, 2024
2 parents a4ddaff + fd462c8 commit 7bcda25
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/commonmark-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ rules.inlineLink = {

replacement: function (content, node) {
var href = node.getAttribute('href')
if (href) href = href.replace(/([()])/g, '\\$1')
var title = cleanAttribute(node.getAttribute('title'))
if (title) title = ' "' + title + '"'
if (title) title = ' "' + title.replace(/"/g, '\\"') + '"'
return '[' + content + '](' + href + title + ')'
}
}
Expand Down
10 changes: 10 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,16 @@
link")</pre>
</div>

<div class="case" data-name="a with quotes in title">
<div class="input"><a href="http://example.com" title="&quot;hello&quot;">An anchor</a></div>
<pre class="expected">[An anchor](http://example.com "\"hello\"")</pre>
</div>

<div class="case" data-name="a with parenthesis in query">
<div class="input"><a href="http://example.com?(query)">An anchor</a></div>
<pre class="expected">[An anchor](http://example.com?\(query\))</pre>
</div>

<div class="case" data-name="a without a src">
<div class="input"><a id="about-anchor">Anchor without a title</a></div>
<pre class="expected">Anchor without a title</pre>
Expand Down

0 comments on commit 7bcda25

Please sign in to comment.