4
4
* https://github.com/markedjs/marked
5
5
*/
6
6
7
+ // Return str with all trailing {c | all but c} removed
8
+ // allButC: Default false
9
+ function rtrim ( str , c , allButC ) {
10
+ if ( typeof ( allButC ) === 'undefined' ) {
11
+ allButC = false ;
12
+ } else {
13
+ allButC = true ;
14
+ }
15
+
16
+ if ( str . length === 0 ) {
17
+ return '' ;
18
+ }
19
+
20
+ // ix+1 of leftmost that fits description
21
+ // i.e. the length of the string we should return
22
+ var curr = str . length ;
23
+
24
+ while ( 0 < curr &&
25
+ ( ( allButC && str . charAt ( curr - 1 ) !== c ) ||
26
+ ( ! allButC && str . charAt ( curr - 1 ) === c ) ) ) {
27
+ curr -- ;
28
+ }
29
+
30
+ return str . substr ( 0 , curr ) ;
31
+ }
32
+
7
33
; ( function ( root ) {
8
34
'use strict' ;
9
35
@@ -216,7 +242,7 @@ Lexer.prototype.token = function(src, top) {
216
242
this . tokens . push ( {
217
243
type : 'code' ,
218
244
text : ! this . options . pedantic
219
- ? cap . replace ( / \n + $ / , '' )
245
+ ? rtrim ( cap , '\n ' )
220
246
: cap
221
247
} ) ;
222
248
continue ;
@@ -238,15 +264,11 @@ Lexer.prototype.token = function(src, top) {
238
264
src = src . substring ( cap [ 0 ] . length ) ;
239
265
// cap[2] might be ' HEADING # '
240
266
item = ( cap [ 2 ] || '' ) . trim ( ) ;
241
- if ( item . slice ( - 1 ) === '#' ) {
242
- // NB replace(/#+$/) is quadratic on mismatch because it's unanchored,
243
- // so we protect with if-check to ensure it won't mismatch.
244
- if ( this . options . pedantic ) {
245
- item = item . replace ( / # + $ / , '' ) ;
246
- } else {
247
- // CM requires a space before additional #s
248
- item = item . replace ( / ( \s | ^ ) # + $ / , '' ) ;
249
- }
267
+ if ( this . options . pedantic ) {
268
+ item = rtrim ( item , '#' ) ;
269
+ } else {
270
+ // CM requires a space before additional #s
271
+ item = item . replace ( / ( \s | ^ ) # + $ / , '' ) ;
250
272
}
251
273
item = item . trim ( ) ;
252
274
this . tokens . push ( {
@@ -1278,7 +1300,7 @@ function resolveUrl(base, href) {
1278
1300
if ( / ^ [ ^ : ] + : \/ * [ ^ / ] * $ / . test ( base ) ) {
1279
1301
baseUrls [ ' ' + base ] = base + '/' ;
1280
1302
} else {
1281
- baseUrls [ ' ' + base ] = base . replace ( / [ ^ / ] * $ / , '' ) ;
1303
+ baseUrls [ ' ' + base ] = rtrim ( base , '/' , true ) ;
1282
1304
}
1283
1305
}
1284
1306
base = baseUrls [ ' ' + base ] ;
0 commit comments