Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(astro): allow URLs in meta.image #422

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions docs/tutorialkit.dev/src/content/docs/reference/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,11 @@ type DownloadAsZip =

```

##### `meta`
### `meta`

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

The `MetaTagsSchema` type has the following shape:
Expand All @@ -449,14 +450,21 @@ meta:
image: /cover.png
title: Title shown on social media and search engines
description: Description shown on social media and search engines

meta:
image: /cover.png # Resolves to public/cover.png

meta:
image: 'https://tutorialkit.dev/tutorialkit-opengraph.png' # URL is used as is

```

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

##### `custom`
### `custom`

Assign custom fields to a chapter/part/lesson.
<PropertyTable inherited type="Record<string,any>" />
Expand Down
9 changes: 6 additions & 3 deletions packages/astro/src/default/components/MetaTags.astro
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ interface Props {
meta?: MetaTagsConfig;
}
const { meta = {} } = Astro.props;
let imageUrl;
if (meta.image) {
imageUrl = readPublicAsset(meta.image, true);
let imageUrl = meta.image;

if (imageUrl?.startsWith('/') || imageUrl?.startsWith('.')) {
imageUrl = readPublicAsset(imageUrl, true);

if (!imageUrl) {
console.warn(`Image ${meta.image} not found in "/public" folder`);
}
}

imageUrl ??= readLogoFile('logo', true);
---

Expand Down
6 changes: 6 additions & 0 deletions packages/astro/src/default/pages/index.astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
import MetaTags from '../components/MetaTags.astro';
import { getTutorial } from '../utils/content';
import { joinPaths } from '../utils/url';

Expand All @@ -9,11 +10,16 @@ const part = lesson.part && tutorial.parts[lesson.part.id];
const chapter = lesson.chapter && part?.chapters[lesson.chapter.id];

const slug = [part?.slug, chapter?.slug, lesson.slug].filter(Boolean).join('/');
const meta = lesson.data?.meta ?? {};

meta.title ??= [lesson.part?.title, lesson.chapter?.title, lesson.data.title].filter(Boolean).join(' / ');
meta.description ??= 'A TutorialKit interactive lesson';

const redirect = joinPaths(import.meta.env.BASE_URL, `/${slug}`);
---

<!doctype html>
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<title>Redirecting to {redirect}</title>
AriPerkkio marked this conversation as resolved.
Show resolved Hide resolved
<MetaTags slot="meta" meta={meta} />
<meta http-equiv="refresh" content=`0;url=${redirect}` />
2 changes: 1 addition & 1 deletion packages/types/src/schemas/metatags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const metaTagsSchema = z.object({
* Ideally we would want to use `image` from:
* https://docs.astro.build/en/guides/images/#images-in-content-collections .
*/
.describe('A relative path to an image that lives in the public folder to show on social previews.'),
.describe('Image to show on social previews. A relative path is resolved to the public folder.'),
description: z.string().optional().describe('A description for metadata'),
title: z.string().optional().describe('A title to use specifically for metadata'),
});
Expand Down