From 60a62e2b4b30e2a1861b844072d6909f31754347 Mon Sep 17 00:00:00 2001
From: James Panther <4462786+jpanther@users.noreply.github.com>
Date: Sun, 6 Feb 2022 11:26:37 +1100
Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=B8=20Prevent=20image=20processing=20o?=
=?UTF-8?q?n=20external=20URLs?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
CHANGELOG.md | 5 +++
exampleSite/content/docs/shortcodes/index.md | 4 +--
layouts/_default/_markup/render-image.html | 34 +++++++++++-------
layouts/shortcodes/figure.html | 38 ++++++++++++--------
4 files changed, 51 insertions(+), 30 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d5450794..5f57525a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
## [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
### Changed
diff --git a/exampleSite/content/docs/shortcodes/index.md b/exampleSite/content/docs/shortcodes/index.md
index 2df0149c..cf117647 100644
--- a/exampleSite/content/docs/shortcodes/index.md
+++ b/exampleSite/content/docs/shortcodes/index.md
@@ -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.
-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:
|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.|
|`caption`|Markdown for the image caption, which will be displayed below the image.|
|`class`|Additional CSS classes to apply to the image.|
diff --git a/layouts/_default/_markup/render-image.html b/layouts/_default/_markup/render-image.html
index 6c01a4b4..ad27611c 100644
--- a/layouts/_default/_markup/render-image.html
+++ b/layouts/_default/_markup/render-image.html
@@ -1,19 +1,27 @@
+{{ $url := urls.Parse .Destination }}
{{ $altText := .Text }}
{{ $caption := .Title }}
-{{ with $.Page.Resources.GetMatch (.Destination) }}
+{{ if findRE "^https?" $url.Scheme }}
{{ 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) }}
+
+ {{ 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 }}
diff --git a/layouts/shortcodes/figure.html b/layouts/shortcodes/figure.html
index 4bca0c15..cc9c7f2b 100644
--- a/layouts/shortcodes/figure.html
+++ b/layouts/shortcodes/figure.html
@@ -1,27 +1,35 @@
{{ if .Get "default" }}
{{ template "_internal/shortcodes/figure.html" . }}
{{ else }}
+ {{ $url := urls.Parse (.Get "src") }}
{{ $altText := .Get "alt" }}
{{ $caption := .Get "caption" }}
{{ $href := .Get "href" }}
{{ $class := .Get "class" }}
- {{ with $.Page.Resources.GetMatch (.Get "src") }}
-