55 */
66
77; ( function ( ) {
8+ 'use strict' ;
89
910/**
1011 * Block-Level Grammar
@@ -449,21 +450,21 @@ Lexer.prototype.token = function(src, top, bq) {
449450
450451var inline = {
451452 escape : / ^ \\ ( [ \\ ` * { } \[ \] ( ) # + \- . ! _ > ] ) / ,
452- autolink : / ^ < ( [ ^ > ] + ( @ | : \/ ) [ ^ > ] + ) > / ,
453+ autolink : / ^ < ( [ ^ < > ] + ( @ | : \/ ) [ ^ < > ] + ) > / ,
453454 url : noop ,
454- tag : / ^ < ! - - [ \s \S ] * ?- - > | ^ < \/ ? \w + (?: " [ ^ " ] * " | ' [ ^ ' ] * ' | [ ^ ' " > ] ) * ?> / ,
455+ tag : / ^ < ! - - [ \s \S ] * ?- - > | ^ < \/ ? \w + (?: " [ ^ " ] * " | ' [ ^ ' ] * ' | [ ^ < ' " > ] ) * ?> / ,
455456 link : / ^ ! ? \[ ( i n s i d e ) \] \( h r e f \) / ,
456457 reflink : / ^ ! ? \[ ( i n s i d e ) \] \s * \[ ( [ ^ \] ] * ) \] / ,
457458 nolink : / ^ ! ? \[ ( (?: \[ [ ^ \] ] * \] | [ ^ \[ \] ] ) * ) \] / ,
458459 strong : / ^ _ _ ( [ \s \S ] + ?) _ _ (? ! _ ) | ^ \* \* ( [ \s \S ] + ?) \* \* (? ! \* ) / ,
459460 em : / ^ \b _ ( (?: [ ^ _ ] | _ _ ) + ?) _ \b | ^ \* ( (?: \* \* | [ \s \S ] ) + ?) \* (? ! \* ) / ,
460- code : / ^ ( ` + ) \s * ( [ \s \S ] * ?[ ^ ` ] ) \s * \1(? ! ` ) / ,
461+ code : / ^ ( ` + ) ( [ \s \S ] * ?[ ^ ` ] ) \1(? ! ` ) / ,
461462 br : / ^ { 2 , } \n (? ! \s * $ ) / ,
462463 del : noop ,
463464 text : / ^ [ \s \S ] + ?(? = [ \\ < ! \[ _ * ` ] | { 2 , } \n | $ ) /
464465} ;
465466
466- inline . _inside = / (?: \[ [ ^ \] ] * \] | [ ^ \[ \] ] | \] (? = [ ^ \[ ] * \] ) ) * / ;
467+ inline . _inside = / (?: \[ [ ^ \] ] * \] | \\ [ \[ \] ] | [ ^ \[ \] ] | \] (? = [ ^ \[ ] * \] ) ) * / ;
467468inline . _href = / \s * < ? ( [ \s \S ] * ?) > ? (?: \s + [ ' " ] ( [ \s \S ] * ?) [ ' " ] ) ? \s * / ;
468469
469470inline . link = replace ( inline . link )
@@ -578,9 +579,11 @@ InlineLexer.prototype.output = function(src) {
578579 if ( cap = this . rules . autolink . exec ( src ) ) {
579580 src = src . substring ( cap [ 0 ] . length ) ;
580581 if ( cap [ 2 ] === '@' ) {
581- text = cap [ 1 ] . charAt ( 6 ) === ':'
582+ text = escape (
583+ cap [ 1 ] . charAt ( 6 ) === ':'
582584 ? this . mangle ( cap [ 1 ] . substring ( 7 ) )
583- : this . mangle ( cap [ 1 ] ) ;
585+ : this . mangle ( cap [ 1 ] )
586+ ) ;
584587 href = this . mangle ( 'mailto:' ) + text ;
585588 } else {
586589 text = escape ( cap [ 1 ] ) ;
@@ -661,7 +664,7 @@ InlineLexer.prototype.output = function(src) {
661664 // code
662665 if ( cap = this . rules . code . exec ( src ) ) {
663666 src = src . substring ( cap [ 0 ] . length ) ;
664- out += this . renderer . codespan ( escape ( cap [ 2 ] , true ) ) ;
667+ out += this . renderer . codespan ( escape ( cap [ 2 ] . trim ( ) , true ) ) ;
665668 continue ;
666669 }
667670
@@ -873,12 +876,15 @@ Renderer.prototype.link = function(href, title, text) {
873876 . replace ( / [ ^ \w : ] / g, '' )
874877 . toLowerCase ( ) ;
875878 } catch ( e ) {
876- return '' ;
879+ return text ;
877880 }
878- if ( prot . indexOf ( 'javascript:' ) === 0 || prot . indexOf ( 'vbscript:' ) === 0 ) {
879- return '' ;
881+ if ( prot . indexOf ( 'javascript:' ) === 0 || prot . indexOf ( 'vbscript:' ) === 0 || prot . indexOf ( 'data:' ) === 0 ) {
882+ return text ;
880883 }
881884 }
885+ if ( this . options . baseUrl && ! originIndependentUrl . test ( href ) ) {
886+ href = resolveUrl ( this . options . baseUrl , href ) ;
887+ }
882888 var out = '<a href="' + href + '"' ;
883889 if ( title ) {
884890 out += ' title="' + title + '"' ;
@@ -888,6 +894,9 @@ Renderer.prototype.link = function(href, title, text) {
888894} ;
889895
890896Renderer . prototype . image = function ( href , title , text ) {
897+ if ( this . options . baseUrl && ! originIndependentUrl . test ( href ) ) {
898+ href = resolveUrl ( this . options . baseUrl , href ) ;
899+ }
891900 var out = '<img src="' + href + '" alt="' + text + '"' ;
892901 if ( title ) {
893902 out += ' title="' + title + '"' ;
@@ -1094,8 +1103,8 @@ function escape(html, encode) {
10941103}
10951104
10961105function unescape ( html ) {
1097- // explicitly match decimal, hex, and named HTML entities
1098- return html . replace ( / & ( # (?: \d + ) | (?: # x [ 0 - 9 A - F a - f ] + ) | (?: \w + ) ) ; ? / g , function ( _ , n ) {
1106+ // explicitly match decimal, hex, and named HTML entities
1107+ return html . replace ( / & ( # (?: \d + ) | (?: # x [ 0 - 9 A - F a - f ] + ) | (?: \w + ) ) ; ? / ig , function ( _ , n ) {
10991108 n = n . toLowerCase ( ) ;
11001109 if ( n === 'colon' ) return ':' ;
11011110 if ( n . charAt ( 0 ) === '#' ) {
@@ -1119,6 +1128,30 @@ function replace(regex, opt) {
11191128 } ;
11201129}
11211130
1131+ function resolveUrl ( base , href ) {
1132+ if ( ! baseUrls [ ' ' + base ] ) {
1133+ // we can ignore everything in base after the last slash of its path component,
1134+ // but we might need to add _that_
1135+ // https://tools.ietf.org/html/rfc3986#section-3
1136+ if ( / ^ [ ^ : ] + : \/ * [ ^ / ] * $ / . test ( base ) ) {
1137+ baseUrls [ ' ' + base ] = base + '/' ;
1138+ } else {
1139+ baseUrls [ ' ' + base ] = base . replace ( / [ ^ / ] * $ / , '' ) ;
1140+ }
1141+ }
1142+ base = baseUrls [ ' ' + base ] ;
1143+
1144+ if ( href . slice ( 0 , 2 ) === '//' ) {
1145+ return base . replace ( / : [ \s \S ] * / , ':' ) + href ;
1146+ } else if ( href . charAt ( 0 ) === '/' ) {
1147+ return base . replace ( / ( : \/ * [ ^ / ] * ) [ \s \S ] * / , '$1' ) + href ;
1148+ } else {
1149+ return base + href ;
1150+ }
1151+ }
1152+ var baseUrls = { } ;
1153+ var originIndependentUrl = / ^ $ | ^ [ a - z ] [ a - z 0 - 9 + . - ] * : | ^ [ ? # ] / i;
1154+
11221155function noop ( ) { }
11231156noop . exec = noop ;
11241157
@@ -1220,7 +1253,7 @@ function marked(src, opt, callback) {
12201253 } catch ( e ) {
12211254 e . message += '\nPlease report this to https://github.com/chjj/marked.' ;
12221255 if ( ( opt || marked . defaults ) . silent ) {
1223- return '<p>An error occured :</p><pre>'
1256+ return '<p>An error occurred :</p><pre>'
12241257 + escape ( e . message + '' , true )
12251258 + '</pre>' ;
12261259 }
@@ -1253,7 +1286,8 @@ marked.defaults = {
12531286 smartypants : false ,
12541287 headerPrefix : '' ,
12551288 renderer : new Renderer ,
1256- xhtml : false
1289+ xhtml : false ,
1290+ baseUrl : null
12571291} ;
12581292
12591293/**
0 commit comments