Skip to content

Commit d0087ef

Browse files
committed
Added handlers for resolving image roots
1 parent 28b7260 commit d0087ef

3 files changed

Lines changed: 62 additions & 7 deletions

File tree

Components/Pages/GuidePage.razor

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,35 @@
4040
}
4141

4242
@{
43-
var ogImage = !string.IsNullOrWhiteSpace(_ogImage)
44-
? _ogImage
45-
: Brand.DefaultOgImage;
43+
var ogImage = !string.IsNullOrWhiteSpace(_ogImage) ? _ogImage : Brand.DefaultOgImage;
44+
var squareImage = !string.IsNullOrWhiteSpace(_squareImage) ? _squareImage : "";
4645
}
46+
4747
@if (!string.IsNullOrWhiteSpace(ogImage))
4848
{
4949
<meta property="og:image" content="@ToAbsolute(ogImage)" />
5050
<meta name="twitter:card" content="summary_large_image" />
5151
}
52-
else
52+
53+
@if (!string.IsNullOrWhiteSpace(squareImage))
5354
{
54-
<meta name="twitter:card" content="summary" />
55+
<meta property="og:image" content="@ToAbsolute(squareImage)" />
56+
<meta property="og:image:width" content="1024" />
57+
<meta property="og:image:height" content="1024" />
58+
59+
60+
<link rel="icon"
61+
type="image/webp"
62+
href="@ToAbsolute(_squareImage)" />
63+
64+
<link rel="apple-touch-icon"
65+
href="@ToAbsolute(_squareImage)" />
66+
67+
<meta name="msapplication-TileImage"
68+
content="@ToAbsolute(_squareImage)" />
5569
}
5670

71+
5772
<meta name="twitter:title" content="@_pageTitleFull" />
5873
@if (!string.IsNullOrWhiteSpace(_description))
5974
{
@@ -161,6 +176,8 @@ else
161176
private string _title = "Loading...";
162177
private string? _description;
163178
private string? _ogImage;
179+
private string? _squareImage;
180+
164181
private string? _canonical;
165182
private bool _noIndex;
166183

@@ -234,6 +251,8 @@ protected override async Task OnParametersSetAsync()
234251
_title = "Not found";
235252
_description = null;
236253
_ogImage = null;
254+
_squareImage = doc.SquareImage;
255+
237256
_noIndex = true;
238257
_canonical = BuildCanonical(ctx, slug ?? "");
239258
return;
@@ -243,6 +262,8 @@ protected override async Task OnParametersSetAsync()
243262
_title = doc.Title;
244263
_description = doc.Description;
245264
_ogImage = doc.OgImage;
265+
_squareImage = doc.SquareImage;
266+
246267
_noIndex = doc.NoIndex;
247268
_canonical = BuildCanonical(ctx, doc.Slug, doc.Canonical);
248269

Data/MarkdownDoc.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public sealed class MarkdownDoc
1717
public string? Canonical { get; init; }
1818
public bool NoIndex { get; init; }
1919
public string? OgImage { get; init; }
20+
public string? SquareImage { get; init; }
2021

2122
public required string Html { get; init; }
2223
public required DateTime LastModifiedUtc { get; init; }

Services/MarkdownStore.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ private bool HasIndex(string slug)
126126
var description = fm.GetString("description");
127127
var canonical = fm.GetString("canonical");
128128
var noindex = fm.GetBool("noindex", defaultValue: false);
129-
var ogImage = fm.GetString("og_image") ?? fm.GetString("og:image");
130129
var gameName = fm.GetString("game") ?? title;
131130
var leaderboardUrl = fm.GetString("leaderboard");
132131
var timingMethod = fm.GetString("timing_method"); // e.g. "RTA (AutoSplitter + LRT)" etc
@@ -135,6 +134,13 @@ private bool HasIndex(string slug)
135134
var whereToBuy = fm.GetString("where_to_buy");
136135
var platforms = fm.GetList("platforms");
137136

137+
138+
var ogImageRaw = fm.GetString("og_image") ?? fm.GetString("og:image");
139+
var squareImageRaw = fm.GetString("square_image") ?? fm.GetString("square:image");
140+
141+
var ogImage = ResolveAssetPath(ogImageRaw, slug);
142+
var squareImage = ResolveAssetPath(squareImageRaw, slug);
143+
138144
var etag = ComputeETag(fi.Length, lastWrite);
139145

140146
var doc = new MarkdownDoc
@@ -156,7 +162,8 @@ private bool HasIndex(string slug)
156162
Html = body,
157163
LastModifiedUtc = lastWrite,
158164
ETag = etag,
159-
OgImage = ogImage
165+
OgImage = ogImage,
166+
SquareImage = squareImage
160167
};
161168

162169
Console.WriteLine($"slug={slug} path={path} exists={File.Exists(path)}");
@@ -340,6 +347,32 @@ public Task<List<DocEntry>> GetGuideIndexAsync(string host, CancellationToken ct
340347

341348
return Task.FromResult(items);
342349
}
350+
private static string? ResolveAssetPath(string? raw, string slug)
351+
{
352+
if (string.IsNullOrWhiteSpace(raw)) return null;
353+
354+
raw = raw.Trim();
355+
356+
// Already absolute URL
357+
if (raw.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
358+
raw.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
359+
return raw;
360+
361+
// Already absolute path (site-root)
362+
if (raw.StartsWith("/"))
363+
return raw;
364+
365+
// Relative to the doc folder
366+
// slug is like "guide/template-game" or "guide/template-game/index"
367+
var cleanSlug = (slug ?? "").Trim('/');
368+
369+
if (cleanSlug.EndsWith("/index", StringComparison.OrdinalIgnoreCase))
370+
cleanSlug = cleanSlug[..^"/index".Length].TrimEnd('/');
371+
372+
// folder base under content
373+
// -> /content/guide/template-game/ + raw
374+
return $"/content/{cleanSlug}/{raw}".Replace("\\", "/");
375+
}
343376

344377

345378
private sealed record CacheEntry(DateTime LastWriteUtc, MarkdownDoc Doc);

0 commit comments

Comments
 (0)