Skip to main content

relative_link

Function relative_link 

Source
pub fn relative_link(from: &str, to: &str, fragment: Option<&str>) -> String
Expand description

The relative URL from the file from to the file to, both vault-relative and /-separated, with an optional #fragment.

The path is relative to from’s folder, path.posix.relative style, and each segment is encoded with encode_uri_component. A file linking itself gets its own name. The fragment is encoded with encode_uri_fragment, so a # inside it cannot start a second one.

§Examples

use markdown_link::relative_link;

assert_eq!(relative_link("a/b.md", "a/c d.md", None), "c%20d.md");
assert_eq!(relative_link("a/b/x.md", "c/y.md", Some("Part 1")), "../../c/y.md#Part%201");
assert_eq!(relative_link("a/x.md", "a/x.md", Some("H#2")), "x.md#H%232");