Skip to main content

transform_link

Function transform_link 

Source
pub fn transform_link(url: &str, config: &LinkTransformConfig) -> String
Expand description

Transform a relative link URL for the trailing-slash URL convention.

§Rules

  1. Anchor-only links (#...) → unchanged
  2. External URLs — anything with a scheme (https:, mailto:, magnet:, blob:, …) or protocol-relative (//host) → unchanged, as decided by crate::url_path::is_external_url
  3. Root-relative URLs (starts with /) → unchanged
  4. Relative markdown links → prepend ../ (if not index file), replace extension with /
  5. Relative extension-less targets → prepend ../ (if not index file), and append / when LinkTransformConfig::markdown_page_probe says the target is a markdown page
  6. 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");