@@ -14,51 +14,32 @@ Discourse.Dialect.on("register", function(event) {
1414 Parses out links from HTML.
1515
1616 @method autoLink
17- @param {Markdown.Block } block the block to examine
18- @param {Array } next the next blocks in the sequence
19- @return {Array } the JsonML containing the markup or undefined if nothing changed.
17+ @param {String } text the text match
18+ @param {Array } match the match found
19+ @param {Array } prev the previous jsonML
20+ @return {Array } an array containing how many chars we've replaced and the jsonML content for it.
2021 @namespace Discourse.Dialect
2122 **/
22- dialect . block [ 'autolink' ] = function autoLink ( block , next ) {
23- var pattern = / ( ^ | \s ) ( (?: h t t p s ? : (?: \/ { 1 , 3 } | [ a - z 0 - 9 % ] ) | w w w \d { 0 , 3 } [ . ] ) (?: [ ^ \s ( ) < > ] + | \( [ ^ \s ( ) < > ] + \) ) + (?: \( [ ^ \s ( ) < > ] + \) | [ ^ ` ! ( ) \[ \] { } ; : ' " . , < > ? « » “ ” ‘ ’ \s ] ) ) / gm,
24- result ,
25- remaining = block ,
26- m ;
23+ dialect . inline [ 'http' ] = dialect . inline [ 'www' ] = function autoLink ( text , match , prev ) {
2724
28- var pushIt = function ( p ) { result . push ( p ) } ;
25+ // We only care about links on boundaries
26+ if ( prev && ( prev . length > 0 ) ) {
27+ var last = prev [ prev . length - 1 ] ;
28+ if ( typeof last === "string" && ( ! last . match ( / \s $ / ) ) ) { return ; }
29+ }
2930
30- while ( m = pattern . exec ( remaining ) ) {
31- result = result || [ 'p' ] ;
31+ var pattern = / ( ^ | \s ) ( (?: h t t p s ? : (?: \/ { 1 , 3 } | [ a - z 0 - 9 % ] ) | w w w \d { 0 , 3 } [ . ] ) (?: [ ^ \s ( ) < > ] + | \( [ ^ \s ( ) < > ] + \) ) + (?: \( [ ^ \s ( ) < > ] + \) | [ ^ ` ! ( ) \[ \] { } ; : ' " . , < > ? « » “ ” ‘ ’ \s ] ) ) / gm ,
32+ m = pattern . exec ( text ) ;
3233
34+ if ( m ) {
3335 var url = m [ 2 ] ,
34- urlIndex = remaining . indexOf ( url ) ,
35- before = remaining . slice ( 0 , urlIndex ) ;
36-
37- if ( before . match ( / \[ \d + \] / ) ) { return ; }
38-
39- pattern . lastIndex = 0 ;
40- remaining = remaining . slice ( urlIndex + url . length ) ;
36+ displayUrl = m [ 2 ] ;
4137
42- if ( before ) {
43- this . processInline ( before ) . forEach ( pushIt ) ;
44- }
45-
46- var displayUrl = url ;
4738 if ( url . match ( / ^ w w w / ) ) { url = "http://" + url ; }
48- result . push ( [ 'a' , { href : url } , displayUrl ] ) ;
49-
50- if ( remaining && remaining . match ( / \n / ) ) {
51- next . unshift ( MD . mk_block ( remaining ) ) ;
52- remaining = [ ] ;
53- }
39+ return [ m [ 0 ] . length , [ 'a' , { href : url } , displayUrl ] ] ;
5440 }
5541
56- if ( result ) {
57- if ( remaining . length ) {
58- this . processInline ( remaining ) . forEach ( pushIt ) ;
59- }
60- return [ result ] ;
61- }
6242 } ;
6343
44+
6445} ) ;
0 commit comments