Browse Source

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.
index-subcmd
Rostislav Vincent Prouillet 4 years ago
parent
commit
145671ed20
2 changed files with 28 additions and 0 deletions
  1. +4
    -0
      components/rendering/src/markdown.rs
  2. +24
    -0
      components/rendering/tests/markdown.rs

+ 4
- 0
components/rendering/src/markdown.rs 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,


+ 24
- 0
components/rendering/tests/markdown.rs 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);
}

Loading…
Cancel
Save