From d69ada09d20e1369b4bb993e47723f96bee51b0d Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Mon, 7 May 2018 21:23:27 +0200 Subject: [PATCH] Make permalinks out of co-located images --- components/rendering/src/markdown.rs | 10 +++++++++- components/rendering/tests/markdown.rs | 15 +++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/components/rendering/src/markdown.rs b/components/rendering/src/markdown.rs index de580e2f..78f6a5f5 100644 --- a/components/rendering/src/markdown.rs +++ b/components/rendering/src/markdown.rs @@ -108,7 +108,15 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<(Strin highlighter = None; Event::Html(Owned("".to_string())) }, - // Need to handle relative links + Event::Start(Tag::Image(src, title)) => { + if is_colocated_asset_link(&src) { + return Event::Start( + Tag::Image(Owned(format!("{}{}", context.current_page_permalink, src)), title) + ); + } + + Event::Start(Tag::Link(src, title)) + }, Event::Start(Tag::Link(link, title)) => { // A few situations here: // - it could be a relative link (starting with `./`) diff --git a/components/rendering/tests/markdown.rs b/components/rendering/tests/markdown.rs index f6eb938f..2e77b21e 100644 --- a/components/rendering/tests/markdown.rs +++ b/components/rendering/tests/markdown.rs @@ -492,9 +492,8 @@ fn can_make_valid_relative_link_in_header() { ); } - #[test] -fn can_make_permalinks_with_colocated_assets() { +fn can_make_permalinks_with_colocated_assets_for_link() { let permalinks_ctx = HashMap::new(); let config = Config::default(); let context = RenderContext::new(&GUTENBERG_TERA, &config, "https://vincent.is/about/", &permalinks_ctx, InsertAnchor::None); @@ -504,3 +503,15 @@ fn can_make_permalinks_with_colocated_assets() { "

an image

\n" ); } + +#[test] +fn can_make_permalinks_with_colocated_assets_for_image() { + let permalinks_ctx = HashMap::new(); + let config = Config::default(); + let context = RenderContext::new(&GUTENBERG_TERA, &config, "https://vincent.is/about/", &permalinks_ctx, InsertAnchor::None); + let res = render_content("![alt text](image.jpg)", &context).unwrap(); + assert_eq!( + res.0, + "

\"alt

\n" + ); +}