🚸 Prevent image processing on external URLs

pull/110/head
James Panther 2022-02-06 11:26:37 +11:00
parent dd3606d33d
commit 60a62e2b4b
No known key found for this signature in database
GPG Key ID: D36F789E45745D17
4 changed files with 51 additions and 30 deletions

View File

@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
## [Unreleased] ## [Unreleased]
### Fixed
- Markdown images and `figure` shortcode fail to load resource when providing an external URL source
- HTML `figcaption` tags are output for Markdown images even when a caption is not provided
## [2.0.2] - 2022-02-05 ## [2.0.2] - 2022-02-05
### Changed ### Changed

View File

@ -99,14 +99,14 @@ You can see some additional Chart.js examples on the [charts samples]({{< ref "c
Congo includes a `figure` shortcode for adding images to content. The shortcode replaces the base Hugo functionality in order to provide additional performance benefits. Congo includes a `figure` shortcode for adding images to content. The shortcode replaces the base Hugo functionality in order to provide additional performance benefits.
Images included using `figure` will be optimised using Hugo Pipes and scaled in order to provide images appropriate to different device resolutions. When a provided image is a page resource, it will be optimised using Hugo Pipes and scaled in order to provide images appropriate to different device resolutions. If a URL to an external image is provided, it will be included as-is without any image processing by Hugo.
The `figure` shortcode accepts six parameters: The `figure` shortcode accepts six parameters:
<!-- prettier-ignore-start --> <!-- prettier-ignore-start -->
|Parameter|Description| |Parameter|Description|
|---|---| |---|---|
|`src`|**Required.** The filename of the image. This image must be a [page resource](https://gohugo.io/content-management/page-resources/) bundled with the page.| |`src`|**Required.** The filename or URL of the image. When providing a filename, this image must be a [page resource](https://gohugo.io/content-management/page-resources/) bundled with the page.|
|`alt`|[Alternative text description](https://moz.com/learn/seo/alt-text) for the image.| |`alt`|[Alternative text description](https://moz.com/learn/seo/alt-text) for the image.|
|`caption`|Markdown for the image caption, which will be displayed below the image.| |`caption`|Markdown for the image caption, which will be displayed below the image.|
|`class`|Additional CSS classes to apply to the image.| |`class`|Additional CSS classes to apply to the image.|

View File

@ -1,19 +1,27 @@
{{ $url := urls.Parse .Destination }}
{{ $altText := .Text }} {{ $altText := .Text }}
{{ $caption := .Title }} {{ $caption := .Title }}
{{ with $.Page.Resources.GetMatch (.Destination) }} {{ if findRE "^https?" $url.Scheme }}
<figure> <figure>
<img <img class="my-0 rounded-md" src="{{ $url.String }}" alt="{{ $altText }}" />
class="my-0 rounded-md" {{ with $caption }}<figcaption>{{ . | markdownify }}</figcaption>{{ end }}
srcset="
{{ (.Resize "330x").RelPermalink }} 330w,
{{ (.Resize "660x").RelPermalink }} 660w,
{{ (.Resize "1024x").RelPermalink }} 1024w,
{{ (.Resize "1320x").RelPermalink }} 2x"
src="{{ (.Resize "660x").RelPermalink }}"
alt="{{ $altText }}"
/>
<figcaption>{{ $caption | markdownify }}</figcaption>
</figure> </figure>
{{ else }} {{ else }}
{{ errorf `[CONGO] Markdown image error in "%s": Resource "%s" not found. Check the path is correct or remove the image from the content.` .Page.Path .Destination }} {{ with $.Page.Resources.GetMatch ($url.String) }}
<figure>
<img
class="my-0 rounded-md"
srcset="
{{ (.Resize "330x").RelPermalink }} 330w,
{{ (.Resize "660x").RelPermalink }} 660w,
{{ (.Resize "1024x").RelPermalink }} 1024w,
{{ (.Resize "1320x").RelPermalink }} 2x"
src="{{ (.Resize "660x").RelPermalink }}"
alt="{{ $altText }}"
/>
{{ with $caption }}<figcaption>{{ . | markdownify }}</figcaption>{{ end }}
</figure>
{{ else }}
{{ errorf `[CONGO] Markdown image error in "%s": Resource "%s" not found. Check the path is correct or remove the image from the content.` .Page.Path $url.String }}
{{ end }}
{{ end }} {{ end }}

View File

@ -1,27 +1,35 @@
{{ if .Get "default" }} {{ if .Get "default" }}
{{ template "_internal/shortcodes/figure.html" . }} {{ template "_internal/shortcodes/figure.html" . }}
{{ else }} {{ else }}
{{ $url := urls.Parse (.Get "src") }}
{{ $altText := .Get "alt" }} {{ $altText := .Get "alt" }}
{{ $caption := .Get "caption" }} {{ $caption := .Get "caption" }}
{{ $href := .Get "href" }} {{ $href := .Get "href" }}
{{ $class := .Get "class" }} {{ $class := .Get "class" }}
{{ with $.Page.Resources.GetMatch (.Get "src") }} {{ if findRE "^https?" $url.Scheme }}
<figure {{ with $class }}class="{{ . }}"{{ end }}> <figure>
{{ with $href }}<a href="{{ . }}">{{ end }} <img class="my-0 rounded-md" src="{{ $url.String }}" alt="{{ $altText }}" />
<img
class="my-0 rounded-md"
srcset="
{{ (.Resize "330x").RelPermalink }} 330w,
{{ (.Resize "660x").RelPermalink }} 660w,
{{ (.Resize "1024x").RelPermalink }} 1024w,
{{ (.Resize "1320x").RelPermalink }} 2x"
src="{{ (.Resize "660x").RelPermalink }}"
alt="{{ $altText }}"
/>
{{ if $href }}</a>{{ end }}
{{ with $caption }}<figcaption>{{ . | markdownify }}</figcaption>{{ end }} {{ with $caption }}<figcaption>{{ . | markdownify }}</figcaption>{{ end }}
</figure> </figure>
{{ else }} {{ else }}
{{ errorf `[CONGO] Shortcode "figure" error in "%s": Resource "%s" not found. Check the path is correct or remove the shortcode.` .Page.Path (.Get "src") }} {{ with $.Page.Resources.GetMatch ($url.String) }}
<figure {{ with $class }}class="{{ . }}"{{ end }}>
{{ with $href }}<a href="{{ . }}">{{ end }}
<img
class="my-0 rounded-md"
srcset="
{{ (.Resize "330x").RelPermalink }} 330w,
{{ (.Resize "660x").RelPermalink }} 660w,
{{ (.Resize "1024x").RelPermalink }} 1024w,
{{ (.Resize "1320x").RelPermalink }} 2x"
src="{{ (.Resize "660x").RelPermalink }}"
alt="{{ $altText }}"
/>
{{ if $href }}</a>{{ end }}
{{ with $caption }}<figcaption>{{ . | markdownify }}</figcaption>{{ end }}
</figure>
{{ else }}
{{ errorf `[CONGO] Shortcode "figure" error in "%s": Resource "%s" not found. Check the path is correct or remove the shortcode.` .Page.Path ($url.String) }}
{{ end }}
{{ end }} {{ end }}
{{ end }} {{ end }}