Skip to content

Commit

Permalink
Fix anchors containing block elements markdown conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Ndpnt committed Aug 31, 2022
1 parent 4499b5c commit 9b332a8
Show file tree
Hide file tree
Showing 2 changed files with 18 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 @@ -155,7 +155,7 @@ rules.inlineLink = {
var href = node.getAttribute('href')
var title = cleanAttribute(node.getAttribute('title'))
if (title) title = ' "' + title + '"'
return '[' + content + '](' + href + title + ')'
return '[' + content.trim() + '](' + href + title + ')'
}
}

Expand All @@ -169,6 +169,7 @@ rules.referenceLink = {
},

replacement: function (content, node, options) {
content = content.trim()
var href = node.getAttribute('href')
var title = cleanAttribute(node.getAttribute('title'))
if (title) title = ' "' + title + '"'
Expand Down
16 changes: 16 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,22 @@
<pre class="expected">[Some `code`](http://example.com/code)</pre>
</div>

<div class="case" data-name="a with a heading child">
<div class="input"><a href="http://example.com/heading"><h1>heading</h1></a></div>
<pre class="expected">[heading
=======](http://example.com/heading)</pre>
</div>

<div class="case" data-name="a with a paragraph child">
<div class="input"><a href="http://example.com/paragraph"><p>paragraph</p></a></div>
<pre class="expected">[paragraph](http://example.com/paragraph)</pre>
</div>

<div class="case" data-name="a with a div child">
<div class="input"><a href="http://example.com/div"><div>div</div></a></div>
<pre class="expected">[div](http://example.com/div)</pre>
</div>

<div class="case" data-name="a reference" data-options='{"linkStyle": "referenced"}'>
<div class="input"><a href="http://example.com">Reference link</a></div>
<pre class="expected">[Reference link][1]
Expand Down

0 comments on commit 9b332a8

Please sign in to comment.