Skip to main content

replace_canonical_link

Function replace_canonical_link 

Source
pub fn replace_canonical_link(html: &str, payload: &str) -> String
Expand description

Replaces every existing <link rel~="canonical"> with a single payload injected just before </head>, in one parser pass.

Equivalent to inject_before_head_close(&remove_canonical_links(html), payload) but executed in a single lol_html walk so that the surrounding whitespace residue from the removal is consumed at the same time as the new link is inserted — keeping the canonical plugin’s transform_html byte-stable across repeated invocations (idempotency requirement).

§Examples

use ssg::util::head_dom::replace_canonical_link;
let html = r#"<html><head><link rel="canonical" href="/old"></head></html>"#;
let out = replace_canonical_link(html, r#"<link rel="canonical" href="/new">"#);
assert!(out.contains("href=\"/new\""));
assert!(!out.contains("href=\"/old\""));