Skip to content

Commit 1fe7687

Browse files
committed
format and switch to use arg.value() and args.get(1)
1 parent 8c7d6c7 commit 1fe7687

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

src/template_helpers.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,6 @@ impl Default for MarkdownHelper {
282282
}
283283

284284
impl MarkdownHelper {
285-
286285
const ALLOW_UNSAFE: &'static str = "allow_unsafe";
287286

288287
fn new(config: &impl MarkdownConfig) -> Self {
@@ -296,9 +295,10 @@ impl MarkdownHelper {
296295
let mut options = self.system_options();
297296

298297
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+
}
302302
}
303303
}
304304

@@ -313,15 +313,14 @@ impl MarkdownHelper {
313313

314314
options
315315
}
316-
317316
}
318317

319318
impl CanHelp for MarkdownHelper {
320319
fn call(&self, args: &[PathAndJson]) -> Result<JsonValue, String> {
321320
let options = self.calculate_options(args);
322321
let as_str = match args {
323322
[v] | [v, _] => v.value(),
324-
_ => return Err("expected one argument".to_string()),
323+
_ => return Err("expected one or two arguments".to_string()),
325324
};
326325
let as_str = match as_str {
327326
JsonValue::String(s) => Cow::Borrowed(s),
@@ -593,9 +592,9 @@ fn replace_helper(text: &JsonValue, original: &JsonValue, replacement: &JsonValu
593592

594593
#[cfg(test)]
595594
mod tests {
595+
use crate::template_helpers::{rfc2822_date_helper, CanHelp, MarkdownHelper};
596596
use handlebars::{JsonValue, PathAndJson, ScopedJson};
597597
use serde_json::Value;
598-
use crate::template_helpers::{rfc2822_date_helper, CanHelp, MarkdownHelper};
599598

600599
const CONTENT_KEY: &'static str = "contents_md";
601600

@@ -673,7 +672,7 @@ mod tests {
673672
let actual = helper
674673
.call(&[
675674
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),
677676
])
678677
.unwrap();
679678

@@ -683,15 +682,18 @@ mod tests {
683682
#[test]
684683
fn test_html_blocks_are_allowed_when_allow_unsafe_is_true() {
685684
let helper = MarkdownHelper::default();
686-
let allow_unsafe = Value::Bool(true);
685+
let allow_unsafe = Value::String(String::from(MarkdownHelper::ALLOW_UNSAFE));
687686
let actual = helper
688687
.call(&as_args_with_unsafe(&contents(), &allow_unsafe))
689688
.unwrap();
690689

691690
assert_eq!(Some(UNSAFE_MARKUP), actual.as_str());
692691
}
693692

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] {
695697
[
696698
as_helper_arg(CONTENT_KEY, contents),
697699
as_helper_arg(MarkdownHelper::ALLOW_UNSAFE, allow_unsafe),
@@ -701,7 +703,6 @@ mod tests {
701703
fn contents() -> Value {
702704
Value::String(UNSAFE_MARKUP.to_string())
703705
}
704-
705706
}
706707

707708
fn as_args(contents: &Value) -> [PathAndJson; 1] {
@@ -720,6 +721,4 @@ mod tests {
720721
fn as_json_context<'a>(path: &'a str, value: &'a Value) -> ScopedJson<'a> {
721722
ScopedJson::Context(value, vec![path.to_string()])
722723
}
723-
724-
725724
}

0 commit comments

Comments
 (0)