pub fn transform_link(url: &str, config: &LinkTransformConfig) -> StringExpand description
Transform a relative link URL for the trailing-slash URL convention.
§Rules
- Anchor-only links (
#...) → unchanged - External URLs — anything with a scheme (
https:,mailto:,magnet:,blob:, …) or protocol-relative (//host) → unchanged, as decided bycrate::url_path::is_external_url - Root-relative URLs (starts with
/) → unchanged - Relative markdown links → prepend
../(if not index file), replace extension with/ - Relative extension-less targets → prepend
../(if not index file), and append/whenLinkTransformConfig::markdown_page_probesays the target is a markdown page - Relative static files → prepend
../(if not index file)
§Examples
use mbr_core::link_transform::{transform_link, LinkTransformConfig};
let config = LinkTransformConfig {
markdown_extensions: vec!["md".to_string()],
index_file: "index.md".to_string(),
is_index_file: false,
url_depth: None,
current_page_url: String::new(),
markdown_page_probe: None,
};
// Regular markdown file: add ../ and trailing slash
assert_eq!(transform_link("other.md", &config), "../other/");
// Index file config: no ../ prefix
let index_config = LinkTransformConfig { is_index_file: true, ..config.clone() };
assert_eq!(transform_link("other.md", &index_config), "other/");
// Absolute URLs unchanged
assert_eq!(transform_link("https://example.com", &config), "https://example.com");