@@ -282,7 +282,6 @@ impl Default for MarkdownHelper {
282
282
}
283
283
284
284
impl MarkdownHelper {
285
-
286
285
const ALLOW_UNSAFE : & ' static str = "allow_unsafe" ;
287
286
288
287
fn new ( config : & impl MarkdownConfig ) -> Self {
@@ -296,9 +295,10 @@ impl MarkdownHelper {
296
295
let mut options = self . system_options ( ) ;
297
296
298
297
if !options. compile . allow_dangerous_html && args. len ( ) > 1 {
299
- let arg = & args[ 1 ] ;
300
- if arg. relative_path ( ) == Some ( & Self :: ALLOW_UNSAFE . to_string ( ) ) {
301
- options. compile . allow_dangerous_html = arg. value ( ) == & JsonValue :: Bool ( true )
298
+ if let Some ( arg) = args. get ( 1 ) {
299
+ if arg. value ( ) . as_str ( ) == Some ( Self :: ALLOW_UNSAFE ) {
300
+ options. compile . allow_dangerous_html = true
301
+ }
302
302
}
303
303
}
304
304
@@ -313,15 +313,14 @@ impl MarkdownHelper {
313
313
314
314
options
315
315
}
316
-
317
316
}
318
317
319
318
impl CanHelp for MarkdownHelper {
320
319
fn call ( & self , args : & [ PathAndJson ] ) -> Result < JsonValue , String > {
321
320
let options = self . calculate_options ( args) ;
322
321
let as_str = match args {
323
322
[ v] | [ v, _] => v. value ( ) ,
324
- _ => return Err ( "expected one argument " . to_string ( ) ) ,
323
+ _ => return Err ( "expected one or two arguments " . to_string ( ) ) ,
325
324
} ;
326
325
let as_str = match as_str {
327
326
JsonValue :: String ( s) => Cow :: Borrowed ( s) ,
@@ -593,9 +592,9 @@ fn replace_helper(text: &JsonValue, original: &JsonValue, replacement: &JsonValu
593
592
594
593
#[ cfg( test) ]
595
594
mod tests {
595
+ use crate :: template_helpers:: { rfc2822_date_helper, CanHelp , MarkdownHelper } ;
596
596
use handlebars:: { JsonValue , PathAndJson , ScopedJson } ;
597
597
use serde_json:: Value ;
598
- use crate :: template_helpers:: { rfc2822_date_helper, CanHelp , MarkdownHelper } ;
599
598
600
599
const CONTENT_KEY : & ' static str = "contents_md" ;
601
600
@@ -673,7 +672,7 @@ mod tests {
673
672
let actual = helper
674
673
. call ( & [
675
674
as_helper_arg ( CONTENT_KEY , & contents ( ) ) ,
676
- to_path_and_json ( MarkdownHelper :: ALLOW_UNSAFE , allow_unsafe)
675
+ to_path_and_json ( MarkdownHelper :: ALLOW_UNSAFE , allow_unsafe) ,
677
676
] )
678
677
. unwrap ( ) ;
679
678
@@ -683,15 +682,18 @@ mod tests {
683
682
#[ test]
684
683
fn test_html_blocks_are_allowed_when_allow_unsafe_is_true ( ) {
685
684
let helper = MarkdownHelper :: default ( ) ;
686
- let allow_unsafe = Value :: Bool ( true ) ;
685
+ let allow_unsafe = Value :: String ( String :: from ( MarkdownHelper :: ALLOW_UNSAFE ) ) ;
687
686
let actual = helper
688
687
. call ( & as_args_with_unsafe ( & contents ( ) , & allow_unsafe) )
689
688
. unwrap ( ) ;
690
689
691
690
assert_eq ! ( Some ( UNSAFE_MARKUP ) , actual. as_str( ) ) ;
692
691
}
693
692
694
- fn as_args_with_unsafe < ' a > ( contents : & ' a Value , allow_unsafe : & ' a Value ) -> [ PathAndJson < ' a > ; 2 ] {
693
+ fn as_args_with_unsafe < ' a > (
694
+ contents : & ' a Value ,
695
+ allow_unsafe : & ' a Value ,
696
+ ) -> [ PathAndJson < ' a > ; 2 ] {
695
697
[
696
698
as_helper_arg ( CONTENT_KEY , contents) ,
697
699
as_helper_arg ( MarkdownHelper :: ALLOW_UNSAFE , allow_unsafe) ,
@@ -701,7 +703,6 @@ mod tests {
701
703
fn contents ( ) -> Value {
702
704
Value :: String ( UNSAFE_MARKUP . to_string ( ) )
703
705
}
704
-
705
706
}
706
707
707
708
fn as_args ( contents : & Value ) -> [ PathAndJson ; 1 ] {
@@ -720,6 +721,4 @@ mod tests {
720
721
fn as_json_context < ' a > ( path : & ' a str , value : & ' a Value ) -> ScopedJson < ' a > {
721
722
ScopedJson :: Context ( value, vec ! [ path. to_string( ) ] )
722
723
}
723
-
724
-
725
724
}
0 commit comments