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

feat(open-graph): Allow custom OG images from any paths or URLs #1592

Open
wants to merge 2 commits into
base: v4
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion docs/features/social images.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ The following properties can be used to customize your link previews:
| `socialDescription` | `description` | Description to be used for preview. |
| `socialImage` | `image`, `cover` | Link to preview image. |

The `socialImage` property should contain a link to an image relative to `quartz/static`. If you have a folder for all your images in `quartz/static/my-images`, an example for `socialImage` could be `"my-images/cover.png"`.
The `socialImage` property should be a link to an image. It can be relative to your base URL, or you can provide a full URL with scheme (HTTP protocol only). For example:

1. If you have an image `cover.png` in `quartz/static/`, the `socialImage` value could be `"static/cover.png"`.
2. If your image is in `content/assets/`, then the `socialImage` value would become `"assets/cover.png"`.
3. If your image is hosted elsewhere, say `https://example.com/cover.png`, then the `socialImage` value should be `"https://example.com/cover.png"`.

> [!info] Info
>
Expand Down
6 changes: 5 additions & 1 deletion quartz/components/Head.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ const defaultOptions: SocialImageOptions = {
excludeRoot: false,
}

const urlSchemeRegex = new RegExp("^(http|https)://", "i")

export default (() => {
let fontsPromise: Promise<SatoriOptions["fonts"]>

Expand Down Expand Up @@ -147,7 +149,9 @@ export default (() => {

// Override with frontmatter url if existing
if (frontmatterImgUrl) {
ogImagePath = `https://${cfg.baseUrl}/static/${frontmatterImgUrl}`
ogImagePath = urlSchemeRegex.test(frontmatterImgUrl)
? frontmatterImgUrl
: `https://${cfg.baseUrl}/${frontmatterImgUrl}`
}

// Url of current page
Expand Down