Skip to content

Commit 469f1b2

Browse files
committed
Configure Twitter cards with thumbnail images
1 parent 1357749 commit 469f1b2

File tree

10 files changed

+40
-12
lines changed

10 files changed

+40
-12
lines changed

src/main/java/zone/nox/Site.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class Site implements SiteConfiguration {
1919
private static final Path SOCIAL_LINKS = RESOURCES.resolve("social-icons");
2020
private static final Path VIDEOS = Path.of("src/main/resources/videos").toAbsolutePath();
2121
private static final Path IMAGES = Path.of("src/main/resources/images").toAbsolutePath();
22+
private static final Path THUMBNAILS = Path.of("src/main/resources/thumbnails").toAbsolutePath();
2223

2324
private final Config config;
2425

@@ -49,6 +50,9 @@ public Outline createOutline(Outliner outliner) {
4950
outliner
5051
.sourceBinaryFiles("images", IMAGES)
5152
.storeResource(res -> "/" + IMAGES.getParent().relativize(res.file()));
53+
outliner
54+
.sourceBinaryFiles("thumbnails", THUMBNAILS)
55+
.storeResource(res -> "/" + THUMBNAILS.getParent().relativize(res.file()));
5256

5357
outliner
5458
.sourceTextFiles("posts", POSTS)
@@ -60,6 +64,11 @@ public Outline createOutline(Outliner outliner) {
6064
outliner.generate(new PostPage(Target.from(config)));
6165
outliner.generate(new FourOhFour());
6266
outliner.generateStaticResources(Path.of(""), "favicon.ico");
67+
outliner.generateStaticResources(
68+
Path.of("thumbnails"),
69+
"/thumbnails/12-last-year.jpg",
70+
"/thumbnails/13-mexico.jpg"
71+
);
6372

6473
return outliner.build();
6574
}

src/main/java/zone/nox/components/Components.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
public class Components {
1010

11-
public static Layout layout = new Layout(null, null, List.of());
11+
public static Layout layout = new Layout(null, null, Optional.empty(), List.of());
1212

1313
public static Header header = new Header();
1414

src/main/java/zone/nox/components/Layout.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@
1111
import java.nio.charset.StandardCharsets;
1212
import java.util.List;
1313
import java.util.Locale;
14+
import java.util.Optional;
1415

1516
import static dev.nipafx.ginevra.html.HtmlElement.body;
1617
import static dev.nipafx.ginevra.html.HtmlElement.div;
1718
import static dev.nipafx.ginevra.html.HtmlElement.document;
1819
import static dev.nipafx.ginevra.html.HtmlElement.head;
1920
import static dev.nipafx.ginevra.html.HtmlElement.meta;
21+
import static dev.nipafx.ginevra.util.CollectionUtils.plus;
2022
import static zone.nox.components.Components.footer;
2123
import static zone.nox.components.Components.header;
2224

23-
public record Layout(String title, String description, List<? extends Element> content) implements Component {
25+
public record Layout(String title, String description, Optional<String> thumbnail, List<? extends Element> content) implements Component {
2426

2527
public record Style(Classes layout, Classes page, Classes header, Classes content, Classes footer, Css css) implements CssStyle { }
2628

@@ -132,14 +134,24 @@ public record Style(Classes layout, Classes page, Classes header, Classes conten
132134

133135
@Override
134136
public Element compose() {
137+
var metaElements = List.of(
138+
meta.name("viewport").content("width=device-width, initial-scale=1"),
139+
meta.name("description").content(description),
140+
meta.name("twitter:title").content(title),
141+
meta.name("twitter:description").content(description)
142+
);
143+
var card = thumbnail.map(thumb -> List.of(
144+
meta.name("twitter:image").content("https://nox.zone/thumbnails/" + thumb),
145+
meta.name("twitter:card").content("summary_large_image")))
146+
.orElse(List.of(meta.name("twitter:card").content("summary")));
147+
metaElements = plus(metaElements, card);
148+
135149
return document
136150
.language(Locale.US)
137151
.head(head
138152
.charset(StandardCharsets.UTF_8)
139153
.title(title)
140-
.children(
141-
meta.name("viewport").content("width=device-width, initial-scale=1"),
142-
meta.name("description").content(description)))
154+
.children(metaElements))
143155
.body(body
144156
.classes(STYLE.layout)
145157
.children(div
@@ -151,19 +163,23 @@ public Element compose() {
151163
}
152164

153165
public Layout title(String title) {
154-
return new Layout(title, this.description, this.content);
166+
return new Layout(title, this.description, this.thumbnail, this.content);
155167
}
156168

157169
public Layout description(String description) {
158-
return new Layout(this.title, description, this.content);
170+
return new Layout(this.title, description, this.thumbnail, this.content);
171+
}
172+
173+
public Layout thumbnail(Optional<String> thumbnail) {
174+
return new Layout(this.title, this.description, thumbnail, this.content);
159175
}
160176

161177
public Layout content(List<? extends Element> children) {
162-
return new Layout(this.title, this.description, children);
178+
return new Layout(this.title, this.description, this.thumbnail, children);
163179
}
164180

165181
public Layout content(Element... children) {
166-
return new Layout(this.title, this.description, List.of(children));
182+
return new Layout(this.title, this.description, this.thumbnail, List.of(children));
167183
}
168184

169185
}

src/main/java/zone/nox/data/Post.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import static java.util.function.Predicate.not;
1212

1313
public record Post(
14-
String title, String summary, String slug, Integer index, String name, LocalDateTime date,
14+
String title, String summary, String slug, Integer index, String name, LocalDateTime date, Optional<String> thumbnail,
1515
Optional<String> localFile, Optional<String> youTubeId, HtmlContent content)
1616
implements Document {
1717

@@ -31,7 +31,7 @@ public static Post fromMd(Md post) {
3131
// setting it to the empty string
3232
.filter(not(String::isBlank));
3333
var content = post.contentParsedAsMarkdown();
34-
return new Post(post.title(), post.summary(), slug, index, name, date, localFile, post.youTubeId(), content);
34+
return new Post(post.title(), post.summary(), slug, index, name, date, post.thumbnail(), localFile, post.youTubeId(), content);
3535
}
3636

3737
private static String[] getFileNameParts(Md post) {
@@ -45,7 +45,7 @@ private static String[] getFileNameParts(Md post) {
4545
}
4646

4747
public record Md(
48-
String title, String summary, Optional<String> localFile, Optional<String> youTubeId,
48+
String title, String summary, Optional<String> thumbnail, Optional<String> localFile, Optional<String> youTubeId,
4949
Path file, HtmlContent contentParsedAsMarkdown)
5050
implements Document { }
5151

src/main/java/zone/nox/templates/PostPage.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ private Element composePage(Post post) {
4141
return layout
4242
.title(post.title())
4343
.description(post.summary())
44+
.thumbnail(post.thumbnail())
4445
.content(
4546
postContent(post)
4647
.embedLocalVideo(target.embedLocalVideo())

src/main/resources/posts/012--2025-04-13-1425--arrived.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: "Nox Is Going Nowhere"
33
summary: "Even after the blackout, Nox is still in Neotropolis"
4+
thumbnail: "12-last-year.jpg"
45
---
56

67
Things didn't really go according to plan, a year ago.

src/main/resources/posts/013--2025-04-14-0806--usa.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: "\"Sleep Now in the Fire\""
33
summary: "The past shaped our present and the present shapes our future - don't let anyone tell you otherwise"
4+
thumbnail: "13-mexico.jpg"
45
---
56

67
When we look at the world around us, we don't just see the present, we also see our past, the integral over the set of humanity's decisions.

src/main/resources/thumbnails/.gitkeep

Whitespace-only changes.
88.1 KB
Loading
36.6 KB
Loading

0 commit comments

Comments
 (0)