From 0935880bf3ca90ab6a44e012fa867d06d6a7a3bf Mon Sep 17 00:00:00 2001 From: Reorx Date: Sat, 3 Sep 2022 21:36:36 +0800 Subject: fix markdown image render hook, handle third party image url --- exampleSite/assets/images/regular.jpg | Bin 0 -> 97512 bytes exampleSite/content/posts/rich-content.md | 10 ++++++++ layouts/_default/_markup/render-image.html | 38 +++++++++++++++++------------ 3 files changed, 32 insertions(+), 16 deletions(-) create mode 100644 exampleSite/assets/images/regular.jpg diff --git a/exampleSite/assets/images/regular.jpg b/exampleSite/assets/images/regular.jpg new file mode 100644 index 0000000..70a3401 Binary files /dev/null and b/exampleSite/assets/images/regular.jpg differ diff --git a/exampleSite/content/posts/rich-content.md b/exampleSite/content/posts/rich-content.md index b7c896a..81fac33 100644 --- a/exampleSite/content/posts/rich-content.md +++ b/exampleSite/content/posts/rich-content.md @@ -14,6 +14,16 @@ Hugo ships with several [Built-in Shortcodes](https://gohugo.io/content-manageme --- +## Image + +third party image: + +![](https://images.unsplash.com/photo-1662141978148-2eeb2afb3837?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=772&q=80) + +internal image: + +![](images/regular.jpg) + ## YouTube Privacy Enhanced Shortcode {{< youtube ZJthWmvUzzc >}} diff --git a/layouts/_default/_markup/render-image.html b/layouts/_default/_markup/render-image.html index 0fc039c..c4d7497 100644 --- a/layouts/_default/_markup/render-image.html +++ b/layouts/_default/_markup/render-image.html @@ -1,20 +1,26 @@ -{{- $img := .Page.Resources.GetMatch .Destination -}} -{{- if and (not $img) .Page.File -}} - {{ $path := path.Join .Page.File.Dir .Destination }} - {{- $img = resources.Get $path -}} -{{- end -}} +{{- $url := urls.Parse .Destination -}} +{{- if $url.Scheme -}} + {{/* third party image */}} + {{ $.Text }} +{{- else -}} + {{/* internal image */}} + {{- $img := .Page.Resources.GetMatch .Destination -}} + {{- if not $img -}} + {{- $img = resources.Get .Destination -}} + {{- end -}} -{{- if $img -}} - {{- if eq $img.MediaType.MainType "video" -}} -
- -
+ {{- if $img -}} + {{- if eq $img.MediaType.MainType "video" -}} +
+ +
+ {{- else -}} + {{ $.Text }} + {{- end -}} {{- else -}} - {{ $.Text }} + {{- warnf "No image found for %s from %s" .Destination .Page.File }} {{- end -}} -{{- else -}} - {{- warnf "No image found for %s from %s" .Destination .Page.File }} {{- end -}} -- cgit v1.2.3