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
+ var mustMatchC = ! allButC ;
16
+
17
+ if ( str . length === 0 ) {
18
+ return '' ;
19
+ }
20
+
21
+ // ix+1 of leftmost that fits description
22
+ // i.e. the length of the string we should return
23
+ var curr = str . length ;
24
+
25
+ while ( curr > 0 ) {
26
+ var currChar = str . charAt ( curr - 1 ) ;
27
+ if ( mustMatchC && currChar === c ) {
28
+ curr -- ;
29
+ } else if ( ! mustMatchC && currChar !== c ) {
30
+ curr -- ;
31
+ } else {
32
+ break ;
33
+ }
34
+ }
35
+
36
+ return str . substr ( 0 , curr ) ;
37
+ }
38
+
7
39
; ( function ( root ) {
8
40
'use strict' ;
9
41
@@ -216,7 +248,7 @@ Lexer.prototype.token = function(src, top) {
216
248
this . tokens . push ( {
217
249
type : 'code' ,
218
250
text : ! this . options . pedantic
219
- ? cap . replace ( / \n + $ / , '' )
251
+ ? rtrim ( cap , '\n ' )
220
252
: cap
221
253
} ) ;
222
254
continue ;
@@ -238,15 +270,11 @@ Lexer.prototype.token = function(src, top) {
238
270
src = src . substring ( cap [ 0 ] . length ) ;
239
271
// cap[2] might be ' HEADING # '
240
272
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
- }
273
+ if ( this . options . pedantic ) {
274
+ item = rtrim ( item , '#' ) ;
275
+ } else {
276
+ // CM requires a space before additional #s
277
+ item = item . replace ( / ( \s | ^ ) # + $ / , '' ) ;
250
278
}
251
279
item = item . trim ( ) ;
252
280
this . tokens . push ( {
@@ -1288,7 +1316,7 @@ function resolveUrl(base, href) {
1288
1316
if ( / ^ [ ^ : ] + : \/ * [ ^ / ] * $ / . test ( base ) ) {
1289
1317
baseUrls [ ' ' + base ] = base + '/' ;
1290
1318
} else {
1291
- baseUrls [ ' ' + base ] = base . replace ( / [ ^ / ] * $ / , '' ) ;
1319
+ baseUrls [ ' ' + base ] = rtrim ( base , '/' , true ) ;
1292
1320
}
1293
1321
}
1294
1322
base = baseUrls [ ' ' + base ] ;
0 commit comments