diff --git a/lib/marked.js b/lib/marked.js index 5f696b36..78750fa3 100644 --- a/lib/marked.js +++ b/lib/marked.js @@ -16,27 +16,31 @@ var block = { hr: /^( *[\-*_]){3,} *\n/, heading: /^ *(#{1,6}) *([^\0]+?) *#* *\n+/, lheading: /^([^\n]+)\n *(=|-){3,}/, - blockquote: /^ *>[^\n]*(?:\n *>[^\n]*)*/, - list: /^( *)([*+-]|\d+\.) [^\0]+?(?:\n{2,}(?! )|\s*$)(?!\1\2|\1\d+\.)/, + blockquote: /^( *>[^\n]+(\n[^\n]+)*)+/, + list: /^( *)([*+-]|\d+\.) [^\0]+?(?:\n{2,}(?! )|\s*$)(?!\1bullet)/, html: /^ *(?:comment|closed|closing) *(?:\n{2,}|\s*$)/, paragraph: /^([^\n]+\n?(?!body))+\n*/, text: /^[^\n]+/ }; +block.list = (function() { + var list = block.list.source; + + list = list + .replace('bullet', /(?:[*+-](?!( *[-*]){2,})|\d+\.)/.source) + + return new RegExp(list); +})(); + block.html = (function() { var html = block.html.source; html = html .replace('comment', //.source) .replace('closed', /<(\w+)[^\0]+?<\/\1>/.source) - .replace('closing', /<\w+[^>]*>/.source); + .replace('closing', /<\w+(?!:\/|@)\b(?:"[^"]*"|'[^']*'|[^>])*>/.source); - // Better regexes: - // .replace('closed', /<(\w+)\b[^\0]*?<\/\1>/.source) - // .replace('closing', /<\w+(?!:\/|@)\b[^>]*>/.source); - - return new - RegExp(html); + return new RegExp(html); })(); block.paragraph = (function() { @@ -76,9 +80,9 @@ block.lexer = function(str) { .replace(/\t/g, ' '); str = str.replace( - /^ {0,3}\[([^\]]+)\]: *([^ ]+)(?: +"([^\n]+)")? *$/gm, + /^ {0,3}\[([^\]]+)\]: *([^ ]+)(?: +["(]([^\n]+)[")])? *$/gm, function(__, id, href, title) { - links[id] = { + links[id.toLowerCase()] = { href: href, title: title }; @@ -175,9 +179,6 @@ block.token = function(str, tokens, top) { cap = cap[0].replace(/^ *>/gm, ''); - // Better conformance: - // cap = cap[0].replace(/^ *> ?/gm, ''); - // Pass `top` to keep the current // "toplevel" state. This is exactly // how markdown.pl works. @@ -202,7 +203,7 @@ block.token = function(str, tokens, top) { // Get each top-level item. cap = cap[0].match( - /^( *)([*+-]|\d+\.)[^\n]*(?:\n(?!\1\2|\1\d+\.)[^\n]*)*/gm + /^( *)([*+-]|\d+\.)[^\n]*(?:\n(?!\1(?:[*+-]|\d+\.))[^\n]*)*/gm ); i = 0; @@ -282,16 +283,16 @@ block.token = function(str, tokens, top) { */ var inline = { - escape: /^\\([\\`*{}\[\]()#+\-.!_])/, + escape: /^\\([\\`*{}\[\]()#+\-.!_>])/, autolink: /^<([^ >]+(@|:\/)[^ >]+)>/, gfm_autolink: /^(\w+:\/\/[^\s]+[^.,:;"')\]\s])/, - tag: /^|^<\/?\w+[^>]*>/, - link: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]\(([^\)]*)\)/, - reflink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]\s*\[([^\]]*)\]/, + tag: /^|^<\/?\w+(?:"[^"]*"|'[^']*'|[^>])*>/, + link: /^!?\[((?:\[[^\]]*\]|[^\[\]]|\[|\](?=[^[\]]*\]))*)\]\(([^\)]*)\)/, + reflink: /^!?\[((?:\[[^\]]*\]|[^\[\]]|\[|\](?=[^[\]]*\]))*)\]\s*\[([^\]]*)\]/, nolink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]/, strong: /^__(?=\S)([^\0]*?\S)__(?!_)|^\*\*(?=\S)([^\0]*?\S)\*\*(?!\*)/, - em: /^\b_(?=\S)([^\0]*?\S)_\b|^\*(?=\S)([^\0]*?\S)\*/, - code: /^`([^`]+)`|^``([^\0]+?)``/, + em: /^\b_(?=\S)([^\0]*?\S)_\b|^\*(?=\S)((?:\*\*|[^\0])*?)\*(?!\*)/, + code: /^(`+)([^\0]*?[^`])\1(?!`)/, br: /^ {2,}\n(?!\s*$)/, text: /^[^\0]+?(?=body|$)/ }; @@ -382,6 +383,11 @@ inline.lexer = function(str) { if (cap = inline.link.exec(str)) { str = str.substring(cap[0].length); text = /^\s*?(?:\s+"([^\n]+)")?\s*$/.exec(cap[2]); + if (!text) { + out += cap[0][0]; + str = cap[0].substring(1) + str; + continue; + } link = { href: text[1], title: text[2] @@ -395,7 +401,7 @@ inline.lexer = function(str) { || (cap = inline.nolink.exec(str))) { str = str.substring(cap[0].length); link = (cap[2] || cap[1]).replace(/\s+/g, ' '); - link = links[link]; + link = links[link.toLowerCase()]; if (!link) { out += cap[0][0]; str = cap[0].substring(1) + str; diff --git a/test/fix.js b/test/fix.js index 84706dd9..756356b8 100644 --- a/test/fix.js +++ b/test/fix.js @@ -60,14 +60,6 @@ fs.readdirSync(dir).forEach(function(file) { fs.writeFileSync(file, html); })(); -// fix bad grammar -(function() { - var file = dir + '/ordered_and_unordered_lists.text'; - var text = fs.readFileSync(file, 'utf8'); - text = text.replace(/(\n\*\sasterisk\s3\n\n)(\*\s\*\s\*)/, '$1\n$2'); - fs.writeFileSync(file, text); -})(); - // fix strange markup that isnt likely // to exist in the reality (function _(ext) { diff --git a/test/index.js b/test/index.js index 1b7a6677..736f9786 100644 --- a/test/index.js +++ b/test/index.js @@ -101,10 +101,11 @@ main.bench = function(name, func) { load(); // remove these as they affect the // comparison to older benchmark times. - Object.keys(files).forEach(function(name) { - if (name.indexOf('gfm') === 0) delete files[name]; + fs.readdirSync(__dirname + '/new').forEach(function(name) { + if (name.split('.').pop() === 'html') return; + if (name === 'main.text') return; + delete files[name]; }); - delete files['toplevel_paragraphs.text']; } var start = Date.now() diff --git a/test/new/case_insensitive_refs.html b/test/new/case_insensitive_refs.html new file mode 100644 index 00000000..c54388ea --- /dev/null +++ b/test/new/case_insensitive_refs.html @@ -0,0 +1 @@ +

hi

diff --git a/test/new/case_insensitive_refs.text b/test/new/case_insensitive_refs.text new file mode 100644 index 00000000..598915a8 --- /dev/null +++ b/test/new/case_insensitive_refs.text @@ -0,0 +1,3 @@ +[hi] + +[HI]: /url diff --git a/test/new/escape_angles.html b/test/new/escape_angles.html new file mode 100644 index 00000000..f0d4bcd7 --- /dev/null +++ b/test/new/escape_angles.html @@ -0,0 +1 @@ +
diff --git a/test/new/escape_angles.text b/test/new/escape_angles.text new file mode 100644 index 00000000..f0d4bcd7 --- /dev/null +++ b/test/new/escape_angles.text @@ -0,0 +1 @@ +
diff --git a/test/new/lazy_blockquotes.html b/test/new/lazy_blockquotes.html new file mode 100644 index 00000000..a701d42a --- /dev/null +++ b/test/new/lazy_blockquotes.html @@ -0,0 +1,4 @@ +
+

hi there +bud

+
diff --git a/test/new/lazy_blockquotes.text b/test/new/lazy_blockquotes.text new file mode 100644 index 00000000..c0e0b154 --- /dev/null +++ b/test/new/lazy_blockquotes.text @@ -0,0 +1,2 @@ +> hi there +bud diff --git a/test/new/naked_angles.html b/test/new/naked_angles.html new file mode 100644 index 00000000..7cf47c49 --- /dev/null +++ b/test/new/naked_angles.html @@ -0,0 +1 @@ +

>

diff --git a/test/new/naked_angles.text b/test/new/naked_angles.text new file mode 100644 index 00000000..db7422f1 --- /dev/null +++ b/test/new/naked_angles.text @@ -0,0 +1 @@ +\> diff --git a/test/new/nested_code.html b/test/new/nested_code.html new file mode 100644 index 00000000..c3705920 --- /dev/null +++ b/test/new/nested_code.html @@ -0,0 +1 @@ +

hi ther `` ok ```

diff --git a/test/new/nested_code.text b/test/new/nested_code.text new file mode 100644 index 00000000..910e3d46 --- /dev/null +++ b/test/new/nested_code.text @@ -0,0 +1 @@ +````` hi ther `` ok ``` ````` diff --git a/test/new/nested_em.html b/test/new/nested_em.html new file mode 100644 index 00000000..aa2064ae --- /dev/null +++ b/test/new/nested_em.html @@ -0,0 +1 @@ +

test test

diff --git a/test/new/nested_em.text b/test/new/nested_em.text new file mode 100644 index 00000000..b6edfadf --- /dev/null +++ b/test/new/nested_em.text @@ -0,0 +1 @@ +*test **test*** diff --git a/test/new/nested_square_link.html b/test/new/nested_square_link.html new file mode 100644 index 00000000..c8b79408 --- /dev/null +++ b/test/new/nested_square_link.html @@ -0,0 +1 @@ +

the ] character

diff --git a/test/new/nested_square_link.text b/test/new/nested_square_link.text new file mode 100644 index 00000000..82226ed5 --- /dev/null +++ b/test/new/nested_square_link.text @@ -0,0 +1 @@ +[the `]` character](/url) diff --git a/test/new/not_a_link.html b/test/new/not_a_link.html new file mode 100644 index 00000000..7943175e --- /dev/null +++ b/test/new/not_a_link.html @@ -0,0 +1 @@ +

\[test](not a link)

diff --git a/test/new/not_a_link.text b/test/new/not_a_link.text new file mode 100644 index 00000000..5cfab5f1 --- /dev/null +++ b/test/new/not_a_link.text @@ -0,0 +1 @@ +\\[test](not a link) diff --git a/test/new/ref_paren.html b/test/new/ref_paren.html new file mode 100644 index 00000000..cff6977f --- /dev/null +++ b/test/new/ref_paren.html @@ -0,0 +1 @@ +

hi

diff --git a/test/new/ref_paren.text b/test/new/ref_paren.text new file mode 100644 index 00000000..aa97c91a --- /dev/null +++ b/test/new/ref_paren.text @@ -0,0 +1,3 @@ +[hi] + +[hi]: /url (there) diff --git a/test/new/sublist.html b/test/new/sublist.html new file mode 100644 index 00000000..9220741c --- /dev/null +++ b/test/new/sublist.html @@ -0,0 +1,5 @@ + diff --git a/test/new/sublist.text b/test/new/sublist.text new file mode 100644 index 00000000..27a89675 --- /dev/null +++ b/test/new/sublist.text @@ -0,0 +1,3 @@ +* test ++ test +- test diff --git a/test/tests/case_insensitive_refs.html b/test/tests/case_insensitive_refs.html new file mode 100644 index 00000000..c54388ea --- /dev/null +++ b/test/tests/case_insensitive_refs.html @@ -0,0 +1 @@ +

hi

diff --git a/test/tests/case_insensitive_refs.text b/test/tests/case_insensitive_refs.text new file mode 100644 index 00000000..598915a8 --- /dev/null +++ b/test/tests/case_insensitive_refs.text @@ -0,0 +1,3 @@ +[hi] + +[HI]: /url diff --git a/test/tests/escape_angles.html b/test/tests/escape_angles.html new file mode 100644 index 00000000..f0d4bcd7 --- /dev/null +++ b/test/tests/escape_angles.html @@ -0,0 +1 @@ +
diff --git a/test/tests/escape_angles.text b/test/tests/escape_angles.text new file mode 100644 index 00000000..f0d4bcd7 --- /dev/null +++ b/test/tests/escape_angles.text @@ -0,0 +1 @@ +
diff --git a/test/tests/lazy_blockquotes.html b/test/tests/lazy_blockquotes.html new file mode 100644 index 00000000..a701d42a --- /dev/null +++ b/test/tests/lazy_blockquotes.html @@ -0,0 +1,4 @@ +
+

hi there +bud

+
diff --git a/test/tests/lazy_blockquotes.text b/test/tests/lazy_blockquotes.text new file mode 100644 index 00000000..c0e0b154 --- /dev/null +++ b/test/tests/lazy_blockquotes.text @@ -0,0 +1,2 @@ +> hi there +bud diff --git a/test/tests/naked_angles.html b/test/tests/naked_angles.html new file mode 100644 index 00000000..7cf47c49 --- /dev/null +++ b/test/tests/naked_angles.html @@ -0,0 +1 @@ +

>

diff --git a/test/tests/naked_angles.text b/test/tests/naked_angles.text new file mode 100644 index 00000000..db7422f1 --- /dev/null +++ b/test/tests/naked_angles.text @@ -0,0 +1 @@ +\> diff --git a/test/tests/nested_code.html b/test/tests/nested_code.html new file mode 100644 index 00000000..c3705920 --- /dev/null +++ b/test/tests/nested_code.html @@ -0,0 +1 @@ +

hi ther `` ok ```

diff --git a/test/tests/nested_code.text b/test/tests/nested_code.text new file mode 100644 index 00000000..910e3d46 --- /dev/null +++ b/test/tests/nested_code.text @@ -0,0 +1 @@ +````` hi ther `` ok ``` ````` diff --git a/test/tests/nested_em.html b/test/tests/nested_em.html new file mode 100644 index 00000000..aa2064ae --- /dev/null +++ b/test/tests/nested_em.html @@ -0,0 +1 @@ +

test test

diff --git a/test/tests/nested_em.text b/test/tests/nested_em.text new file mode 100644 index 00000000..b6edfadf --- /dev/null +++ b/test/tests/nested_em.text @@ -0,0 +1 @@ +*test **test*** diff --git a/test/tests/nested_square_link.html b/test/tests/nested_square_link.html new file mode 100644 index 00000000..c8b79408 --- /dev/null +++ b/test/tests/nested_square_link.html @@ -0,0 +1 @@ +

the ] character

