fix markdown image render hook, handle third party image url

This commit is contained in:
Reorx 2022-09-03 21:36:36 +08:00
parent 295396b440
commit 0935880bf3
3 changed files with 34 additions and 18 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 95 KiB

View File

@ -14,6 +14,16 @@ Hugo ships with several [Built-in Shortcodes](https://gohugo.io/content-manageme
<!--more--> <!--more-->
--- ---
## 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 Privacy Enhanced Shortcode
{{< youtube ZJthWmvUzzc >}} {{< youtube ZJthWmvUzzc >}}

View File

@ -1,7 +1,12 @@
{{- $url := urls.Parse .Destination -}}
{{- if $url.Scheme -}}
{{/* third party image */}}
<img loading="lazy" src="{{ .Destination | safeURL }}" alt="{{ $.Text }}" {{ with $.Title}} title="{{ . }}" {{ end }} />
{{- else -}}
{{/* internal image */}}
{{- $img := .Page.Resources.GetMatch .Destination -}} {{- $img := .Page.Resources.GetMatch .Destination -}}
{{- if and (not $img) .Page.File -}} {{- if not $img -}}
{{ $path := path.Join .Page.File.Dir .Destination }} {{- $img = resources.Get .Destination -}}
{{- $img = resources.Get $path -}}
{{- end -}} {{- end -}}
{{- if $img -}} {{- if $img -}}
@ -18,3 +23,4 @@
{{- else -}} {{- else -}}
{{- warnf "No image found for %s from %s" .Destination .Page.File }} {{- warnf "No image found for %s from %s" .Destination .Page.File }}
{{- end -}} {{- end -}}
{{- end -}}