Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rendering: Avoid prepending URL prefix to links that start with a scheme #817

Merged
merged 1 commit into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 61 additions & 2 deletions components/rendering/src/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use pulldown_cmark as cmark;
use regex::Regex;
use slug::slugify;
use syntect::easy::HighlightLines;
use syntect::html::{
Expand Down Expand Up @@ -60,11 +61,31 @@ fn find_anchor(anchors: &[String], name: String, level: u8) -> String {
find_anchor(anchors, name, level + 1)
}

// Returns whether the given string starts with a schema.
//
// Although there exists [a list of registered URI schemes][uri-schemes], a link may use arbitrary,
// private schemes. This function checks if the given string starts with something that just looks
// like a scheme, i.e., a case-insensitive identifier followed by a colon.
//
// [uri-schemes]: https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml
fn starts_with_schema(s: &str) -> bool {
lazy_static! {
static ref PATTERN: Regex = Regex::new(r"^[0-9A-Za-z\-]+:").unwrap();
Keats marked this conversation as resolved.
Show resolved Hide resolved
}

PATTERN.is_match(s)
}

// Colocated asset links refers to the files in the same directory,
// there it should be a filename only
fn is_colocated_asset_link(link: &str) -> bool {
!link.contains('/') // http://, ftp://, ../ etc
&& !link.starts_with("mailto:")
&& !starts_with_schema(link)
}

// Returns whether a link starts with an HTTP(s) scheme.
fn is_external_link(link: &str) -> bool {
link.starts_with("http:") || link.starts_with("https:")
}

fn fix_link(
Expand Down Expand Up @@ -103,7 +124,7 @@ fn fix_link(
} else if is_colocated_asset_link(&link) {
format!("{}{}", context.current_page_permalink, link)
} else {
if !link.starts_with('#') && !link.starts_with("mailto:") {
if is_external_link(link) {
external_links.push(link.to_owned());
}
link.to_string()
Expand Down Expand Up @@ -328,3 +349,41 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<Render
})
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_starts_with_schema() {
// registered
assert!(starts_with_schema("https://example.com/"));
assert!(starts_with_schema("ftp://example.com/"));
assert!(starts_with_schema("mailto:[email protected]"));
assert!(starts_with_schema("xmpp:[email protected]"));
assert!(starts_with_schema("tel:18008675309"));
assert!(starts_with_schema("sms:18008675309"));
assert!(starts_with_schema("h323:[email protected]"));

// arbitrary
assert!(starts_with_schema("zola:post?content=hi"));

// case-insensitive
assert!(starts_with_schema("MailTo:[email protected]"));
assert!(starts_with_schema("MAILTO:[email protected]"));
}

#[test]
fn test_is_external_link() {
assert!(is_external_link("http://example.com/"));
assert!(is_external_link("https://example.com/"));
assert!(is_external_link("https://example.com/index.html#introduction"));

assert!(!is_external_link("mailto:[email protected]"));
assert!(!is_external_link("tel:18008675309"));

assert!(!is_external_link("#introduction"));

assert!(!is_external_link("http.jpg"))
}
}
30 changes: 26 additions & 4 deletions components/rendering/tests/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -821,12 +821,34 @@ fn doesnt_try_to_highlight_content_from_shortcode() {
//}

// https://github.com/getzola/zola/issues/747
// https://github.com/getzola/zola/issues/816
#[test]
fn leaves_custom_url_scheme_untouched() {
let content = r#"[[email protected]](xmpp:[email protected])

[(123) 456-7890](tel:+11234567890)

[blank page](about:blank)
"#;

let tera_ctx = Tera::default();
let permalinks_ctx = HashMap::new();
let config = Config::default();
let context = RenderContext::new(&tera_ctx, &config, "", &permalinks_ctx, InsertAnchor::None);
let res = render_content("[[email protected]](xmpp:[email protected])", &context).unwrap();
assert_eq!(res.body, "<p><a href=\"xmpp:[email protected]\">[email protected]</a></p>\n");
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).unwrap();

let expected = r#"<p><a href="xmpp:[email protected]">[email protected]</a></p>
<p><a href="tel:+11234567890">(123) 456-7890</a></p>
<p><a href="about:blank">blank page</a></p>
"#;

assert_eq!(res.body, expected);
}