diff options
author | Francesco Tomaselli <tomaselli.fr@gmail.com> | 2024-11-05 22:04:54 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-05 22:04:54 +0100 |
commit | f025f972e0b1ffb8df288be632bb3c34a58b66e8 (patch) | |
tree | 98d31689f7185f5ad26f3b1a9feb599e0feccef7 /layouts | |
parent | d8f5706dc3be687d07930608c483a528d6ae2bad (diff) | |
parent | bd698f9e62b0c15b877d38ad5df4a477a65350e9 (diff) |
Merge pull request #43 from runofthemillgeek/feat/md-img-relative-links
Support relative paths in markdown images
Diffstat (limited to 'layouts')
-rw-r--r-- | layouts/_default/_markup/render-image.html | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/layouts/_default/_markup/render-image.html b/layouts/_default/_markup/render-image.html index 25d893e..0930316 100644 --- a/layouts/_default/_markup/render-image.html +++ b/layouts/_default/_markup/render-image.html @@ -1,5 +1,33 @@ +{{/* +To get page resources and relative paths to work. Copied as is from default +hook: +https://github.com/gohugoio/hugo/blob/89bd02/tpl/tplimpl/embedded/templates/_default/_markup/render-image.html +*/}} +{{- $u := urls.Parse .Destination -}} +{{- $url := $u.String -}} +{{- if not $u.IsAbs -}} + {{- $path := strings.TrimPrefix "./" $u.Path -}} + {{- $imgResource := .Page.Scratch.Get "typoNilVariable" -}} + {{/* Check if this is a page bundle or standalone page */}} + {{- if .PageInner.Resources -}} + {{- $imgResource = .PageInner.Resources.Get $path -}} + {{- else if (or .PageInner.Parent .PageInner.Parent.Resources) -}} + {{- $imgResource = .PageInner.Parent.Resources.Get $path -}} + {{- end -}} + {{- $imgResource := or $imgResource (resources.Get $path) -}} + {{- with $imgResource -}} + {{- $url = .RelPermalink -}} + {{- with $u.RawQuery -}} + {{- $url = printf "%s?%s" $url . -}} + {{- end -}} + {{- with $u.Fragment -}} + {{- $url = printf "%s#%s" $url . -}} + {{- end -}} + {{- end -}} +{{- end -}} + {{/* Split URL at # */}} -{{ $url := .Destination | safeURL }} +{{ $url = $url | safeURL }} {{ $file_name_array := split $url "#" }} {{/* @@ -18,7 +46,7 @@ and build the img class string as "img-tag1 img-tag2 ..." <figure class="{{ $classes }}"> <div> - <img loading="lazy" alt="{{ .Text }}" src=" {{ $url }}"> + <img loading="lazy" alt="{{ .Text }}" src="{{ $url }}"> </div> {{ with .Title }} |