Repository navigation
feat(images): encode Markdown images to WebP at build time #1259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,44 @@ | ||
| <!-- Images that use the standard image format for markdown will be displayed in a | ||
| <!-- Images that use the standard image format for markdown will be displayed in a | ||
| magnific modal popup. Use format  --> | ||
| <!-- Place this file in layouts > _default > _markup > render-image.html --> | ||
| <!-- Also place the image-modal.css and partial/image-modal.html appropriately. --> | ||
| <!-- The image is passed to openModal directly, so no generated id is needed. The previous | ||
| `first 6 (shuffle (seq 1 500))` emitted a whole slice, e.g. id="[487 38 279 330 254 290]", | ||
| which is not a valid HTML id. openModal accepts an <img> element as well as an id. --> | ||
| <!-- PNG and JPEG sources are encoded to WebP at build time and offered through | ||
| <picture>. The original stays as the <img> fallback, so nothing in content has | ||
| to change and browsers without WebP support still get a working image. | ||
| Content is mounted at assets/contentimg (see hugo.toml) because Hugo can only | ||
| process files that are resources. --> | ||
| <!-- Onclick opens the modal, and displays/overlays the clicked image. --> | ||
| <!-- attribution appreciated. github: zjeaton web: https://froglegs.co --> | ||
|
|
||
| {{- $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 -}} | ||
|
|
||
| <div class="md__image"> | ||
| <img src="{{ .Destination | safeURL }}" onclick="openModal(this)" alt="{{ .Text }}" | ||
| class="md-image-responsive{{ with .Title }} {{ . }}{{ end }}" /> | ||
| </div> | ||
| {{- with $img }} | ||
| {{- $webp := .Process "webp q85" }} | ||
| <picture> | ||
| <source srcset="{{ $webp.RelPermalink }}" type="image/webp" width="{{ .Width }}" height="{{ .Height }}"> | ||
| <img src="{{ $dest | safeURL }}" onclick="openModal(this)" alt="{{ $alt }}" | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| width="{{ .Width }}" height="{{ .Height }}" | ||
| class="{{ $class }}" /> | ||
| </picture> | ||
| {{- else }} | ||
| <img src="{{ $dest | safeURL }}" onclick="openModal(this)" alt="{{ $alt }}" | ||
| class="{{ $class }}" /> | ||
| {{- end }} | ||
| </div> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.