Skip to content

Commit

Permalink
feat: add basic svg support to the img shortcode (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoxys authored Dec 11, 2022
1 parent 8a5e030 commit 5d4370b
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 19 deletions.
16 changes: 16 additions & 0 deletions exampleSite/content/en/shortcodes/images/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ resources:
title: Forest (7)
params:
credits: "[Asher Ward](https://unsplash.com/@the_asher_ward) on [Unsplash](https://unsplash.com/s/photos/forest)"
- name: forest-8
src: "forest-8.svg"
title: Forest (8)
params:
credits: "SVG Repo on [SVGRepo](https://www.svgrepo.com/svg/286078/forest)"
---

If you need more flexibility for your embedded images, you could use the `img` shortcode. It is using Hugo's
Expand Down Expand Up @@ -144,3 +149,14 @@ copious quo ad. Stet probates in duo.
{{< img name="forest-7" lazy=true >}}

<!-- spellchecker-enable -->

Dolor sit, sumo unique argument um no. Gracie nominal id xiv. Romanesque acclimates
investiture. Ornateness bland it ex enc, est yeti am bongo detract re. Pro ad prompts
feud gait, quid exercise emeritus bis e. In pro quints consequent, denim fastidious
copious quo ad. Stet probates in duo.

<!-- spellchecker-disable -->

{{< img name="forest-8" size=small lazy=true >}}

<!-- spellchecker-enable -->
90 changes: 90 additions & 0 deletions exampleSite/content/en/shortcodes/images/forest-8.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 35 additions & 19 deletions layouts/shortcodes/img.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@
{{- $customAlt := .Get "alt" }}
{{- $customSize := .Get "size" | lower }}
{{- $lazyLoad := default (default true $.Site.Params.GeekdocImageLazyLoading) (.Get "lazy") }}
{{- $data := newScratch }}

{{- with $source }}
{{- $caption := default .Title $customAlt }}
{{- $isSVG := (eq .MediaType.SubType "svg") }}

{{- $origin := .Permalink }}
{{- $profile := (.Fill "180x180 Center").Permalink }}
{{- $tiny := (.Resize "320x").Permalink }}
{{- $small := (.Resize "600x").Permalink }}
{{- $medium := (.Resize "1200x").Permalink }}
{{- $large := (.Resize "1800x").Permalink }}

{{- $size := dict "origin" $origin "profile" $profile "tiny" $tiny "small" $small "medium" $medium "large" $large }}
{{ if $isSVG }}
{{- $data.SetInMap "size" "profile" "180" }}
{{- $data.SetInMap "size" "tiny" "320" }}
{{- $data.SetInMap "size" "small" "600" }}
{{- $data.SetInMap "size" "medium" "1200" }}
{{- $data.SetInMap "size" "large" "1800" }}
{{ else }}
{{- $data.SetInMap "size" "profile" (.Fill "180x180 Center").Permalink }}
{{- $data.SetInMap "size" "tiny" (.Resize "320x").Permalink }}
{{- $data.SetInMap "size" "small" (.Resize "600x").Permalink }}
{{- $data.SetInMap "size" "medium" (.Resize "1200x").Permalink }}
{{- $data.SetInMap "size" "large" (.Resize "1800x").Permalink }}
{{ end }}


<div class="flex justify-center">
Expand All @@ -23,21 +31,29 @@
>
<a class="gdoc-markdown__link--raw" href="{{ .Permalink }}">
<picture>
<source
{{- with $customSize }}
srcset="{{ index $size $customSize }}"
{{- else }}
srcset="{{ $size.small }} 600w, {{ $size.medium }} 1200w" sizes="100vw"
{{- end }}
/>
{{- $size := $data.Get "size" }}
{{- if not $isSVG }}
<source
{{- with $customSize }}
srcset="{{ index $size $customSize }}"
{{- else }}
srcset="{{ $size.small }} 600w, {{ $size.medium }} 1200w" sizes="100vw"
{{- end }}

/>
{{- end }}
<img
{{- if $lazyLoad }}{{ print " loading=\"lazy\"" | safeHTMLAttr }}{{- end }}
{{- if eq $customSize "origin" }}
src="{{ $size.origin }}"
{{- if $isSVG }}
src="{{ $origin }}" width="{{ index $size (default "medium" $customSize) }}"
{{- else }}
src="{{ $size.large }}"
{{- if $lazyLoad }}{{ print " loading=\"lazy\"" | safeHTMLAttr }}{{- end }}
{{- if eq $customSize "origin" }}
src="{{ $origin }}"
{{- else }}
src="{{ $size.large }}"
{{- end }}
alt="{{ $caption }}"
{{- end }}
alt="{{ $caption }}"
/>
</picture>
</a>
Expand Down

0 comments on commit 5d4370b

Please sign in to comment.