img width="" height="" generation for djot renderer

This commit is contained in:
りき萌 2025-08-31 12:06:28 +02:00
parent b6ff4bca21
commit 96fc77dc3e

View file

@ -19,7 +19,9 @@ use crate::dirs::Dirs;
use crate::state::FileId;
use crate::state::Treehouse;
use crate::vfs;
use crate::vfs::Dir;
use crate::vfs::ImageSize;
use crate::vfs::VPathBuf;
use super::highlight::highlight;
@ -476,9 +478,28 @@ impl<'a> Writer<'a> {
Container::Image(src, link_type) => {
if self.img_alt_text == 1 {
if !src.is_empty() {
out.push_str(r#"" src=""#);
out.push_str(r#"" "#);
if let SpanLinkType::Unresolved = link_type {
// TODO: Image size.
let resolved_image =
resolve_image_link(self.renderer.config, src);
let size = if let Some(ResolvedImageLink::VPath(vpath)) =
&resolved_image
{
vfs::query::<ImageSize>(&self.renderer.dirs.pic, vpath)
} else {
None
};
if let Some(size) = size {
write!(
out,
r#" width="{}" height="{}""#,
size.width, size.height
)?;
}
out.push_str(r#" src=""#);
if let Some(resolved) = resolve_link(
self.renderer.config,
self.renderer.treehouse,
@ -489,11 +510,14 @@ impl<'a> Writer<'a> {
} else {
write_attr(src, out);
}
out.push('"');
} else {
out.push_str(r#" src=""#);
write_attr(src, out);
out.push('"');
}
}
out.push_str(r#"">"#);
out.push('>');
}
self.img_alt_text -= 1;
}
@ -672,3 +696,30 @@ pub fn resolve_link(
_ => None,
})
}
#[derive(Debug, Clone)]
pub enum ResolvedImageLink {
VPath(VPathBuf),
Url(String),
}
impl ResolvedImageLink {
pub fn into_url(self, config: &Config, pics_dir: &dyn Dir) -> Option<String> {
match self {
ResolvedImageLink::VPath(vpath) => vfs::url(&config.site, pics_dir, &vpath),
ResolvedImageLink::Url(url) => Some(url),
}
}
}
pub fn resolve_image_link(config: &Config, link: &str) -> Option<ResolvedImageLink> {
link.split_once(':').and_then(|(kind, linked)| match kind {
"def" => config.defs.get(linked).cloned().map(ResolvedImageLink::Url),
"pic" => config
.pics
.get(linked)
.cloned()
.map(ResolvedImageLink::VPath),
_ => None,
})
}