diff --git a/test/tests/nested_square_link.text b/test/tests/nested_square_link.text new file mode 100644 index 00000000..82226ed5 --- /dev/null +++ b/test/tests/nested_square_link.text @@ -0,0 +1 @@ +[the `]` character](/url) diff --git a/test/tests/not_a_link.html b/test/tests/not_a_link.html new file mode 100644 index 00000000..7943175e --- /dev/null +++ b/test/tests/not_a_link.html @@ -0,0 +1 @@ +

\[test](not a link)

diff --git a/test/tests/not_a_link.text b/test/tests/not_a_link.text new file mode 100644 index 00000000..5cfab5f1 --- /dev/null +++ b/test/tests/not_a_link.text @@ -0,0 +1 @@ +\\[test](not a link) diff --git a/test/tests/ordered_and_unordered_lists.text b/test/tests/ordered_and_unordered_lists.text index 5363f8d1..7f3b4977 100644 --- a/test/tests/ordered_and_unordered_lists.text +++ b/test/tests/ordered_and_unordered_lists.text @@ -15,7 +15,6 @@ Asterisks loose: * asterisk 3 - * * * Pluses tight: diff --git a/test/tests/ref_paren.html b/test/tests/ref_paren.html new file mode 100644 index 00000000..cff6977f --- /dev/null +++ b/test/tests/ref_paren.html @@ -0,0 +1 @@ +

hi

diff --git a/test/tests/ref_paren.text b/test/tests/ref_paren.text new file mode 100644 index 00000000..aa97c91a --- /dev/null +++ b/test/tests/ref_paren.text @@ -0,0 +1,3 @@ +[hi] + +[hi]: /url (there) diff --git a/test/tests/sublist.html b/test/tests/sublist.html new file mode 100644 index 00000000..9220741c --- /dev/null +++ b/test/tests/sublist.html @@ -0,0 +1,5 @@ +
    +
  • test
  • +
  • test
  • +
  • test
  • +
diff --git a/test/tests/sublist.text b/test/tests/sublist.text new file mode 100644 index 00000000..27a89675 --- /dev/null +++ b/test/tests/sublist.text @@ -0,0 +1,3 @@ +* test ++ test +- test