Skip to content

Commit

Permalink
feat: support custom canonical URLs (#83)
Browse files Browse the repository at this point in the history
Adding an optional attribute in the posts' frontmatter that allows the user to set a custom canonical URL
  • Loading branch information
ngarbezza authored and satnaing committed Jul 23, 2023
1 parent 0838309 commit 4687bd5
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/content/_schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const blogSchema = z
tags: z.array(z.string()).default(["others"]),
ogImage: z.string().optional(),
description: z.string(),
canonicalURL: z.string().optional(),
})
.strict();

Expand Down
4 changes: 2 additions & 2 deletions src/content/blog/adding-new-post.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Here is the list of frontmatter property for each post.
| **_draft_** | Mark this post 'unpublished'. | default = false |
| **_tags_** | Related keywords for this post. Written in array yaml format. | default = others |
| **_ogImage_** | OG image of the post. Useful for social media sharing and SEO. | default = SITE.ogImage or generated SVG image |
| **_canonicalUrl_** | Canonical URL (absolute), in case the article already exists on other source. | default = `Astro.site` + `Astro.url.pathname` |
| **_canonicalURL_** | Canonical URL (absolute), in case the article already exists on other source. | default = `Astro.site` + `Astro.url.pathname` |

Only `title`, `description` and `pubDatetime` fields in frontmatter must be specified.

Expand Down Expand Up @@ -74,7 +74,7 @@ tags:
- tags
ogImage: ""
description: This is the example description of the example post.
canonicalUrl: https://example.org/my-article-was-already-posted-here
canonicalURL: https://example.org/my-article-was-already-posted-here
---
```

Expand Down
4 changes: 2 additions & 2 deletions src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ export interface Props {
author?: string;
description?: string;
ogImage?: string;
canonicalURL?: string;
}
const {
title = SITE.title,
author = SITE.author,
description = SITE.desc,
ogImage = SITE.ogImage,
canonicalURL = new URL(Astro.url.pathname, Astro.site).href,
} = Astro.props;
const canonicalURL = new URL(Astro.url.pathname, Astro.site);
const socialImageURL = new URL(
ogImage ? ogImage : SITE.ogImage,
Astro.url.origin
Expand Down
4 changes: 2 additions & 2 deletions src/layouts/PostDetails.astro
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ export interface Props {
const { post } = Astro.props;
const { title, author, description, ogImage, pubDatetime, tags } = post.data;
const { title, author, description, ogImage, canonicalURL, pubDatetime, tags } = post.data;
const { Content } = await post.render();
const ogUrl = new URL(ogImage ? ogImage : `${title}.png`, Astro.url.origin)
.href;
---

<Layout title={title} author={author} description={description} ogImage={ogUrl}>
<Layout title={title} author={author} description={description} ogImage={ogUrl} canonicalURL={canonicalURL}>
<Header />
<div class="mx-auto flex w-full max-w-3xl justify-start px-2">
<button
Expand Down

0 comments on commit 4687bd5

Please sign in to comment.