@@ -564,36 +564,57 @@ inline.pedantic = merge({}, inline.normal, {
564564 . replace ( 'label' , inline . _label )
565565 . getRegex ( ) ;
566566
567- function unwrapAngleBrackets ( str ) {
568- if ( str . match ( / ^ < .* > $ / ) ) {
569- str = str . slice ( 1 , - 1 ) ;
567+ // destination: DESTINATION from generalLinkRe
568+ // returns [destination, title]: no angle-brackets on destination, no quotes on title
569+ function splitIntoDestinationAndTitle ( destination ) {
570+ function unwrapAngleBrackets ( str ) {
571+ if ( str . match ( / ^ < .* > $ / ) ) {
572+ str = str . slice ( 1 , - 1 ) ;
573+ }
574+ return str ;
575+ }
576+
577+ // Valid DESTINATIONs, in decreasing specificity.
578+ var destinationAndTitleRe = / ^ ( [ ^ ' " ( ] * [ ^ \s ] ) \s + ( [ ' " ( ] .* [ ' " ) ] ) / ;
579+ var destinationRe = / ^ ( < ? [ \s \S ] * > ? ) / ;
580+ var parsingRegexes = [ destinationAndTitleRe , destinationRe ] ;
581+
582+ var match = false ;
583+ var dest = undefined ;
584+ var title = undefined ;
585+ for ( var i = 0 ; i < parsingRegexes . length ; i ++ ) {
586+ match = parsingRegexes [ i ] . exec ( destination ) ;
587+ if ( match ) {
588+ dest = match [ 1 ] ;
589+ title = match [ 2 ] ;
590+ break ;
591+ }
592+ }
593+
594+ if ( match ) {
595+ // title is optional.
596+ if ( typeof title === 'undefined' ) {
597+ title = '' ;
598+ }
599+
600+ // Format dest.
601+ dest = dest . trim ( ) ;
602+ dest = unwrapAngleBrackets ( dest ) ;
603+
604+ return [ dest , title ] ;
570605 }
571- return str ;
606+ return null ;
572607 }
573608
574609 var fullMatch = generalLinkRe . exec ( s ) ;
575610 if ( fullMatch ) {
576611 var text = fullMatch [ 1 ] ;
577612 var destination = fullMatch [ 2 ] ;
578613
579- var m ;
580-
581- var destinationAndTitleRe = / ^ ( [ ^ ' " ( ] * [ ^ \s ] ) \s + ( [ ' " ( ] .* [ ' " ) ] ) / ;
582- if ( m = destinationAndTitleRe . exec ( destination ) ) {
583- // <destination> -> destination
584- var dest1 = m [ 1 ] . trim ( ) ;
585- dest1 = unwrapAngleBrackets ( dest1 ) ;
586- var title1 = m [ 2 ] ;
587- return [ fullMatch [ 0 ] , text , dest1 , title1 ] ;
588- }
589-
590- var destinationRe = / ^ ( < ? [ \s \S ] * > ? ) / ;
591- if ( m = destinationRe . exec ( destination ) ) {
592- // <destination> -> destination
593- var dest2 = m [ 1 ] . trim ( ) ;
594- dest2 = unwrapAngleBrackets ( dest2 ) ;
595- var title2 = '' ;
596- return [ fullMatch [ 0 ] , text , dest2 , title2 ] ;
614+ // Does 'destination' match spec?
615+ var destinationAndTitle = splitIntoDestinationAndTitle ( destination ) ;
616+ if ( destinationAndTitle ) {
617+ return [ fullMatch [ 0 ] , text , destinationAndTitle [ 0 ] , destinationAndTitle [ 1 ] ] ;
597618 }
598619 }
599620 return null ;
0 commit comments