Skip to content

Commit

Permalink
Improve block quote conversion test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
domchristie committed Oct 29, 2011
1 parent 5272ec3 commit fa4099d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ An HTML to Markdown converter written in javascript

## Todo

* Blockquotes
* Inline HTML

## Licence
Expand Down
21 changes: 16 additions & 5 deletions src/to-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,23 @@ var toMarkdown = function(string) {
}

// Blockquotes
var deepest = /<blockquote\b[^>]*>((?:(?!<blockquote)[\s\S])*?)<\/blockquote>/gi;
while(string.match(deepest)) {
string = string.replace(deepest, function(str) {
return replaceBlockquotes(str);
});
}

string = string.replace(/<blockquote\b[^>]*>((?:(?!<blockquotel)[\s\S])*?)<\/blockquote>/gi, function(str, inner) {
inner = inner.replace(/^\s+|\s+$/g, '');
inner = cleanUp(inner);
return inner.replace(/^/gm, '> ');
});
function replaceBlockquotes(html) {
html = html.replace(/<blockquote\b[^>]*>([\s\S]*?)<\/blockquote>/gi, function(str, inner) {
inner = inner.replace(/^\s+|\s+$/g, '');
inner = cleanUp(inner);
inner = inner.replace(/^/gm, '> ');
inner = inner.replace(/^(>([ \t]{2,}>)+)/gm, '> >');
return inner;
});
return html;
}

function cleanUp(string) {
string = string.replace(/^[\t\r\n]+|[\t\r\n]+$/g, ''); // trim leading/trailing whitespace
Expand Down
45 changes: 44 additions & 1 deletion test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,49 @@ $(function(){
"> ",
"> Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse id sem consectetuer libero luctus adipiscing."
].join('\n');
equal(toMarkdown(html), md, "We expect block quotes with two paragraphs to be converted");
equal(toMarkdown(html), md, "We expect blockquotes with two paragraphs to be converted");

html = [
"<blockquote>",
" <p>This is the first level of quoting.</p>",
"",
" <blockquote>",
" <p>This is nested blockquote.</p>",
" </blockquote>",
"",
" <p>Back to the first level.</p>",
"</blockquote>"
].join('\n');
md = [
"> This is the first level of quoting.",
"> ",
"> > This is nested blockquote.",
"> ",
"> Back to the first level."
].join('\n');
equal(toMarkdown(html), md, "We expect nested blockquotes to be converted");

html = [
"<blockquote>",
" <h2>This is a header.</h2>",
" <ol>",
" <li>This is the first list item.</li>",
" <li>This is the second list item.</li>",
" </ol>",
" <p>Here's some example code:</p>",
" <pre><code>return shell_exec(\"echo $input | $markdown_script\");</code></pre>",
"</blockquote>"
].join('\n');
md = [
"> ## This is a header.",
"> ",
"> 1. This is the first list item.",
"> 2. This is the second list item.",
"> ",
"> Here's some example code:",
"> ",
"> return shell_exec(\"echo $input | $markdown_script\");"
].join('\n');
strictEqual(toMarkdown(html), md, "We expect html in blockquotes to be converted");
});
});

0 comments on commit fa4099d

Please sign in to comment.