Skip to content

Commit 52cd013

Browse files
committed
add unit test for function convertHTMLtoMarkdown
1 parent 0732c3a commit 52cd013

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

src/common/mdh-html-to-text.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ function convertHTMLtoMarkdown(tag, html) {
304304
( // begin optional prefix capture group
305305
(?:\]\([^\)]*) // match an unclosed URL portion of a MD link -- like "...](..."
306306
|(?:\[[^\]]*) // match an unclosed name portion of a MD link -- like "...[..."
307+
|(?:\[.*\]:.*) // match the patterns of reflink and nolink -- link "[...]:..."
307308
)? // capture group is optional so that we do the "negative" lookbehind -- that is, we can match links that are *not* preceded by the stuff we *don't* want
308309
<a\s[^>]*href="([^"]*)"[^>]*>(.*?)<\/a> // an HTML link
309310
Then the replace callback looks like this:

src/common/test/def-convert.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* Test the convertHTMLtoMarkdown function whether can handle def pattern correctly.
2+
* Test cases:
3+
* [label]: http://<a href="http://example.com" target="_blank">example.com</a>
4+
* [label]: <a href="http://example.com" target="_blank">http://example.com</a>
5+
* [text](<a href="http://example.com" target="_blank">http://example.com</a>)
6+
* Expect results:
7+
* All of them should be unchanged
8+
*
9+
* (c) 2014 Meng Wang <wm8120@gmail.com>
10+
* MIT License
11+
*/
12+
13+
function convert() {
14+
var convertHTMLtoMarkdown = MdhHtmlToText._testExports.convertHTMLtoMarkdown;
15+
var case1 = '[label]: http://<a href="http://example.com" target="_blank">example.com</a>'
16+
var case2 = '[label]: <a href="http://example.com" target="_blank">http://example.com</a>'
17+
var case3 = '[text](<a href="http://example.com" target="_blank">http://example.com</a>)'
18+
19+
var result1 = convertHTMLtoMarkdown('a', case1);
20+
var result2 = convertHTMLtoMarkdown('a', case2);
21+
var result3 = convertHTMLtoMarkdown('a', case3);
22+
23+
if (result1 !== case1 || result2 !== case2 || result3 !== case3) {
24+
window.alert("convertHTMLtoMarkdown generate false MD links!");
25+
}
26+
};
27+

src/common/test/index.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
<script src="../options-store.js"></script>
3737
<script src="../markdown-here.js"></script>
3838

39-
<!--
4039
<script src="../vendor/jquery-1.9.1.js"></script>
4140
<script src="underscore.js"></script>
4241
<script src="mocha.js"></script>
@@ -50,7 +49,6 @@
5049
<script src="markdown-render-test.js"></script>
5150
<script src="markdown-here-test.js"></script>
5251
<script src="test-run.js"></script>
53-
-->
5452

5553
<script src="def-convert.js"></script>
5654
<script type="text/javascript">

0 commit comments

Comments
 (0)