Skip to content

Commit

Permalink
Fix broken links to images in summary
Browse files Browse the repository at this point in the history
Restores the change of relative to absolute links for colocated files
that was removed in commit 4086b07 / PR getzola#1582.
  • Loading branch information
Yaroslav-95 committed May 4, 2022
1 parent 1fbe6fb commit 0f8fd53
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
31 changes: 31 additions & 0 deletions components/rendering/src/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use lazy_static::lazy_static;
use pulldown_cmark as cmark;
use regex::Regex;

use crate::context::RenderContext;
use crate::table_of_contents::{make_table_of_contents, Heading};
Expand Down Expand Up @@ -64,6 +65,26 @@ fn is_external_link(link: &str) -> bool {
link.starts_with("http:") || link.starts_with("https:")
}

/// 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();
}
PATTERN.is_match(s)
}

/// Colocated asset links refers to the files in the same directory,
/// there it shouldn't start with / or a schema
fn is_colocated_asset_link(link: &str) -> bool {
!link.starts_with("/") && !starts_with_schema(link)
}

fn fix_link(
link_type: LinkType,
link: &str,
Expand All @@ -89,6 +110,8 @@ fn fix_link(
return Err(format!("Relative link {} not found.", link).into());
}
}
} else if is_colocated_asset_link(link) {
format!("{}{}", context.current_page_permalink, link)
} else {
if is_external_link(link) {
external_links.push(link.to_owned());
Expand Down Expand Up @@ -298,6 +321,14 @@ pub fn markdown_to_html(
code_block = None;
events.push(Event::Html("</code></pre>\n".into()));
}
Event::Start(Tag::Image(link_type, src, title)) => {
if is_colocated_asset_link(&src) {
let link = format!("{}{}", context.current_page_permalink, &*src);
events.push(Event::Start(Tag::Image(link_type, link.into(), title)));
} else {
events.push(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"));
events.push(Event::Start(Tag::Link(link_type, "#".into(), title)));
Expand Down
4 changes: 2 additions & 2 deletions components/rendering/tests/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ fn can_make_permalinks_with_colocated_assets_for_link() {
InsertAnchor::None,
);
let res = render_content("[an image](image.jpg)", &context).unwrap();
assert_eq!(res.body, "<p><a href=\"image.jpg\">an image</a></p>\n");
assert_eq!(res.body, "<p><a href=\"https://vincent.is/about/image.jpg\">an image</a></p>\n");
}

#[test]
Expand All @@ -1033,7 +1033,7 @@ fn can_make_permalinks_with_colocated_assets_for_image() {
InsertAnchor::None,
);
let res = render_content("![alt text](image.jpg)", &context).unwrap();
assert_eq!(res.body, "<p><img src=\"image.jpg\" alt=\"alt text\" /></p>\n");
assert_eq!(res.body, "<p><img src=\"https://vincent.is/about/image.jpg\" alt=\"alt text\" /></p>\n");
}

#[test]
Expand Down

0 comments on commit 0f8fd53

Please sign in to comment.