Rust library to generate static websites
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(assets): Respect assets_dir setting when making URLs

+48 -7
+38 -4
crates/framework/src/assets.rs
··· 13 13 pub(crate) assets: FxHashSet<Box<dyn Asset>>, 14 14 pub(crate) scripts: FxHashSet<Script>, 15 15 pub(crate) styles: FxHashSet<Style>, 16 + 16 17 pub(crate) included_styles: Vec<Style>, 17 18 pub(crate) included_scripts: Vec<Script>, 19 + 20 + pub(crate) assets_dir: PathBuf, 18 21 } 19 22 20 23 impl PageAssets { ··· 24 27 { 25 28 let image = Box::new(Image { 26 29 path: image_path.into(), 30 + assets_dir: self.assets_dir.clone(), 27 31 }); 28 32 29 33 self.assets.insert(image.clone()); ··· 37 41 { 38 42 let script = Script { 39 43 path: script_path.into(), 44 + assets_dir: self.assets_dir.clone(), 40 45 }; 41 46 42 47 self.scripts.insert(script.clone()); ··· 50 55 { 51 56 let script = Script { 52 57 path: script_path.into(), 58 + assets_dir: self.assets_dir.clone(), 53 59 }; 54 60 55 61 self.scripts.insert(script.clone()); ··· 63 69 let style = Style { 64 70 path: style_path.into(), 65 71 tailwind, 72 + assets_dir: self.assets_dir.clone(), 66 73 }; 67 74 68 75 self.styles.insert(style.clone()); ··· 77 84 let style = Style { 78 85 path: style_path.into(), 79 86 tailwind, 87 + assets_dir: self.assets_dir.clone(), 80 88 }; 81 89 82 90 self.styles.insert(style.clone()); ··· 84 92 } 85 93 } 86 94 87 - pub trait Asset: DynEq { 95 + #[allow(private_bounds)] // Users never interact with the internal trait, so it's fine 96 + pub trait Asset: DynEq + InternalAsset { 88 97 fn url(&self) -> Option<String>; 89 98 fn path(&self) -> &PathBuf; 90 99 ··· 94 103 fn hash(&self) -> [u8; 8]; 95 104 } 96 105 106 + trait InternalAsset { 107 + fn assets_dir(&self) -> PathBuf; 108 + } 109 + 97 110 impl Hash for dyn Asset { 98 111 fn hash<H: std::hash::Hasher>(&self, state: &mut H) { 99 112 self.hash().hash(state); ··· 106 119 #[non_exhaustive] 107 120 pub struct Image { 108 121 pub path: PathBuf, 122 + pub(crate) assets_dir: PathBuf, 123 + } 124 + 125 + impl InternalAsset for Image { 126 + fn assets_dir(&self) -> PathBuf { 127 + self.assets_dir.clone() 128 + } 109 129 } 110 130 111 131 impl Asset for Image { 112 132 fn url(&self) -> Option<String> { 113 133 let file_name = self.path.file_name().unwrap().to_str().unwrap(); 114 134 115 - format!("/_assets/{}", file_name).into() 135 + format!("{}/{}", self.assets_dir().to_string_lossy(), file_name).into() 116 136 } 117 137 118 138 fn path(&self) -> &PathBuf { ··· 146 166 #[non_exhaustive] 147 167 pub struct Script { 148 168 pub path: PathBuf, 169 + pub(crate) assets_dir: PathBuf, 170 + } 171 + 172 + impl InternalAsset for Script { 173 + fn assets_dir(&self) -> PathBuf { 174 + self.assets_dir.clone() 175 + } 149 176 } 150 177 151 178 impl Asset for Script { 152 179 fn url(&self) -> Option<String> { 153 180 let file_name = self.path.file_name().unwrap().to_str().unwrap(); 154 181 155 - format!("/_assets/{}", file_name).into() 182 + format!("{}/{}", self.assets_dir().to_string_lossy(), file_name).into() 156 183 } 157 184 158 185 fn path(&self) -> &PathBuf { ··· 178 205 pub struct Style { 179 206 pub path: PathBuf, 180 207 pub(crate) tailwind: bool, 208 + pub(crate) assets_dir: PathBuf, 209 + } 210 + 211 + impl InternalAsset for Style { 212 + fn assets_dir(&self) -> PathBuf { 213 + self.assets_dir.clone() 214 + } 181 215 } 182 216 183 217 impl Asset for Style { 184 218 fn url(&self) -> Option<String> { 185 219 let file_name = self.path.file_name().unwrap().to_str().unwrap(); 186 220 187 - format!("/_assets/{}", file_name).into() 221 + format!("{}/{}", self.assets_dir().to_string_lossy(), file_name).into() 188 222 } 189 223 190 224 fn path(&self) -> &PathBuf {
+10 -3
crates/framework/src/lib.rs
··· 93 93 fn default() -> Self { 94 94 Self { 95 95 output_dir: "dist".to_string(), 96 - assets_dir: "_assets".to_string(), 96 + assets_dir: "_maudit".to_string(), 97 97 static_dir: "static".to_string(), 98 98 } 99 99 } ··· 191 191 match routes.is_empty() { 192 192 true => { 193 193 let route_start = SystemTime::now(); 194 - let mut page_assets = assets::PageAssets::default(); 194 + let mut page_assets = assets::PageAssets { 195 + assets_dir: options.assets_dir.clone().into(), 196 + ..Default::default() 197 + }; 198 + 195 199 let mut ctx = RouteContext { 196 200 params: page::RouteParams(FxHashMap::default()), 197 201 content: &content_sources, ··· 227 231 info!(target: "build", "{}", route.route_raw().to_string().bold()); 228 232 229 233 for params in routes { 230 - let mut pages_assets = assets::PageAssets::default(); 234 + let mut pages_assets = assets::PageAssets { 235 + assets_dir: options.assets_dir.clone().into(), 236 + ..Default::default() 237 + }; 231 238 let route_start = SystemTime::now(); 232 239 let mut ctx = RouteContext { 233 240 params,