Detect empty links on markdown rendering and issue an error (#884)

* Detect empty links on markdown rendering and issue an error

* Add a test for empty links stopping rendering with an error

* Assert error message is the expected one

When testing for empty links detection compare the error message
to make sure it's the correct error that stopped the process
and not some unrelated issue.
This commit is contained in:
Rostislav 2020-01-11 10:34:03 +01:00 committed by Vincent Prouillet
parent 0c93e15b1f
commit 145671ed20
2 changed files with 28 additions and 0 deletions

View file

@ -243,6 +243,10 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<Render
Event::Start(Tag::Image(link_type, src, title))
}
Event::Start(Tag::Link(link_type, link, title)) if link.is_empty() => {
error = Some(Error::msg("There is a link that is missing a URL"));
Event::Start(Tag::Link(link_type, "#".into(), title))
}
Event::Start(Tag::Link(link_type, link, title)) => {
let fixed_link = match fix_link(
link_type,

View file

@ -857,3 +857,27 @@ fn leaves_custom_url_scheme_untouched() {
assert_eq!(res.body, expected);
}
#[test]
fn stops_with_an_error_on_an_empty_link() {
let content = r#"[some link]()"#;
let tera_ctx = Tera::default();
let config = Config::default();
let permalinks_ctx = HashMap::new();
let context = RenderContext::new(
&tera_ctx,
&config,
"https://vincent.is/",
&permalinks_ctx,
InsertAnchor::None,
);
let res = render_content(content, &context);
let expected = "There is a link that is missing a URL";
assert!(res.is_err());
assert_eq!(res.unwrap_err().to_string(), expected);
}