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,20 +1,26 @@
{{- $img := .Page.Resources.GetMatch .Destination -}} {{- $url := urls.Parse .Destination -}}
{{- if and (not $img) .Page.File -}} {{- if $url.Scheme -}}
{{ $path := path.Join .Page.File.Dir .Destination }} {{/* third party image */}}
{{- $img = resources.Get $path -}} <img loading="lazy" src="{{ .Destination | safeURL }}" alt="{{ $.Text }}" {{ with $.Title}} title="{{ . }}" {{ end }} />
{{- end -}}
{{- if $img -}}
{{- if eq $img.MediaType.MainType "video" -}}
<figure>
<video class="video-shortcode" preload="metadata" controls>
<source src="{{ $img.RelPermalink }}">
There should have been a video here but your browser does not seem to support it.
</video>
</figure>
{{- else -}}
<img loading="lazy" src="{{ $img.RelPermalink }}" type="" alt="{{ $.Text }}" {{ with $.Title}} title="{{ . }}" {{ end }} />
{{- end -}}
{{- else -}} {{- else -}}
{{- warnf "No image found for %s from %s" .Destination .Page.File }} {{/* internal image */}}
{{- $img := .Page.Resources.GetMatch .Destination -}}
{{- if not $img -}}
{{- $img = resources.Get .Destination -}}
{{- end -}}
{{- if $img -}}
{{- if eq $img.MediaType.MainType "video" -}}
<figure>
<video class="video-shortcode" preload="metadata" controls>
<source src="{{ $img.RelPermalink }}">
There should have been a video here but your browser does not seem to support it.
</video>
</figure>
{{- else -}}
<img loading="lazy" src="{{ $img.RelPermalink }}" type="" alt="{{ $.Text }}" {{ with $.Title}} title="{{ . }}" {{ end }} />
{{- end -}}
{{- else -}}
{{- warnf "No image found for %s from %s" .Destination .Page.File }}
{{- end -}}
{{- end -}} {{- end -}}