Skip to content

Commit

Permalink
prefix img src
Browse files Browse the repository at this point in the history
  • Loading branch information
fiji-flo committed Jun 27, 2024
1 parent c60763a commit 9ab7f76
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/rari-doc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 2 additions & 0 deletions crates/rari-doc/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ pub enum DocError {
NoBlogRoot,
#[error(transparent)]
L10nError(#[from] L10nError),
#[error(transparent)]
UrlParseError(#[from] url::ParseError),
}

impl<T> From<PoisonError<T>> for DocError {
Expand Down
13 changes: 13 additions & 0 deletions crates/rari-doc/src/html/rewriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -21,6 +22,9 @@ pub fn post_process_html<T: PageLike>(
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| {
Expand All @@ -47,6 +51,15 @@ pub fn post_process_html<T: PageLike>(
}
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(())
Expand Down

0 comments on commit 9ab7f76

Please sign in to comment.