diff options
Diffstat (limited to 'src/generate_html.rs')
-rw-r--r-- | src/generate_html.rs | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/src/generate_html.rs b/src/generate_html.rs index 6220015..9533410 100644 --- a/src/generate_html.rs +++ b/src/generate_html.rs @@ -289,21 +289,29 @@ fn line_to_html(line: &Line, page: &Page, website: &Website) -> String { html.push_str(&format!("<a href='{path}' class='{class}'>{label}</a>")) } Token::ExternalLink { label, path } => { + let mut path = path.to_owned(); let mut label = label.to_string(); - // Strip the protocol from the path when using the path as a label. - if label.is_empty() { - label = path.to_string(); + + if !path.contains("://") { + // Check that the linked static file exists. + match website.has_static(page, &path) { + Some(resolved_path) => path = resolved_path, + None => warn!("Page {:?} contains link to nonexistent static file {path:?}", page.name), + } + // Take the file name as the label if the link is unlabeled. + if label.is_empty() { + label = match path.rsplit_once('/') { + Some((_, file)) => file.to_string(), + None => path.clone(), + }; + } + } else if label.is_empty() { for protocol in ["mailto://", "http://", "https://"] { if let Some(stripped) = path.strip_prefix(protocol) { label = stripped.to_string(); } } } - // Support absolute local paths. - let path = match path.strip_prefix('/') { - Some(stripped) => format!("{}{stripped}", page.root()), - None => path.to_string(), - }; let label = sanitize_text(&label); html.push_str(&format!("<a href='{path}' class='external'>{label}</a>")); } |