Skip to content

Commit 3125547

Browse files
authored
fix(astro): allow URLs in meta.image (#422)
1 parent ad624e4 commit 3125547

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

docs/tutorialkit.dev/src/content/docs/reference/configuration.mdx

+10-2
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,11 @@ type DownloadAsZip =
426426
427427
```
428428

429-
##### `meta`
429+
### `meta`
430430

431431
Configures `<meta>` tags for Open Graph protocole and Twitter.
432432
TutorialKit will use your logo as the default image.
433+
Relative paths are resolved to `public` directory.
433434
<PropertyTable inherited type="MetaTagsSchema" />
434435

435436
The `MetaTagsSchema` type has the following shape:
@@ -449,14 +450,21 @@ meta:
449450
image: /cover.png
450451
title: Title shown on social media and search engines
451452
description: Description shown on social media and search engines
453+
454+
meta:
455+
image: /cover.png # Resolves to public/cover.png
456+
457+
meta:
458+
image: 'https://tutorialkit.dev/tutorialkit-opengraph.png' # URL is used as is
459+
452460
```
453461

454462
:::tip
455463
If your logo uses the SVG format, it may not display on most social platforms.
456464
Use a raster format instead, such as WEBP or PNG.
457465
:::
458466

459-
##### `custom`
467+
### `custom`
460468

461469
Assign custom fields to a chapter/part/lesson.
462470
<PropertyTable inherited type="Record<string,any>" />

packages/astro/src/default/components/MetaTags.astro

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ interface Props {
77
meta?: MetaTagsConfig;
88
}
99
const { meta = {} } = Astro.props;
10-
let imageUrl;
11-
if (meta.image) {
12-
imageUrl = readPublicAsset(meta.image, true);
10+
let imageUrl = meta.image;
11+
12+
if (imageUrl?.startsWith('/') || imageUrl?.startsWith('.')) {
13+
imageUrl = readPublicAsset(imageUrl, true);
14+
1315
if (!imageUrl) {
1416
console.warn(`Image ${meta.image} not found in "/public" folder`);
1517
}
1618
}
19+
1720
imageUrl ??= readLogoFile('logo', true);
1821
---
1922

packages/astro/src/default/pages/index.astro

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---
2+
import MetaTags from '../components/MetaTags.astro';
23
import { getTutorial } from '../utils/content';
34
import { joinPaths } from '../utils/url';
45
@@ -9,11 +10,16 @@ const part = lesson.part && tutorial.parts[lesson.part.id];
910
const chapter = lesson.chapter && part?.chapters[lesson.chapter.id];
1011
1112
const slug = [part?.slug, chapter?.slug, lesson.slug].filter(Boolean).join('/');
13+
const meta = lesson.data?.meta ?? {};
14+
15+
meta.title ??= [lesson.part?.title, lesson.chapter?.title, lesson.data.title].filter(Boolean).join(' / ');
16+
meta.description ??= 'A TutorialKit interactive lesson';
1217
1318
const redirect = joinPaths(import.meta.env.BASE_URL, `/${slug}`);
1419
---
1520

1621
<!doctype html>
1722
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
1823
<title>Redirecting to {redirect}</title>
24+
<MetaTags slot="meta" meta={meta} />
1925
<meta http-equiv="refresh" content=`0;url=${redirect}` />

packages/types/src/schemas/metatags.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const metaTagsSchema = z.object({
88
* Ideally we would want to use `image` from:
99
* https://docs.astro.build/en/guides/images/#images-in-content-collections .
1010
*/
11-
.describe('A relative path to an image that lives in the public folder to show on social previews.'),
11+
.describe('Image to show on social previews. A relative path is resolved to the public folder.'),
1212
description: z.string().optional().describe('A description for metadata'),
1313
title: z.string().optional().describe('A title to use specifically for metadata'),
1414
});

0 commit comments

Comments
 (0)