Skip to content

Commit 9ab7f76

Browse files
committedJun 27, 2024··
prefix img src
1 parent c60763a commit 9ab7f76

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed
 

‎Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/rari-doc/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ icu_collator = "1"
3131
icu_locid = "1"
3232
strum = { version = "0.26", features = ["derive"] }
3333
ego-tree = "0.6"
34+
url = "2"
3435

3536
rari-types = { path = "../rari-types" }
3637
rari-md = { path = "../rari-md" }

‎crates/rari-doc/src/error.rs

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ pub enum DocError {
8585
NoBlogRoot,
8686
#[error(transparent)]
8787
L10nError(#[from] L10nError),
88+
#[error(transparent)]
89+
UrlParseError(#[from] url::ParseError),
8890
}
8991

9092
impl<T> From<PoisonError<T>> for DocError {

‎crates/rari-doc/src/html/rewriter.rs

+13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use lol_html::{element, text, HtmlRewriter, Settings};
66
use rari_md::bq::NoteCard;
77
use rari_types::fm_types::PageType;
88
use rari_types::locale::Locale;
9+
use url::Url;
910

1011
use crate::docs::curriculum::relative_file_to_curriculum_page;
1112
use crate::docs::page::{Page, PageLike};
@@ -21,6 +22,9 @@ pub fn post_process_html<T: PageLike>(
2122
let mut output = vec![];
2223
let mut ids = HashSet::new();
2324
let open_dt_a = std::rc::Rc::new(std::cell::RefCell::new(false));
25+
let options = Url::options();
26+
let base = Url::parse(&format!("http://rari.placeholder{}/", page.url()))?;
27+
let base_url = options.base_url(Some(&base));
2428

2529
let mut element_content_handlers = vec![
2630
element!("*[id]", |el| {
@@ -47,6 +51,15 @@ pub fn post_process_html<T: PageLike>(
4751
}
4852
Ok(())
4953
}),
54+
element!("img[src]", |el| {
55+
if let Some(src) = el.get_attribute("src") {
56+
let url = base_url.parse(&src)?;
57+
if url.host() == base.host() {
58+
el.set_attribute("src", url.path())?;
59+
}
60+
}
61+
Ok(())
62+
}),
5063
element!("img:not([loading])", |el| {
5164
el.set_attribute("loading", "lazy")?;
5265
Ok(())

0 commit comments

Comments
 (0)
Please sign in to comment.