From 145671ed2078815d17594cbe2de7ab16edaa1ef3 Mon Sep 17 00:00:00 2001 From: Rostislav Date: Sat, 11 Jan 2020 10:34:03 +0100 Subject: [PATCH] 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. --- components/rendering/src/markdown.rs | 4 ++++ components/rendering/tests/markdown.rs | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/components/rendering/src/markdown.rs b/components/rendering/src/markdown.rs index d326595..9d48206 100644 --- a/components/rendering/src/markdown.rs +++ b/components/rendering/src/markdown.rs @@ -243,6 +243,10 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result { + 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, diff --git a/components/rendering/tests/markdown.rs b/components/rendering/tests/markdown.rs index dece103..338f19e 100644 --- a/components/rendering/tests/markdown.rs +++ b/components/rendering/tests/markdown.rs @@ -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); +} \ No newline at end of file