diff --git a/source/Ox/js/HTML.js b/source/Ox/js/HTML.js index 43c99af2..81eeefb9 100644 --- a/source/Ox/js/HTML.js +++ b/source/Ox/js/HTML.js @@ -71,7 +71,7 @@ function addLinks(string, obfuscate) { return string .replace( - /\b((https?:\/\/|www\.).+?)([\.,:;!\?\)\]]*?(\s|$))/gi, + /\b((https?:\/\/|www\.).+?)([.,:;!?)\]]*?(\s|$))/gi, function(match, url, prefix, end) { prefix = prefix.toLowerCase() == 'www.' ? 'http://' : ''; return Ox.formatString( @@ -81,7 +81,7 @@ } ) .replace( - /\b([0-9A-Z\.\+\-_]+@(?:[0-9A-Z\-]+\.)+[A-Z]{2,6})\b/gi, + /\b([0-9A-Z.+\-_]+@(?:[0-9A-Z\-]+\.)+[A-Z]{2,6})\b/gi, obfuscate ? function(match, mail) { return Ox.encodeEmailAddress(mail); } : '$1' @@ -370,14 +370,21 @@ /*@ Ox.parseMarkdown Parses (a tiny subset of) Markdown. + Supports `*emphasis*`, `_emphasis_`, `**strong**`, `__strong__`, + `` `code` ``, ``` ``code with backtick (`)`` ```, + ```` ```classname\ngithub-style\ncode blocks\n``` ````, + ``, `` and + `[text](http://example.com "title")`. > Ox.parseMarkdown('*foo* **bar** `baz` ``back`tick``') 'foo bar baz back`tick' > Ox.parseMarkdown('foo\n\nbar\n\nbaz') 'foo

bar

baz' - > Ox.parseMarkdown('\n```foo\n\nbar\n\nbaz\n```') - '
bar

baz
' + > Ox.parseMarkdown('```foo\n\nbar\n\nbaz\n```') + '
bar\n\nbaz\n
' > Ox.parseMarkdown('') 'http://example.com' + > Ox.parseMarkdown('``') + '<http://example.com>' > Ox.parseMarkdown('[example](http://example.com "example.com")') 'example' > Ox.parseMarkdown('[example](http://example.com?foo=bar&bar=baz)') @@ -393,7 +400,29 @@ */ Ox.parseMarkdown = function(string) { // see https://github.com/coreyti/showdown/blob/master/src/showdown.js + var array = []; return string.replace(/\r\n/g, '\n').replace(/\r/g, '\n') + .replace( + /(?:^|\n)```(.*)\n([^`]+)\n```/g, + function(match, classname, code) { + array.push( + '
'
+                        + code.trim().replace(/
' + ); + return salt.join(array.length - 1); + } + ) + .replace( + /(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/gm, + function(match, prev, backticks, code, next) { + array.push( + prev + '' + + code.trim().replace(/' + ); + return salt.join(array.length - 1); + } + ) .replace( /(\*\*|__)(?=\S)([^\r]*?\S[*_]*)\1/g, '$2' @@ -402,21 +431,6 @@ /(\*|_)(?=\S)([^\r]*?\S)\1/g, '$2' ) - .replace( - /\n```(.*)\n([^`]+)\n```/g, - function(match, classname, code) { - return '
'
-                        + code.trim().replace(/
'; - } - ) - .replace( - /(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/gm, - function(match, prev, backticks, code, next) { - return prev + '' - + code.trim().replace(/'; - } - ) .replace( /(\[((?:\[[^\]]*\]|[^\[\]])*)\]\([ \t]*()?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g, function(match, all, text, id, url, rest, quote, title) { @@ -435,7 +449,13 @@ return Ox.encodeEmailAddress(mail); } ) - .replace(/\n\n/g, '

'); + .replace(/\n\n/g, '

') + .replace( + new RegExp(salt.join('(\\d+)'), 'g'), + function(match, index) { + return array[parseInt(index)]; + } + ); }; /*@ @@ -476,13 +496,18 @@ 'foo' > Ox.sanitizeHTML('&&') '&&' + > Ox.sanitizeHTML('') + '<http://foo.com>' @*/ Ox.sanitizeHTML = function(html, tags) { var matches = []; tags = tags || defaultTags; // html = Ox.clean(html); fixme: can this be a parameter? if (tags.indexOf('[]') > -1) { - html = html.replace(/\[((\/|https?:\/\/|mailto:).+?) (.+?)\]/gi, '$3'); + html = html.replace( + /\[((\/|https?:\/\/|mailto:).+?) (.+?)\]/gi, + '$3' + ); tags = tags.filter(function(tag) { return tag != '[]'; });