@@ -89,8 +89,8 @@ var Parser = (function () {
89
89
key : 'initText' ,
90
90
value : function initText ( text ) {
91
91
if ( text ) {
92
- text = text . replace ( '\t' , ' ' ) ;
93
- text = text . replace ( '\r' , '' ) ;
92
+ text = text . replace ( / \t / g , ' ' ) ;
93
+ text = text . replace ( / \r / g , '' ) ;
94
94
} else {
95
95
text = '' ;
96
96
}
@@ -119,6 +119,7 @@ var Parser = (function () {
119
119
html += '<li id="fn-' + index + '">' + val + '</li>' ;
120
120
121
121
index ++ ;
122
+ val = this . footnotes . pop ( ) ;
122
123
}
123
124
html += '</ol></div>' ;
124
125
}
@@ -134,7 +135,7 @@ var Parser = (function () {
134
135
} , {
135
136
key : 'parse' ,
136
137
value : function parse ( text ) {
137
- var _this = this ;
138
+ var _this2 = this ;
138
139
139
140
var lines = text . split ( "\n" ) ;
140
141
var blocks = this . parseBlock ( text , lines ) ;
@@ -151,9 +152,9 @@ var Parser = (function () {
151
152
var extract = lines . slice ( start , end + 1 ) ;
152
153
var method = 'parse' + type . slice ( 0 , 1 ) . toUpperCase ( ) + type . slice ( 1 ) ;
153
154
var beforeMethod = 'beforeParse' + type . slice ( 0 , 1 ) . toUpperCase ( ) + type . slice ( 1 ) ;
154
- extract = _this . call ( beforeMethod , extract , value ) ;
155
- var result = _this [ method ] ( extract , value ) ;
156
- result = _this . call ( 'after' + method . slice ( 0 , 1 ) . toUpperCase ( ) + method . slice ( 1 ) , result , value ) ;
155
+ extract = _this2 . call ( beforeMethod , extract , value ) ;
156
+ var result = _this2 [ method ] ( extract , value ) ;
157
+ result = _this2 . call ( 'after' + method . slice ( 0 , 1 ) . toUpperCase ( ) + method . slice ( 1 ) , result , value ) ;
157
158
158
159
html += result ;
159
160
} ) ;
@@ -222,14 +223,20 @@ var Parser = (function () {
222
223
var whiteList = arguments . length <= 1 || arguments [ 1 ] === undefined ? '' : arguments [ 1 ] ;
223
224
224
225
text = this . call ( 'beforeParseInline' , text ) ;
225
-
226
+ var _this = this ;
226
227
// code
227
- var codeMatches = / ( ^ | [ ^ \\ ] ) ` ( .+ ?) ` / . exec ( text ) ;
228
- if ( codeMatches ) {
229
- text = codeMatches [ 1 ] + this . makeHolder ( '<code>' + this . htmlspecialchars ( codeMatches [ 2 ] ) + '</code>' ) ;
230
- }
228
+ text = text . replace ( / ( ^ | [ ^ \\ ] ) ( ` + ) ( .+ ?) \2 / g , function ( ) {
229
+ var codeMatches = / ( ^ | [ ^ \\ ] ) ( ` + ) ( . + ? ) \2 / g . exec ( text ) ;
230
+ return codeMatches [ 1 ] + _this . makeHolder ( '<code>' + _this . htmlspecialchars ( codeMatches [ 3 ] ) + '</code>' ) ;
231
+ } ) ;
231
232
232
- // escape unsafe tags
233
+ // link
234
+ text = text . replace ( / < ( h t t p s ? : \/ \/ .+ ) > / ig, function ( ) {
235
+ var linkMatches = / < ( h t t p s ? : \/ \/ .+ ) > / ig. exec ( text ) ;
236
+ return '<a href="' + linkMatches [ 1 ] + '">' + linkMatches [ 1 ] + '</a>' ;
237
+ } ) ;
238
+
239
+ // encode unsafe tags
233
240
var unsafeTagMatches = / < ( \/ ? ) ( [ a - z 0 - 9 - ] + ) ( \s + [ ^ > ] * ) ? > / i. exec ( text ) ;
234
241
if ( unsafeTagMatches ) {
235
242
var whiteLists = this . commonWhiteList + '|' + whiteList ;
@@ -240,83 +247,84 @@ var Parser = (function () {
240
247
}
241
248
}
242
249
243
- text = text . replace ( '<' , '<' ) ;
244
- text = text . replace ( '>' , '>' ) ;
250
+ text = text . replace ( / < / g, '<' ) ;
251
+ text = text . replace ( / > / g, '>' ) ;
252
+
253
+ // footnote
254
+ var footnotePattern = / \[ \^ ( (?: [ ^ \] ] | \] | \[ ) + ?) \] / g;
245
255
246
- var footnotePattern = / \[ \^ ( (?: [ ^ \] ] | \] | \[ ) + ?) \] / ;
247
- var footnoteMatches = footnotePattern . exec ( text ) ;
248
- if ( footnoteMatches ) {
249
- var id = this . footnotes . indexOf ( footnoteMatches [ 1 ] ) ;
256
+ text = text . replace ( footnotePattern , function ( ) {
257
+ var footnoteMatches = text . match ( footnoteMatches ) ;
258
+ var id = _this . footnotes . indexOf ( footnoteMatches [ 1 ] ) ;
250
259
251
260
if ( id === - 1 ) {
252
- id = this . footnotes . length + 1 ;
253
- this . footnotes [ id ] = footnoteMatches [ 1 ] ;
261
+ id = _this . footnotes . length + 1 ;
262
+ _this . footnotes [ id ] = footnoteMatches [ 1 ] ;
254
263
}
255
264
256
- text = this . makeHolder ( '<sup id="fnref-' + id + '"><a href="#fn-' + id + '" class="footnote-ref">' + id + '</a></sup>' ) ;
257
- }
265
+ return _this . makeHolder ( '<sup id="fnref-' + id + '"><a href="#fn-' + id + '" class="footnote-ref">' + id + '</a></sup>' ) ;
266
+ } ) ;
258
267
259
268
// image
260
269
var imagePattern1 = / ! \[ ( (?: [ ^ \] ] | \] | \[ ) * ?) \] \( ( (?: [ ^ \) ] | \) | \( ) + ?) \) / ;
261
270
var imageMatches1 = imagePattern1 . exec ( text ) ;
262
- if ( imageMatches1 ) {
263
- var escaped = this . escapeBracket ( imageMatches1 [ 1 ] ) ;
264
- var url = this . escapeBracket ( imageMatches1 [ 2 ] ) ;
265
- text = this . makeHolder ( '<img src="' + url + '" alt="' + escaped + '" title="' + escaped + '">' ) ;
266
- }
271
+ text = text . replace ( imagePattern1 , function ( ) {
272
+ var escaped = _this . escapeBracket ( imageMatches1 [ 1 ] ) ;
273
+ var url = _this . escapeBracket ( imageMatches1 [ 2 ] ) ;
274
+ return _this . makeHolder ( '<img src="' + url + '" alt="' + escaped + '" title="' + escaped + '">' ) ;
275
+ } ) ;
267
276
268
277
var imagePattern2 = / ! \[ ( (?: [ ^ \] ] | \] | \[ ) * ?) \] \[ ( (?: [ ^ \] ] | \] | \[ ) + ?) \] / ;
269
278
var imageMatches2 = imagePattern2 . exec ( text ) ;
270
- if ( imageMatches2 ) {
271
- var escaped = this . escapeBracket ( imageMatches2 [ 1 ] ) ;
279
+ text = text . replace ( imagePattern2 , function ( ) {
280
+ var escaped = _this . escapeBracket ( imageMatches2 [ 1 ] ) ;
272
281
var result = '' ;
273
- if ( this . definitions [ imageMatches2 [ 2 ] ] ) {
274
- result = '<img src="' + this . definitions [ imageMatches2 [ 2 ] ] + '" alt="' + escaped + '" title="' + escaped + '">' ;
282
+ if ( _this . definitions [ imageMatches2 [ 2 ] ] ) {
283
+ result = '<img src="' + _this . definitions [ imageMatches2 [ 2 ] ] + '" alt="' + escaped + '" title="' + escaped + '">' ;
275
284
} else {
276
285
result = escaped ;
277
286
}
278
- text = this . makeHolder ( result ) ;
279
- }
287
+ return _this . makeHolder ( result ) ;
288
+ } ) ;
280
289
281
290
// link
282
291
var linkPattern1 = / \[ ( (?: [ ^ \] ] | \] | \[ ) + ?) \] \( ( (?: [ ^ \) ] | \) | \( ) + ?) \) / ;
283
292
var linkMatches1 = linkPattern1 . exec ( text ) ;
284
293
285
- if ( linkMatches1 ) {
286
- var escaped = this . escapeBracket ( linkMatches1 [ 1 ] ) ;
287
- var url = this . escapeBracket ( linkMatches1 [ 2 ] ) ;
288
- text = this . makeHolder ( '<a href="' + url + '">' + escaped + '</a>' ) ;
289
- }
294
+ text = text . replace ( linkPattern1 , function ( ) {
295
+ var escaped = _this . escapeBracket ( linkMatches1 [ 1 ] ) ;
296
+ var url = _this . escapeBracket ( linkMatches1 [ 2 ] ) ;
297
+ return _this . makeHolder ( '<a href="' + url + '">' + escaped + '</a>' ) ;
298
+ } ) ;
290
299
291
300
var linkPattern2 = / \[ ( (?: [ ^ \] ] | \] | \[ ) + ?) \] \[ ( (?: [ ^ \] ] | \] | \[ ) + ?) \] / ;
292
301
var linkMatches2 = linkPattern2 . exec ( text ) ;
293
- if ( linkMatches2 ) {
294
- var escaped = this . escapeBracket ( linkMatches2 [ 1 ] ) ;
302
+ text = text . replace ( linkPattern2 , function ( ) {
303
+ var escaped = _this . escapeBracket ( linkMatches2 [ 1 ] ) ;
295
304
296
- var result = this . definitions [ linkMatches2 [ 2 ] ] ? '<a href="' + this . definitions [ linkMatches2 [ 2 ] ] + '">' + escaped + '</a>' : escaped ;
305
+ var result = _this . definitions [ linkMatches2 [ 2 ] ] ? '<a href="' + _this . definitions [ linkMatches2 [ 2 ] ] + '">' + escaped + '</a>' : escaped ;
297
306
298
- text = this . makeHolder ( result ) ;
299
- }
307
+ return _this . makeHolder ( result ) ;
308
+ } ) ;
300
309
301
310
// escape
302
- var escapeMatches = / \\ ( ` | \* | _ | ~ ) / . exec ( text ) ;
303
- if ( escapeMatches ) {
304
- text = this . makeHolder ( this . htmlspecialchars ( escapeMatches [ 1 ] ) ) ;
305
- }
311
+ text = text . replace ( / \\ ( ` | \* | _ | ~ ) / , function ( ) {
312
+ var escapeMatches = / \\ ( ` | \* | _ | ~ ) / . exec ( text ) ;
313
+ return _this . makeHolder ( _this . htmlspecialchars ( escapeMatches [ 1 ] ) ) ;
314
+ } ) ;
306
315
307
316
// strong and em and some fuck
308
- text = text . replace ( / ( \* { 3 } ) ( .+ ?) \1/ , "<strong><em>$2</em></strong>" ) ;
309
- text = text . replace ( / ( \* { 2 } ) ( .+ ?) \1/ , "<strong>$2</strong>" ) ;
310
- text = text . replace ( / ( \* ) ( .+ ?) \1/ , "<em>$2</em>" ) ;
311
- text = text . replace ( / ( \s + ) ( _ { 3 } ) ( .+ ?) \2( \s + ) / , "$1<strong><em>$3</em></strong>$4" ) ;
312
- text = text . replace ( / ( \s + ) ( _ { 2 } ) ( .+ ?) \2( \s + ) / , "$1<strong>$3</strong>$4" ) ;
313
- text = text . replace ( / ( \s + ) ( _ ) ( .+ ?) \2( \s + ) / , "$1<em>$3</em>$4" ) ;
314
- text = text . replace ( / ( ~ { 2 } ) ( .+ ?) \1/ , "<del>$2</del>" ) ;
315
- text = text . replace ( / < ( h t t p s ? : \/ \/ .+ ) > / i, "<a href=\"$1\">$1</a>" ) ;
316
- text = text . replace ( / < ( [ _ a - z 0 - 9 -\. \+ ] + @ [ ^ @ ] + \. [ a - z ] { 2 , } ) > / i, "<a href=\"mailto:$1\">$1</a>" ) ;
317
+ text = text . replace ( / ( \* { 3 } ) ( .+ ?) \1/ g, "<strong><em>$2</em></strong>" ) ;
318
+ text = text . replace ( / ( \* { 2 } ) ( .+ ?) \1/ g, "<strong>$2</strong>" ) ;
319
+ text = text . replace ( / ( \* ) ( .+ ?) \1/ g, "<em>$2</em>" ) ;
320
+ text = text . replace ( / ( \s + ) ( _ { 3 } ) ( .+ ?) \2( \s + ) / g, "$1<strong><em>$3</em></strong>$4" ) ;
321
+ text = text . replace ( / ( \s + ) ( _ { 2 } ) ( .+ ?) \2( \s + ) / g, "$1<strong>$3</strong>$4" ) ;
322
+ text = text . replace ( / ( \s + ) ( _ ) ( .+ ?) \2( \s + ) / g, "$1<em>$3</em>$4" ) ;
323
+ text = text . replace ( / ( ~ { 2 } ) ( .+ ?) \1/ g, "<del>$2</del>" ) ;
324
+ text = text . replace ( / < ( [ _ a - z 0 - 9 -\. \+ ] + @ [ ^ @ ] + \. [ a - z ] { 2 , } ) > / ig, "<a href=\"mailto:$1\">$1</a>" ) ;
317
325
318
326
// autolink url
319
- text = text . replace ( / ( ^ | [ ^ " ] ) ( ( h t t p | h t t p s | f t p | m a i l t o ) : [ _ a - z 0 - 9 -\. \/ % # @ \? \+ = ~ \| \, ] + ) ( $ | [ ^ " ] ) / i , "$1<a href=\"$2\">$2</a>$4" ) ;
327
+ text = text . replace ( / ( ^ | [ ^ " ] ) ( ( h t t p | h t t p s | f t p | m a i l t o ) : [ _ a - z 0 - 9 -\. \/ % # @ \? \+ = ~ \| \, ] + ) ( $ | [ ^ " ] ) / ig , "$1<a href=\"$2\">$2</a>$4" ) ;
320
328
321
329
text = this . call ( 'afterParseInlineBeforeRelease' , text ) ;
322
330
@@ -414,7 +422,7 @@ var Parser = (function () {
414
422
415
423
// footnote
416
424
case / ^ \[ \^ ( (?: [ ^ \] ] | \] | \[ ) + ?) \] : / . test ( line ) :
417
- var footnoteMatches = line . match ( / ^ \[ \^ ( (?: [ ^ \] ] | \] | \[ ) + ?) \] : / ) ;
425
+ var footnoteMatches = / ^ \[ \^ ( (?: [ ^ \] ] | \] | \[ ) + ?) \] : / . exec ( line ) ;
418
426
var footnoteSpace = footnoteMatches [ 0 ] . length - 1 ;
419
427
this . startBlock ( 'footnote' , key , [ footnoteSpace , footnoteMatches [ 1 ] ] ) ;
420
428
break ;
@@ -625,7 +633,7 @@ var Parser = (function () {
625
633
626
634
if ( 'normal' === type ) {
627
635
// combine two splitted list
628
- if ( from === to && lines [ from ] . match ( / ^ \s * $ / ) && prevBlock . length && nextBlock . length ) {
636
+ if ( from === to && lines [ from ] . match ( / ^ \s * $ / ) && prevBlock && nextBlock ) {
629
637
if ( prevBlock [ 0 ] === 'list' && nextBlock [ 0 ] === 'list' ) {
630
638
// combine 3 blocks
631
639
blocks [ key - 1 ] = [ 'list' , prevBlock [ 1 ] , nextBlock [ 2 ] , null ] ;
@@ -897,7 +905,7 @@ var Parser = (function () {
897
905
} , {
898
906
key : 'parseTable' ,
899
907
value : function parseTable ( lines , value ) {
900
- var _this2 = this ;
908
+ var _this3 = this ;
901
909
902
910
var _value = _slicedToArray ( value , 2 ) ;
903
911
@@ -976,7 +984,7 @@ var Parser = (function () {
976
984
html += ' align="' + aligns [ key ] + '"' ;
977
985
}
978
986
979
- html += '>' + _this2 . parseInline ( text ) + ( '</' + tag + '>' ) ;
987
+ html += '>' + _this3 . parseInline ( text ) + ( '</' + tag + '>' ) ;
980
988
} ) ;
981
989
982
990
html += '</tr>' ;
@@ -1022,15 +1030,15 @@ var Parser = (function () {
1022
1030
} , {
1023
1031
key : 'parseNormal' ,
1024
1032
value : function parseNormal ( lines ) {
1025
- var _this3 = this ;
1033
+ var _this4 = this ;
1026
1034
1027
1035
lines = lines . map ( function ( line ) {
1028
- return _this3 . parseInline ( line ) ;
1036
+ return _this4 . parseInline ( line ) ;
1029
1037
} ) ;
1030
1038
1031
1039
var str = lines . join ( "\n" ) . trim ( ) ;
1032
- str = str . replace ( / ( \n \s * ) { 2 , } / , "</p><p>" ) ;
1033
- str = str . replace ( / \n / , "<br>" ) ;
1040
+ str = str . replace ( / ( \n \s * ) { 2 , } / g , "</p><p>" ) ;
1041
+ str = str . replace ( / \n / g , "<br>" ) ;
1034
1042
1035
1043
return ( / ^ \s * $ / . test ( str ) ? '' : '<p>' + str + '</p>'
1036
1044
) ;
@@ -1052,8 +1060,7 @@ var Parser = (function () {
1052
1060
var note = _value2 [ 1 ] ;
1053
1061
1054
1062
var index = this . footnotes . indexOf ( note ) ;
1055
-
1056
- if ( false !== index ) {
1063
+ if ( - 1 !== index ) {
1057
1064
if ( lines [ 0 ] ) {
1058
1065
lines [ 0 ] = lines [ 0 ] . replace ( / ^ \[ \^ ( (?: [ ^ \] ] | \] | \[ ) + ?) \] : / , '' ) ;
1059
1066
}
@@ -1120,10 +1127,10 @@ var Parser = (function () {
1120
1127
key : 'escapeBracket' ,
1121
1128
value : function escapeBracket ( str ) {
1122
1129
if ( str ) {
1123
- str = str . replace ( '\[' , '[' ) ;
1124
- str = str . replace ( '\]' , ']' ) ;
1125
- str = str . replace ( '\(' , '(' ) ;
1126
- str = str . replace ( '\)' , ')' ) ;
1130
+ str = str . replace ( / \[ / g , '[' ) ;
1131
+ str = str . replace ( / \] / g , ']' ) ;
1132
+ str = str . replace ( / \( / g , '(' ) ;
1133
+ str = str . replace ( / \) / g , ')' ) ;
1127
1134
return str ;
1128
1135
}
1129
1136
}
0 commit comments