diff --git a/hugo.toml b/hugo.toml index a697e6188bb3..a76a15dbe02a 100644 --- a/hugo.toml +++ b/hugo.toml @@ -348,6 +348,14 @@ enable = false [[module.imports]] path = "github.com/google/docsy/dependencies" disable = false + [[module.mounts]] + source = "assets" + target = "assets" + # Content images are mounted into assets so the image render hook can + # process them (Hugo can only process files that are resources). + [[module.mounts]] + source = "content/en" + target = "assets/contentimg" [[module.mounts]] source = "static" target = "static" diff --git a/layouts/_default/_markup/render-image.html b/layouts/_default/_markup/render-image.html index f5ab8e99ada3..34af92caad01 100644 --- a/layouts/_default/_markup/render-image.html +++ b/layouts/_default/_markup/render-image.html @@ -1,14 +1,44 @@ - - + +{{- $dest := .Destination -}} +{{- $alt := .Text -}} +{{- $class := "md-image-responsive" -}} +{{- with .Title }}{{ $class = printf "md-image-responsive %s" . }}{{ end -}} +{{- $img := "" -}} +{{- $ext := lower (path.Ext $dest) -}} + +{{- if and (in (slice ".png" ".jpg" ".jpeg") $ext) (not (urls.Parse $dest).Scheme) (not (hasPrefix $dest "//")) -}} + {{- $rel := $dest -}} + {{- if not (hasPrefix $dest "/") -}} + {{- with .Page.File -}} + {{- $rel = path.Join (replace .Dir "\\" "/") $dest -}} + {{- end -}} + {{- end -}} + {{- $img = resources.Get (path.Join "contentimg" $rel) -}} + {{- if not $img -}}{{- $img = resources.Get $rel -}}{{- end -}} +{{- end -}} +
- {{ .Text }} -
\ No newline at end of file +{{- with $img }} + {{- $webp := .Process "webp q85" }} + + + {{ $alt }} + +{{- else }} + {{ $alt }} +{{- end }} + diff --git a/layouts/partials/image-modal.html b/layouts/partials/image-modal.html index ef3552b0a988..72650e62a27f 100644 --- a/layouts/partials/image-modal.html +++ b/layouts/partials/image-modal.html @@ -14,8 +14,10 @@ // If it's a string (image ID), get the source using the ID src = document.getElementById(imageIdOrElement).src; } else if (imageIdOrElement instanceof HTMLImageElement) { - // If , get the source directly - src = imageIdOrElement.src; + // If , get the source directly. currentSrc is what the browser actually + // chose, so inside a the modal reuses the already-fetched WebP + // rather than downloading the larger original. + src = imageIdOrElement.currentSrc || imageIdOrElement.src; } if (src && src.includes("#")) {