|
1 | | -{{ if strings.HasSuffix .Destination ".svg"}} |
2 | | -<object type="image/svg+xml" data="{{ .Destination | safeURL }}" > |
3 | | -<img src="{{ .Destination | safeURL }}" alt="{{ .Text }}" {{ with .Title}} title="{{ . }}"{{ end }} /> |
| 1 | +{{- $u := urls.Parse .Destination -}} |
| 2 | +{{- $src := $u.String -}} |
| 3 | +{{- if not $u.IsAbs -}} |
| 4 | +{{ $parsedURL := urls.Parse .Page.Site.BaseURL }} |
| 5 | +{{ $basePath := $parsedURL.Path }} |
| 6 | +{{ $markdownDir := .Page.File.Dir }} |
| 7 | +{{ $fullURLDir := printf "%s%s" $basePath $markdownDir }} |
| 8 | +{{ $imageRelativePath := .Destination }} |
| 9 | +{{ $imgPath := path.Join $fullURLDir $imageRelativePath }} |
| 10 | +{{ $imgUrl := $imgPath | relURL }} |
| 11 | +{{ $siteUrl := .Page.RelPermalink }} |
| 12 | + |
| 13 | +{{- $baseSegs := split (trim $siteUrl "/") "/" -}} |
| 14 | +{{- $targetSegs := split (trim $imgUrl "/") "/" -}} |
| 15 | + |
| 16 | +{{- $s := newScratch -}} |
| 17 | +{{- $s.Set "common" 0 -}} |
| 18 | +{{- $s.Set "done" false -}} |
| 19 | +{{- range $i, $seg := $baseSegs -}} |
| 20 | + {{- if not ($s.Get "done") -}} |
| 21 | + {{- if and (lt $i (len $targetSegs)) (eq $seg (index $targetSegs $i)) -}} |
| 22 | + {{- $s.Set "common" (add ($s.Get "common") 1) -}} |
| 23 | + {{- else -}} |
| 24 | + {{- $s.Set "done" true -}} |
| 25 | + {{- end -}} |
| 26 | + {{- end -}} |
| 27 | +{{- end -}} |
| 28 | + |
| 29 | +{{- $common := $s.Get "common" -}} |
| 30 | +{{- $ups := sub (len $baseSegs) $common -}} |
| 31 | +{{- $tail := delimit (after $common $targetSegs) "/" -}} |
| 32 | + |
| 33 | +{{- $s.Set "up" "" -}} |
| 34 | +{{- range seq $ups -}} |
| 35 | + {{- $s.Set "up" (printf "%s../" ($s.Get "up")) -}} |
| 36 | +{{- end -}} |
| 37 | +{{- $src := printf "%s%s" ($s.Get "up") $tail -}} |
| 38 | + |
| 39 | +{{ if strings.HasSuffix .Destination ".svg" }} |
| 40 | +<object type="image/svg+xml" data="{{ $src }}" > |
| 41 | + <img src="{{ $src }}" alt="{{ .PlainText }}" {{ with .Title }} title="{{ . }}" {{ end }} /> |
4 | 42 | </object> |
5 | 43 | {{ else }} |
6 | | -<img src="{{ .Destination | safeURL }}" alt="{{ .Text }}" {{ with .Title}} title="{{ . }}"{{ end }} /> |
| 44 | +<img src="{{ $src }}" alt="{{ .PlainText }}" {{ with .Title }} title="{{ . }}" {{ end }} /> |
7 | 45 | {{ end }} |
| 46 | +{{ else }} |
| 47 | +{{ if strings.HasSuffix .Destination ".svg" }} |
| 48 | +<object type="image/svg+xml" data="{{ $src }}" > |
| 49 | + <img src="{{ $src }}" alt="{{ .PlainText }}" {{ with .Title }} title="{{ . }}" {{ end }} /> |
| 50 | +</object> |
| 51 | +{{ else }} |
| 52 | +<img src="{{ $src }}" alt="{{ .PlainText }}" {{ with .Title }} title="{{ . }}" {{ end }} /> |
| 53 | +{{ end }} |
| 54 | +{{- end -}} |
0 commit comments