Skip to content

Commit

Permalink
Merge pull request #43 from runofthemillgeek/feat/md-img-relative-links
Browse files Browse the repository at this point in the history
Support relative paths in markdown images
  • Loading branch information
tomfran authored Nov 5, 2024
2 parents c80f6e2 + 9f5b8e8 commit 4e760d1
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions layouts/_default/_markup/render-image.html
Original file line number Diff line number Diff line change
@@ -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 "#" }}

{{/*
Expand All @@ -18,7 +46,7 @@
<figure class="{{ $classes }}">

<div>
<img loading="lazy" alt="{{ .Text }}" src=" {{ $url }}">
<img loading="lazy" alt="{{ .Text }}" src="{{ $url }}">
</div>

{{ with .Title }}
Expand Down

0 comments on commit 4e760d1

Please sign in to comment.