diff --git a/Cargo.lock b/Cargo.lock index 955fe2b..0265984 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2302,6 +2302,7 @@ dependencies = [ "strum", "thiserror", "tracing", + "url", "validator", "yaml-rust", ] diff --git a/crates/rari-doc/Cargo.toml b/crates/rari-doc/Cargo.toml index 46b092c..da67f2e 100644 --- a/crates/rari-doc/Cargo.toml +++ b/crates/rari-doc/Cargo.toml @@ -31,6 +31,7 @@ icu_collator = "1" icu_locid = "1" strum = { version = "0.26", features = ["derive"] } ego-tree = "0.6" +url = "2" rari-types = { path = "../rari-types" } rari-md = { path = "../rari-md" } diff --git a/crates/rari-doc/src/error.rs b/crates/rari-doc/src/error.rs index 35fd02f..6bd6816 100644 --- a/crates/rari-doc/src/error.rs +++ b/crates/rari-doc/src/error.rs @@ -85,6 +85,8 @@ pub enum DocError { NoBlogRoot, #[error(transparent)] L10nError(#[from] L10nError), + #[error(transparent)] + UrlParseError(#[from] url::ParseError), } impl From> for DocError { diff --git a/crates/rari-doc/src/html/rewriter.rs b/crates/rari-doc/src/html/rewriter.rs index 3c53fb5..7a94276 100644 --- a/crates/rari-doc/src/html/rewriter.rs +++ b/crates/rari-doc/src/html/rewriter.rs @@ -6,6 +6,7 @@ use lol_html::{element, text, HtmlRewriter, Settings}; use rari_md::bq::NoteCard; use rari_types::fm_types::PageType; use rari_types::locale::Locale; +use url::Url; use crate::docs::curriculum::relative_file_to_curriculum_page; use crate::docs::page::{Page, PageLike}; @@ -21,6 +22,9 @@ pub fn post_process_html( let mut output = vec![]; let mut ids = HashSet::new(); let open_dt_a = std::rc::Rc::new(std::cell::RefCell::new(false)); + let options = Url::options(); + let base = Url::parse(&format!("http://rari.placeholder{}/", page.url()))?; + let base_url = options.base_url(Some(&base)); let mut element_content_handlers = vec![ element!("*[id]", |el| { @@ -47,6 +51,15 @@ pub fn post_process_html( } Ok(()) }), + element!("img[src]", |el| { + if let Some(src) = el.get_attribute("src") { + let url = base_url.parse(&src)?; + if url.host() == base.host() { + el.set_attribute("src", url.path())?; + } + } + Ok(()) + }), element!("img:not([loading])", |el| { el.set_attribute("loading", "lazy")?; Ok(())