Git Product home page Git Product logo

Comments (1)

AndrewRadev avatar AndrewRadev commented on August 19, 2024 2

Currently, it's possible to render a file-relative image using ![Company logo](./distribution/media/logo.png) -- that gets parsed right here:

quickmd/src/markdown.rs

Lines 55 to 57 in 7c1f2ee

Event::Start(Tag::Image(_, url, _)) if url.starts_with("./") => {
*url = format!("file://{}/{}", root_dir.display(), &url[2..]).into();
},

I'd be happy to implement rendering your example as well. In fact, I think I considered it, but I wasn't sure if it was a file path or a domain name. Now that I'm testing, though, it seems github only parses URLs if they start with http:// or https://, so maybe it'd be quite simple to get this done by changing the .starts_with? with a check for not starting with http:// or with https://. Or maybe even .\w+:// for stuff like ftp:// or file://.

Ideally it would look for the file starting at the root of the current Git repo,

According to this doc, I think repo-root-relative files start with /? https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#images

This would also be fine to implement, and pretty useful -- the code could check for a .git file or directory upwards, and if it exists, check if that file exists. But maybe this could be done separately.

I can try opening a PR for this if it's not too hard to implement.

I think it should be relatively easy to use the regex crate to check that the "url" in that code doesn't start with a protocol and ://. There's also a test that would need to be tweaked and maybe extended:

#[test]
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();
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\""));
}

If you don't have the time/energy or Rust experience, I can take care of it as well -- let me know 👍.

from quickmd.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.