Permalink
Browse files

add unit test for function convertHTMLtoMarkdown

  • Loading branch information...
wm8120 committed Dec 4, 2014
1 parent 0732c3a commit 52cd013413ff4645ed124cef68b5fc9044d65a96
Showing with 28 additions and 2 deletions.
  1. +1 −0 src/common/mdh-html-to-text.js
  2. +27 −0 src/common/test/def-convert.js
  3. +0 −2 src/common/test/index.html
@@ -304,6 +304,7 @@ function convertHTMLtoMarkdown(tag, html) {
( // begin optional prefix capture group
(?:\]\([^\)]*) // match an unclosed URL portion of a MD link -- like "...](..."
|(?:\[[^\]]*) // match an unclosed name portion of a MD link -- like "...[..."
|(?:\[.*\]:.*) // match the patterns of reflink and nolink -- link "[...]:..."
)? // 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
<a\s[^>]*href="([^"]*)"[^>]*>(.*?)<\/a> // an HTML link
Then the replace callback looks like this:
@@ -0,0 +1,27 @@
/* Test the convertHTMLtoMarkdown function whether can handle def pattern correctly.
* Test cases:
* [label]: http://<a href="http://example.com" target="_blank">example.com</a>
* [label]: <a href="http://example.com" target="_blank">http://example.com</a>
* [text](<a href="http://example.com" target="_blank">http://example.com</a>)
* Expect results:
* All of them should be unchanged
*
* (c) 2014 Meng Wang <wm8120@gmail.com>
* MIT License
*/
function convert() {
var convertHTMLtoMarkdown = MdhHtmlToText._testExports.convertHTMLtoMarkdown;
var case1 = '[label]: http://<a href="http://example.com" target="_blank">example.com</a>'
var case2 = '[label]: <a href="http://example.com" target="_blank">http://example.com</a>'
var case3 = '[text](<a href="http://example.com" target="_blank">http://example.com</a>)'
var result1 = convertHTMLtoMarkdown('a', case1);
var result2 = convertHTMLtoMarkdown('a', case2);
var result3 = convertHTMLtoMarkdown('a', case3);
if (result1 !== case1 || result2 !== case2 || result3 !== case3) {
window.alert("convertHTMLtoMarkdown generate false MD links!");
}
};
@@ -36,7 +36,6 @@
<script src="../options-store.js"></script>
<script src="../markdown-here.js"></script>
<!--
<script src="../vendor/jquery-1.9.1.js"></script>
<script src="underscore.js"></script>
<script src="mocha.js"></script>
@@ -50,7 +49,6 @@
<script src="markdown-render-test.js"></script>
<script src="markdown-here-test.js"></script>
<script src="test-run.js"></script>
-->
<script src="def-convert.js"></script>
<script type="text/javascript">

0 comments on commit 52cd013

Please sign in to comment.