Introduce expandable image shortcode, improve portable links logic

This commit is contained in:
Alex Shpak
2025-09-08 20:04:04 +02:00
parent dbaaa516e8
commit 249682f9dd
13 changed files with 191 additions and 68 deletions
+4 -17
View File
@@ -1,19 +1,6 @@
{{- $destination := .Destination -}}
{{- if .Page.Site.Params.BookPortableLinks -}}
{{- template "portable-image" . -}}
{{- else -}}
<img src="{{ .Destination | safeURL }}" alt="{{ .Text }}" {{ with .Title }}title="{{ . }}"{{ end }}/>
{{- end -}}
{{- define "portable-image" -}}
{{- $isRemote := or (in .Destination "://") (strings.HasPrefix .Destination "//") }}
{{- if not $isRemote }}
{{- $path := print .Page.File.Dir .Destination }}
{{- if strings.HasPrefix .Destination "/" }}
{{- $path = print "/static" .Destination }}
{{- end }}
{{- if not (fileExists $path) }}
{{- warnf "Image '%s' not found in '%s'" .Destination .Page.File }}
{{- end }}
{{- end }}
<img src="{{ .Destination | safeURL }}" alt="{{ .Text }}" {{ with .Title }}title="{{ . }}"{{ end }}/>
{{- $destination = partial "docs/links/portable-image" . -}}
{{- end -}}
<img src="{{ $destination | safeURL }}" alt="{{ .Text }}" {{ with .Title }}title="{{ . }}"{{ end }}/>
{{- /**/ -}}
+4 -27
View File
@@ -1,29 +1,6 @@
{{- $destination := .Destination -}}
{{- if .Page.Site.Params.BookPortableLinks -}}
{{- template "portable-link" . -}}
{{- else -}}
<a href="{{ .Destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}>{{ .Text | safeHTML }}</a>
{{- end -}}
{{- define "portable-link" -}}
{{- $destination := .Destination }}
{{- $isRemote := or (in .Destination ":") (strings.HasPrefix .Destination "//") }}
{{- $isFragment := strings.HasPrefix .Destination "#" }}
{{- if and (not $isRemote) (not $isFragment) }}
{{- $url := urls.Parse .Destination }}
{{- $path := strings.TrimSuffix "/_index.md" $url.Path }}
{{- $path = strings.TrimSuffix "/_index" $path }}
{{- $path = strings.TrimSuffix ".md" $path }}
{{- $page := .Page.GetPage $path }}
{{- if $page }}
{{- $destination = $page.RelPermalink }}
{{- if $url.Fragment }}
{{- $destination = print $destination "#" $url.Fragment }}
{{- end }}
{{- else if fileExists (print .Page.File.Dir .Destination) }}
<!-- Nothing -->
{{- else -}}
{{- warnf "Page '%s' not found in '%s'" .Destination .Page.File }}
{{- end }}
{{- end }}
<a href="{{ $destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}>{{ .Text | safeHTML }}</a>
{{- $destination = partial "docs/links/portable-link" . -}}
{{- end -}}
<a href="{{ $destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}>{{ .Text | safeHTML }}</a>
{{- /**/ -}}
@@ -0,0 +1,26 @@
{{- $destination := .Destination -}}
{{- $url := urls.Parse .Destination -}}
{{- $isRemote := gt (len $url.Host) 0 -}}
{{- $isFragment := strings.HasPrefix .Destination "#" -}}
{{- if and (not $isRemote) (not $isFragment) -}}
{{- $path := strings.TrimPrefix "./" $url.Path -}}
{{- $path = strings.TrimPrefix "/assets/" $path -}}
{{- with (.Page.Resources.Get $path) -}}
{{- $destination = .RelPermalink -}}
{{- else with (resources.Get $path) -}}
{{- $destination = .RelPermalink -}}
{{- else -}}
{{- warnf "Image reference '%s' not found in '%s'" .Destination .Page.Permalink -}}
{{- end -}}
{{- with $url.RawQuery -}}
{{- $destination = print $destination "?" . -}}
{{- end -}}
{{- with $url.Fragment -}}
{{- $destination = print $destination "#" . -}}
{{- end -}}
{{- end -}}
{{- return $destination -}}
@@ -0,0 +1,29 @@
{{- $destination := .Destination -}}
{{- $url := urls.Parse .Destination -}}
{{- $isRemote := gt (len $url.Host) 0 -}}
{{- $isFragment := strings.HasPrefix .Destination "#" -}}
{{- if and (not $isRemote) (not $isFragment) -}}
{{- $path := strings.TrimPrefix "./" $url.Path -}}
{{- $path = strings.TrimSuffix ".md" $path -}}
{{- $path = strings.TrimSuffix "/_index" $path -}}
{{- with (.Page.GetPage $path) -}}
{{- $destination = .RelPermalink -}}
{{- else with (.Page.Resources.Get $path) -}}
{{- $destination = .RelPermalink -}}
{{- else with (resources.Get $path) -}}
{{- $destination = .RelPermalink -}}
{{- else -}}
{{- warnf "Reference '%s' not found in '%s'" .Destination .Page.Permalink -}}
{{- end -}}
{{- with $url.RawQuery -}}
{{- $destination = print $destination "?" . -}}
{{- end -}}
{{- with $url.Fragment -}}
{{- $destination = print $destination "#" . -}}
{{- end -}}
{{- end -}}
{{- return $destination -}}
+2 -2
View File
@@ -10,8 +10,8 @@
<ul>
{{ range . }}
<li>
{{ $local := or (.Page) (strings.HasPrefix .URL "/") }}
<a href="{{ .URL }}" {{ with .Params.class }}class="{{ . }}"{{ end }} {{ if not $local }}target="_blank" rel="noopener"{{ end }}>
{{ $isRemote := not (or (.Page) (strings.HasPrefix .URL "/")) }}
<a href="{{ .URL }}" {{ with .Params.class }}class="{{ . }}"{{ end }} {{ if $isRemote }}target="_blank" rel="noopener"{{ end }}>
{{- .Pre -}}
{{ with .Page }}
{{ partial "docs/title" .Page }}
+7 -5
View File
@@ -1,17 +1,19 @@
{{- /* remove whitespace */ -}}
{{- if .Get "href" -}}
<a class="book-card{{ with .Get "class" }} {{ . }}{{ end }}"{{ with .Get "href" }} href="{{ . | relURL }}"{{ end }}>
{{- template "book-card-content" . -}}
{{- with .Get "href" -}}
{{- $destination := partial "docs/links/portable-link" (dict "Page" $.Page "Destination" .) -}}
<a class="book-card{{ with $.Get "class" }} {{ . }}{{ end }}" href="{{ $destination | safeURL }}">
{{- template "book-card-content" $ -}}
</a>
{{- else -}}
<div class="book-card{{ with .Get "class" }} {{ . }}{{ end }}">
{{- template "book-card-content" . -}}
{{- template "book-card-content" $ -}}
</div>
{{- end -}}
{{- define "book-card-content" -}}
{{- with .Get "image" -}}
<img src="{{ . | urls.RelLangURL }}" />
{{- $destination := partial "docs/links/portable-image" (dict "Page" $.Page "Destination" .) -}}
<img src="{{ $destination | safeURL }}" />
{{- end }}
<div class="markdown-inner">
{{ with .InnerDeindent }}{{ . }}{{ end }}
+12
View File
@@ -0,0 +1,12 @@
{{- /* remove whitespaces */ -}}
{{- $destination := partial "docs/links/portable-image" (dict "Page" .Page "Destination" (.Get "src")) -}}
<label class="book-image" for="book-image-toggle-{{ .Ordinal }}">
<input class="hidden toggle" type="checkbox" id="book-image-toggle-{{ .Ordinal }}" />
<img src="{{ $destination | safeURL }}"
{{- with .Get "alt"}} alt="{{ . }}"{{ end -}}
{{- with .Get "title"}} title="{{ . }}"{{ end -}}
{{- with .Get "loading"}} loading="{{ . }}"{{ end -}}
{{- with .Get "class"}} class="{{ . }}"{{ end -}}
/>
</label>
{{- /* remove whitespaces */ -}}