Skip to content

Commit 249682f

Browse files
committed
Introduce expandable image shortcode, improve portable links logic
1 parent dbaaa51 commit 249682f

File tree

13 files changed

+191
-68
lines changed

13 files changed

+191
-68
lines changed

assets/_main.scss

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,7 @@ body[dir="rtl"] .book-menu {
334334

335335
// Responsive styles
336336
.book-menu-content,
337-
.book-toc-content,
338-
.book-page,
339-
.book-header aside,
340-
.markdown {
337+
.book-toc-content {
341338
transition: 0.2s ease-in-out;
342339
transition-property: transform, margin, opacity, visibility;
343340
will-change: transform, margin, opacity;

assets/_markdown.scss

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,6 @@
200200

201201
figure {
202202
margin: $padding-16 0;
203-
figcaption p {
204-
margin-top: 0;
205-
}
206203
}
207204
}
208205

assets/_shortcodes.scss

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
.book-badge {
121121
display: inline-block;
122122
font-size: $font-size-14;
123+
font-weight: $body-font-weight;
123124
vertical-align: middle;
124125
border-radius: $border-radius;
125126
overflow: hidden;
@@ -149,6 +150,8 @@
149150

150151
// {{< steps >}}
151152
.book-steps {
153+
position: relative;
154+
152155
>ol {
153156
counter-reset: steps;
154157
list-style: none;
@@ -191,32 +194,62 @@
191194
}
192195
}
193196

194-
// {{< card >}}
195-
.book-card {
196-
display: block;
197+
// {{< card >}}, {{< figure >}}
198+
.book-card, figure {
199+
display: inline-block;
197200
overflow: hidden;
198201
height: 100%;
199202
border-radius: $border-radius;
200203
border: $padding-1 solid var(--gray-200);
201204

205+
&[href], &[href]:visited {
206+
color: var(--body-font-color);
207+
}
202208
&[href]:hover {
203209
text-decoration: none;
204210
background: var(--gray-100);
205211
}
206-
&[href]:visited {
207-
color: var(--body-font-color);
208-
}
209212

210213
img {
214+
width: 100%;
211215
display: block;
212-
object-fit: contain;
213216
aspect-ratio: 4 / 3;
214217
object-fit: cover;
215218
}
216219

217220
.markdown-inner {
218221
padding: $padding-16;
219222
}
223+
224+
figcaption {
225+
@extend .markdown-inner;
226+
padding: $padding-16;
227+
}
228+
}
229+
230+
.book-image {
231+
input + img {
232+
cursor: zoom-in;
233+
transition: transform 0.2s ease-in-out;
234+
}
235+
236+
input:checked + img {
237+
position: fixed;
238+
239+
top: 0;
240+
left: 0;
241+
right: 0;
242+
bottom: 0;
243+
244+
background: var(--body-background);
245+
object-fit: contain;
246+
width: 100%;
247+
height: 100%;
248+
249+
z-index: 1;
250+
cursor: zoom-out;
251+
padding: $padding-16;
252+
}
220253
}
221254

222255
.book-hero {

exampleSite/content.en/docs/shortcodes/cards.md

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
## Example
44

55
{{% columns %}}
6-
- {{< card image="docs/shortcodes/cards/placeholder.svg" >}}
6+
- {{< card image="placeholder.svg" >}}
77
### Line 1
88
Line 2
99
{{< /card >}}
1010

11-
- {{< card image="docs/shortcodes/cards/placeholder.svg" >}}
11+
- {{< card image="placeholder.svg" >}}
1212
This is tab MacOS content.
1313
{{< /card >}}
1414
{{% /columns %}}
1515

1616
{{% columns %}}
17-
- {{< card href="/" >}}
17+
- {{< card href="/docs/shortcodes/cards" >}}
1818
**Markdown**
1919
Suspendisse sed congue orci, eu congue metus. Nullam feugiat urna massa.
2020
{{< /card >}}
@@ -28,3 +28,36 @@
2828
This is tab MacOS content.
2929
{{< /card >}}
3030
{{% /columns %}}
31+
32+
## Build-in figure shortcode
33+
Hugo's built-in figure shortcode is also styled as a card
34+
35+
{{% columns %}}
36+
- ```go-html-template
37+
{{</* figure
38+
src="placeholder.svg"
39+
alt="A placeholder image"
40+
link="#"
41+
caption="A placeholder image caption"
42+
loading="lazy"
43+
target="_blank"
44+
title="Figure Title"
45+
caption="A caption for the figure"
46+
attr="An attribution"
47+
attrlink="#"
48+
*/>}}
49+
```
50+
51+
- {{< figure
52+
src="placeholder.svg"
53+
alt="A placeholder image"
54+
link="#"
55+
caption="A placeholder image caption"
56+
loading="lazy"
57+
target="_blank"
58+
title="Figure Title"
59+
caption="A caption for the figure"
60+
attr="An attribution"
61+
attrlink="#"
62+
>}}
63+
{{% /columns %}}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Images
2+
3+
Image shortcode produces an image that can be clicked to expand.
4+
5+
## Example
6+
7+
```go-html-template
8+
{{</* image src="placeholder.svg" alt="A placeholder" title="A placeholder" loading="lazy" */>}}
9+
```
10+
{{< image src="placeholder.svg" alt="A placeholder" title="A placeholder" loading="lazy" >}}
11+
12+
## Parameters
13+
14+
`src` {{< badge style="warning" title="Required" >}}
15+
: The link to the image
16+
17+
`class` {{< badge style="info" title="Optional" >}}
18+
: An optional CSS class name that will be applied to the `img` element
19+
20+
`alt` {{< badge style="info" title="Optional" >}}
21+
: An optional alternate text for the image
22+
23+
`title` {{< badge style="info" title="Optional" >}}
24+
: An optional title for the image
25+
26+
`loading` {{< badge style="info" title="Optional" >}}
27+
: Sets `loading` control for the image: `lazy`, `eager` or `auto`
28+
29+
30+

layouts/_markup/render-image.html

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
1+
{{- $destination := .Destination -}}
12
{{- if .Page.Site.Params.BookPortableLinks -}}
2-
{{- template "portable-image" . -}}
3-
{{- else -}}
4-
<img src="{{ .Destination | safeURL }}" alt="{{ .Text }}" {{ with .Title }}title="{{ . }}"{{ end }}/>
5-
{{- end -}}
6-
7-
{{- define "portable-image" -}}
8-
{{- $isRemote := or (in .Destination "://") (strings.HasPrefix .Destination "//") }}
9-
{{- if not $isRemote }}
10-
{{- $path := print .Page.File.Dir .Destination }}
11-
{{- if strings.HasPrefix .Destination "/" }}
12-
{{- $path = print "/static" .Destination }}
13-
{{- end }}
14-
{{- if not (fileExists $path) }}
15-
{{- warnf "Image '%s' not found in '%s'" .Destination .Page.File }}
16-
{{- end }}
17-
{{- end }}
18-
<img src="{{ .Destination | safeURL }}" alt="{{ .Text }}" {{ with .Title }}title="{{ . }}"{{ end }}/>
3+
{{- $destination = partial "docs/links/portable-image" . -}}
194
{{- end -}}
5+
<img src="{{ $destination | safeURL }}" alt="{{ .Text }}" {{ with .Title }}title="{{ . }}"{{ end }}/>
6+
{{- /**/ -}}

layouts/_markup/render-link.html

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,6 @@
1+
{{- $destination := .Destination -}}
12
{{- if .Page.Site.Params.BookPortableLinks -}}
2-
{{- template "portable-link" . -}}
3-
{{- else -}}
4-
<a href="{{ .Destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}>{{ .Text | safeHTML }}</a>
5-
{{- end -}}
6-
7-
{{- define "portable-link" -}}
8-
{{- $destination := .Destination }}
9-
{{- $isRemote := or (in .Destination ":") (strings.HasPrefix .Destination "//") }}
10-
{{- $isFragment := strings.HasPrefix .Destination "#" }}
11-
{{- if and (not $isRemote) (not $isFragment) }}
12-
{{- $url := urls.Parse .Destination }}
13-
{{- $path := strings.TrimSuffix "/_index.md" $url.Path }}
14-
{{- $path = strings.TrimSuffix "/_index" $path }}
15-
{{- $path = strings.TrimSuffix ".md" $path }}
16-
{{- $page := .Page.GetPage $path }}
17-
{{- if $page }}
18-
{{- $destination = $page.RelPermalink }}
19-
{{- if $url.Fragment }}
20-
{{- $destination = print $destination "#" $url.Fragment }}
21-
{{- end }}
22-
{{- else if fileExists (print .Page.File.Dir .Destination) }}
23-
<!-- Nothing -->
24-
{{- else -}}
25-
{{- warnf "Page '%s' not found in '%s'" .Destination .Page.File }}
26-
{{- end }}
27-
{{- end }}
28-
<a href="{{ $destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}>{{ .Text | safeHTML }}</a>
3+
{{- $destination = partial "docs/links/portable-link" . -}}
294
{{- end -}}
5+
<a href="{{ $destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}>{{ .Text | safeHTML }}</a>
6+
{{- /**/ -}}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{{- $destination := .Destination -}}
2+
{{- $url := urls.Parse .Destination -}}
3+
4+
{{- $isRemote := gt (len $url.Host) 0 -}}
5+
{{- $isFragment := strings.HasPrefix .Destination "#" -}}
6+
7+
{{- if and (not $isRemote) (not $isFragment) -}}
8+
{{- $path := strings.TrimPrefix "./" $url.Path -}}
9+
{{- $path = strings.TrimPrefix "/assets/" $path -}}
10+
11+
{{- with (.Page.Resources.Get $path) -}}
12+
{{- $destination = .RelPermalink -}}
13+
{{- else with (resources.Get $path) -}}
14+
{{- $destination = .RelPermalink -}}
15+
{{- else -}}
16+
{{- warnf "Image reference '%s' not found in '%s'" .Destination .Page.Permalink -}}
17+
{{- end -}}
18+
19+
{{- with $url.RawQuery -}}
20+
{{- $destination = print $destination "?" . -}}
21+
{{- end -}}
22+
{{- with $url.Fragment -}}
23+
{{- $destination = print $destination "#" . -}}
24+
{{- end -}}
25+
{{- end -}}
26+
{{- return $destination -}}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{{- $destination := .Destination -}}
2+
{{- $url := urls.Parse .Destination -}}
3+
4+
{{- $isRemote := gt (len $url.Host) 0 -}}
5+
{{- $isFragment := strings.HasPrefix .Destination "#" -}}
6+
7+
{{- if and (not $isRemote) (not $isFragment) -}}
8+
{{- $path := strings.TrimPrefix "./" $url.Path -}}
9+
{{- $path = strings.TrimSuffix ".md" $path -}}
10+
{{- $path = strings.TrimSuffix "/_index" $path -}}
11+
12+
{{- with (.Page.GetPage $path) -}}
13+
{{- $destination = .RelPermalink -}}
14+
{{- else with (.Page.Resources.Get $path) -}}
15+
{{- $destination = .RelPermalink -}}
16+
{{- else with (resources.Get $path) -}}
17+
{{- $destination = .RelPermalink -}}
18+
{{- else -}}
19+
{{- warnf "Reference '%s' not found in '%s'" .Destination .Page.Permalink -}}
20+
{{- end -}}
21+
22+
{{- with $url.RawQuery -}}
23+
{{- $destination = print $destination "?" . -}}
24+
{{- end -}}
25+
{{- with $url.Fragment -}}
26+
{{- $destination = print $destination "#" . -}}
27+
{{- end -}}
28+
{{- end -}}
29+
{{- return $destination -}}

0 commit comments

Comments
 (0)