Skip to content

Commit

Permalink
refactor escape numbers that could trigger an ol so that they don't a…
Browse files Browse the repository at this point in the history
…pply links, code blocks, or random numbers
  • Loading branch information
Justin Derrek Van Eaton committed Aug 24, 2012
1 parent 67c025b commit 47df7a3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/to-markdown.js
Expand Up @@ -112,9 +112,11 @@ var toMarkdown = function(string) {
});

// Lists

// Escape numbers that could trigger an ol
string = string.replace(/(\d+). /g, '$1\\. ');
// If there are more than three spaces before the code, it would be in a pre tag
// Make sure we are escaping the period not matching any character
string = string.replace(/^(\s{0,3}\d+)\. /g, '$1\\. ');

// Converts lists that have no child lists (of same type) first, then works it's way up
var noChildrenRegex = /<(ul|ol)\b[^>]*>(?:(?!<ul|<ol)[\s\S])*?<\/\1>/gi;
Expand Down
18 changes: 17 additions & 1 deletion test/nodejs/convert.js
Expand Up @@ -83,12 +83,28 @@ exports['converting code blocks'] = function(test) {
};

exports['converting list elements'] = function(test) {
test.equal(toMarkdown('1986. What a great season.'), '1986\\. What a great season.','We expect numbers that could trigger an ol to be escaped');
test.equal(toMarkdown("<ol>\n\t<li>Hello world</li>\n\t<li>Lorem ipsum</li>\n</ol>"), "1. Hello world\n2. Lorem ipsum", "We expect ol elements to be converted properly");
test.equal(toMarkdown("<ul>\n\t<li>Hello world</li>\n\t<li>Lorem ipsum</li>\n</ul>"), "* Hello world\n* Lorem ipsum", "We expect ul elements with line breaks and tabs to be converted properly");
test.equal(toMarkdown("<ul class='blargh'><li class='first'>Hello world</li><li>Lorem ipsum</li></ul>"), "* Hello world\n* Lorem ipsum", "We expect ul elements with attributes to be converted properly");
test.equal(toMarkdown("<ul><li>Hello world</li><li>Lorem ipsum</li></ul><ul><li>Hello world</li><li>Lorem ipsum</li></ul>"), "* Hello world\n* Lorem ipsum\n\n* Hello world\n* Lorem ipsum", "We expect multiple ul elements to be converted properly");
test.equal(toMarkdown("<ul><li><p>Hello world</p></li><li>Lorem ipsum</li></ul>"), "* Hello world\n\n* Lorem ipsum", "We expect li elements with ps to be converted properly");

var numsToTriggerOlHtml = [
"1986. What a great season.",
"Dont apply to links that end in numbers <a href='http://mygreatsite/users/55'>Like This</a> and have spaces after them",
"Or like an address 123. or anything",
"<pre><code>1234. Four spaces make a code block</code></pre>"
].join('\n'),

numsToTriggerOlMd = [
"1986\\. What a great season.",
"Dont apply to links that end in numbers [Like This](http://mygreatsite/users/55) and have spaces after them",
"Or like an address 123. or anything",
"",
" 1234. Four spaces make a code block"
].join('\n');

test.equal(toMarkdown(numsToTriggerOlHtml), numsToTriggerOlMd, 'We expect only the numbers that could trigger an ol to be escaped');

var lisWithPsHtml = [
"<ol>",
Expand Down

0 comments on commit 47df7a3

Please sign in to comment.