Skip to content

Commit

Permalink
Add support for relative image source paths with no prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
cheap-glitch committed Mar 30, 2022
1 parent 884dfbe commit f6e55e2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
27 changes: 27 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ log = "0.4"
notify = "4.0.17"
pathbuftools = "0.1.2"
pulldown-cmark = { version = "0.9.0", default-features = false, features = ["simd"] }
regex = "1.5.4"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.74"
serde_yaml = "0.8.23"
Expand Down
8 changes: 6 additions & 2 deletions src/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::fs;
use std::io;
use std::path::{PathBuf, Path};
use std::collections::HashSet;
use regex::Regex;
use pulldown_cmark::{Parser, Options, Event, html};

/// Encapsulates a markdown file and provides an interface to turn its contents into HTML.
Expand Down Expand Up @@ -35,6 +36,9 @@ impl Renderer {
let markdown = fs::read_to_string(&self.canonical_md_path)?;
let root_dir = self.canonical_md_path.parent().unwrap_or_else(|| Path::new(""));

let re_absolute_url = Regex::new(r"^[a-z]+://").unwrap();
let re_path_prefix = Regex::new(r"^(/|\./)?").unwrap();

let mut options = Options::empty();
options.insert(Options::ENABLE_TABLES);
options.insert(Options::ENABLE_FOOTNOTES);
Expand All @@ -52,8 +56,8 @@ impl Renderer {
languages.insert(content.to_string());
}
},
Event::Start(Tag::Image(_, url, _)) if url.starts_with("./") => {
*url = format!("file://{}/{}", root_dir.display(), &url[2..]).into();
Event::Start(Tag::Image(_, url, _)) if !re_absolute_url.is_match(url) => {
*url = format!("file://{}/{}", root_dir.display(), re_path_prefix.replace(url, "")).into();
},
_ => (),
}
Expand Down
23 changes: 14 additions & 9 deletions tests/test_markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,22 @@ fn test_renders_local_images() {
let mut file = NamedTempFile::new().unwrap();
let tempdir = file.path().parent().unwrap().to_path_buf();

writeln!(file, "![demo image](./local-image.png)").unwrap();
writeln!(file, "![demo image](http://remote-image.png)").unwrap();
writeln!(file, "![demo image](unprefixed_image.png)").unwrap();
writeln!(file, "![demo image](local-image-01.png)").unwrap();
writeln!(file, "![demo image](.local-image-02.png)").unwrap();
writeln!(file, "![demo image](/local-image-03.png)").unwrap();
writeln!(file, "![demo image](./local-image-04.png)").unwrap();
writeln!(file, "![demo image](../local-image-05.png)").unwrap();
writeln!(file, "![demo image](http://remote-image-01.png)").unwrap();
writeln!(file, "![demo image](https://remote-image-02.png)").unwrap();

let renderer = Renderer::new(file.path().to_path_buf());
let content = renderer.run().unwrap();

let local_src = format!("src=\"file://{}/local-image.png\"", tempdir.display());
assert!(content.html.contains(&local_src));
assert!(content.html.contains("src=\"http://remote-image.png\""));

// Without a ./ prefix, it's left alone
assert!(content.html.contains("src=\"unprefixed_image.png\""));
assert!(content.html.contains(&format!("src=\"file://{}/local-image-01.png\"", tempdir.display())));
assert!(content.html.contains(&format!("src=\"file://{}/.local-image-02.png\"", tempdir.display())));
assert!(content.html.contains(&format!("src=\"file://{}/local-image-03.png\"", tempdir.display())));
assert!(content.html.contains(&format!("src=\"file://{}/local-image-04.png\"", tempdir.display())));
assert!(content.html.contains(&format!("src=\"file://{}/../local-image-05.png\"", tempdir.display())));
assert!(content.html.contains("src=\"http://remote-image-01.png\""));
assert!(content.html.contains("src=\"https://remote-image-02.png\""));
}

0 comments on commit f6e55e2

Please sign in to comment.