Skip to content

Commit 99802ce

Browse files
ggodlewskipierzchala-m
authored andcommitted
Update render-image.html
Signed-off-by: Grzegorz Godlewski <gg@gitgis.com>
1 parent e14d555 commit 99802ce

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed
Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,54 @@
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 }} />
442
</object>
543
{{ else }}
6-
<img src="{{ .Destination | safeURL }}" alt="{{ .Text }}" {{ with .Title}} title="{{ . }}"{{ end }} />
44+
<img src="{{ $src }}" alt="{{ .PlainText }}" {{ with .Title }} title="{{ . }}" {{ end }} />
745
{{ 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

Comments
 (0)