Rust is an awesome systems programming language!
\n"); /// ``` pub fn text_to_html( text: &str, readme_path_in_pkg: &str, base_url: Option<&str>, pkg_path_in_vcs: Option<&str>, ) -> String { let path_in_vcs = Path::new(pkg_path_in_vcs.unwrap_or("")).join(readme_path_in_pkg); let base_dir = path_in_vcs.parent().and_then(|p| p.to_str()).unwrap_or(""); if path_in_vcs.extension().is_none() { return markdown_to_html(text, base_url, base_dir); } if let Some(ext) = path_in_vcs.extension().and_then(|ext| ext.to_str()) { if MARKDOWN_EXTENSIONS.contains(&ext.to_lowercase().as_str()) { return markdown_to_html(text, base_url, base_dir); } } encode_minimal(text).replace("\n", "foo_readme
\n<script>alert(\'Hello World\')</script>\n" ); } #[test] fn text_with_iframe_tag() { let text = "foo_readme\n\n"; let result = markdown_to_html(text, None, ""); assert_eq!( result, "foo_readme
\n<iframe>alert(\'Hello World\')</iframe>\n" ); } #[test] fn text_with_unknown_tag() { let text = "foo_readme\n\nfoo_readme
\nalert(\'Hello World\')
\n"); } #[test] fn text_with_inline_javascript() { let text = r#"foo_readme\n\nCrate page"#; let result = markdown_to_html(text, None, ""); assert_eq!( result, "foo_readme\\n\\nCrate page
\n" ); } // See https://github.com/kivikakk/comrak/issues/37. This panic happened // in comrak 0.1.8 but was fixed in 0.1.9. #[test] fn text_with_fancy_single_quotes() { let text = "wb’"; let result = markdown_to_html(text, None, ""); assert_eq!(result, "wb’
\n"); } #[test] fn code_block_with_syntax_highlighting() { let code_block = r#"```rust \ println!("Hello World"); \ ```"#; let result = markdown_to_html(code_block, None, ""); assert!(result.contains(""));
}
#[test]
fn code_block_with_syntax_highlighting_even_if_annot_has_no_run() {
let code_block = r#"```rust , no_run \
println!("Hello World"); \
```"#;
let result = markdown_to_html(code_block, None, "");
assert!(result.contains(""));
}
#[test]
fn text_with_forbidden_class_attribute() {
let text = "Hello World!
";
let result = markdown_to_html(text, None, "");
assert_eq!(result, "Hello World!
\n");
}
#[test]
fn relative_links() {
let absolute = "[hi](/hi)";
let relative = "[there](there)";
let image = "![alt](img.png)";
let html_image = "";
let svg = "![alt](sanitize.svg)";
for host in &["github.com", "gitlab.com", "bitbucket.org"] {
for (&extra_slash, &dot_git) in [true, false].iter().zip(&[true, false]) {
let url = format!(
"https://{}/rust-lang/test{}{}",
host,
if dot_git { ".git" } else { "" },
if extra_slash { "/" } else { "" },
);
let result = markdown_to_html(absolute, Some(&url), "");
assert_eq!(
result,
format!(
"\n",
host
)
);
let result = markdown_to_html(relative, Some(&url), "");
assert_eq!(
result,
format!(
"\n",
host
)
);
let result = markdown_to_html(image, Some(&url), "");
assert_eq!(
result,
format!(
"\n",
host
)
);
let result = markdown_to_html(html_image, Some(&url), "");
assert_eq!(
result,
format!(
"\n",
host
)
);
let result = markdown_to_html(svg, Some(&url), "");
assert_eq!(
result,
format!(
"\n",
host
)
);
let result = markdown_to_html(svg, Some(&url), "subdir");
assert_eq!(
result,
format!(
"\n",
host
)
);
let result = markdown_to_html(svg, Some(&url), "subdir1/subdir2");
assert_eq!(
result,
format!(
"\n",
host
)
);
}
}
let result = markdown_to_html(absolute, Some("https://google.com/"), "");
assert_eq!(
result,
"\n"
);
}
#[test]
fn absolute_links_dont_get_resolved() {
let text =
"[![Crates.io](https://img.shields.io/crates/v/clap.svg)](https://crates.io/crates/clap)";
let repository = "https://github.com/kbknapp/clap-rs/";
let result = markdown_to_html(text, Some(repository), "");
assert_eq!(
result,
"\n"
);
}
#[test]
fn text_to_html_renders_markdown() {
for f in &[
"README",
"readme.md",
"README.MARKDOWN",
"whatever.mkd",
"s/readme.md",
"s1/s2/readme.md",
] {
assert_eq!(
text_to_html("*lobster*", f, None, None),
"lobster
\n"
);
}
assert_eq!(
text_to_html("*[lobster](docs/lobster)*", "readme.md", Some("https://github.com/rust-lang/test"), None),
"\n"
);
assert_eq!(
text_to_html("*[lobster](docs/lobster)*", "s/readme.md", Some("https://github.com/rust-lang/test"), None),
"\n"
);
assert_eq!(
text_to_html("*[lobster](docs/lobster)*", "s1/s2/readme.md", Some("https://github.com/rust-lang/test"), None),
"\n"
);
assert_eq!(
text_to_html("*[lobster](docs/lobster)*", "s1/s2/readme.md", Some("https://github.com/rust-lang/test"), Some("path/in/vcs/")),
"\n"
);
assert_eq!(
text_to_html("*[lobster](docs/lobster)*", "s1/s2/readme.md", Some("https://github.com/rust-lang/test"), Some("path/in/vcs")),
"\n"
);
}
#[test]
fn text_to_html_renders_other_things() {
for f in &["readme.exe", "readem.org", "blah.adoc"] {
assert_eq!(
text_to_html("\n\nis my friend\n", f, None, None),
"<script>lobster</script>
\n
\nis my friend
\n"
);
}
}
#[test]
fn header_has_tags() {
let text = "# My crate\n\nHello, world!\n";
let result = markdown_to_html(text, None, "");
assert_eq!(
result,
"My crate
\nHello, world!
\n"
);
}
#[test]
fn manual_anchor_is_sanitized() {
let text =
"My crate
\nHello, world!
\n";
let result = markdown_to_html(text, None, "");
assert_eq!(
result,
"My crate
\nHello, world!
\n"
);
}
#[test]
fn tables_with_rowspan_and_colspan() {
let text = "Target
\n";
let result = markdown_to_html(text, None, "");
assert_eq!(
result,
"Target
\n"
);
}
#[test]
fn text_alignment() {
let text = "foo-bar
\nHello World!
\n";
let result = markdown_to_html(text, None, "");
assert_eq!(
result,
"foo-bar
\nHello World!
\n"
);
}
#[test]
fn image_alignment() {
let text =
"\n";
let result = markdown_to_html(text, None, "");
assert_eq!(
result,
"\n"
);
}
}