mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-05-18 14:34:23 +02:00
make emitters async generators
This commit is contained in:
parent
5d50282124
commit
c5a8b199ae
@ -2,400 +2,18 @@
|
|||||||
title: "Social Media Preview Cards"
|
title: "Social Media Preview Cards"
|
||||||
---
|
---
|
||||||
|
|
||||||
A lot of social media platforms can display a rich preview for your website when sharing a link (most notably, a cover image, a title and a description). Quartz automatically handles most of this for you with reasonable defaults, but for more control, you can customize these by setting [[social images#Frontmatter Properties]].
|
A lot of social media platforms can display a rich preview for your website when sharing a link (most notably, a cover image, a title and a description).
|
||||||
Quartz can also dynamically generate and use new cover images for every page to be used in link previews on social media for you. To get started with this, set `generateSocialImages: true` in `quartz.config.ts`.
|
|
||||||
|
Quartz can also dynamically generate and use new cover images for every page to be used in link previews on social media for you.
|
||||||
|
|
||||||
## Showcase
|
## Showcase
|
||||||
|
|
||||||
After enabling `generateSocialImages` in `quartz.config.ts`, the social media link preview for [[authoring content | Authoring Content]] looks like this:
|
After enabling the [[CustomOgImages]] emitter plugin, the social media link preview for [[authoring content | Authoring Content]] looks like this:
|
||||||
|
|
||||||
| Light | Dark |
|
| Light | Dark |
|
||||||
| ----------------------------------- | ---------------------------------- |
|
| ----------------------------------- | ---------------------------------- |
|
||||||
| ![[social-image-preview-light.png]] | ![[social-image-preview-dark.png]] |
|
| ![[social-image-preview-light.png]] | ![[social-image-preview-dark.png]] |
|
||||||
|
|
||||||
For testing, it is recommended to use [opengraph.xyz](https://www.opengraph.xyz/) to see what the link to your page will look like on various platforms (more info under [[social images#local testing]]).
|
## Configuration
|
||||||
|
|
||||||
## Customization
|
This functionality is provided by the [[CustomOgImages]] plugin. See the plugin page for customization options.
|
||||||
|
|
||||||
You can customize how images will be generated in the quartz config.
|
|
||||||
|
|
||||||
For example, here's what the default configuration looks like if you set `generateSocialImages: true`:
|
|
||||||
|
|
||||||
```typescript title="quartz.config.ts"
|
|
||||||
generateSocialImages: {
|
|
||||||
colorScheme: "lightMode", // what colors to use for generating image, same as theme colors from config, valid values are "darkMode" and "lightMode"
|
|
||||||
width: 1200, // width to generate with (in pixels)
|
|
||||||
height: 630, // height to generate with (in pixels)
|
|
||||||
excludeRoot: false, // wether to exclude "/" index path to be excluded from auto generated images (false = use auto, true = use default og image)
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### Frontmatter Properties
|
|
||||||
|
|
||||||
> [!tip] Hint
|
|
||||||
>
|
|
||||||
> Overriding social media preview properties via frontmatter still works even if `generateSocialImages` is disabled.
|
|
||||||
|
|
||||||
The following properties can be used to customize your link previews:
|
|
||||||
|
|
||||||
| Property | Alias | Summary |
|
|
||||||
| ------------------- | ---------------- | ----------------------------------- |
|
|
||||||
| `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"`.
|
|
||||||
|
|
||||||
> [!info] Info
|
|
||||||
>
|
|
||||||
> The priority for what image will be used for the cover image looks like the following: `frontmatter property > generated image (if enabled) > default image`.
|
|
||||||
>
|
|
||||||
> The default image (`quartz/static/og-image.png`) will only be used as a fallback if nothing else is set. If `generateSocialImages` is enabled, it will be treated as the new default per page, but can be overwritten by setting the `socialImage` frontmatter property for that page.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### Fully customized image generation
|
|
||||||
|
|
||||||
You can fully customize how the images being generated look by passing your own component to `generateSocialImages.imageStructure`. This component takes html/css + some page metadata/config options and converts it to an image using [satori](https://github.com/vercel/satori). Vercel provides an [online playground](https://og-playground.vercel.app/) that can be used to preview how your html/css looks like as a picture. This is ideal for prototyping your custom design.
|
|
||||||
|
|
||||||
It is recommended to write your own image components in `quartz/util/og.tsx` or any other `.tsx` file, as passing them to the config won't work otherwise. An example of the default image component can be found in `og.tsx` in `defaultImage()`.
|
|
||||||
|
|
||||||
> [!tip] Hint
|
|
||||||
>
|
|
||||||
> Satori only supports a subset of all valid CSS properties. All supported properties can be found in their [documentation](https://github.com/vercel/satori#css).
|
|
||||||
|
|
||||||
Your custom image component should have the `SocialImageOptions["imageStructure"]` type, to make development easier for you. Using a component of this type, you will be passed the following variables:
|
|
||||||
|
|
||||||
```ts
|
|
||||||
imageStructure: (
|
|
||||||
cfg: GlobalConfiguration, // global Quartz config (useful for getting theme colors and other info)
|
|
||||||
userOpts: UserOpts, // options passed to `generateSocialImage`
|
|
||||||
title: string, // title of current page
|
|
||||||
description: string, // description of current page
|
|
||||||
fonts: SatoriOptions["fonts"], // header + body font
|
|
||||||
) => JSXInternal.Element
|
|
||||||
```
|
|
||||||
|
|
||||||
Now, you can let your creativity flow and design your own image component! For reference and some cool tips, you can check how the markup for the default image looks.
|
|
||||||
|
|
||||||
> [!example] Examples
|
|
||||||
>
|
|
||||||
> Here are some examples for markup you may need to get started:
|
|
||||||
>
|
|
||||||
> - Get a theme color
|
|
||||||
>
|
|
||||||
> `cfg.theme.colors[colorScheme].<colorName>`, where `<colorName>` corresponds to a key in `ColorScheme` (defined at the top of `quartz/util/theme.ts`)
|
|
||||||
>
|
|
||||||
> - Use the page title/description
|
|
||||||
>
|
|
||||||
> `<p>{title}</p>`/`<p>{description}</p>`
|
|
||||||
>
|
|
||||||
> - Use a font family
|
|
||||||
>
|
|
||||||
> Detailed in the Fonts chapter below
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### Fonts
|
|
||||||
|
|
||||||
You will also be passed an array containing a header and a body font (where the first entry is header and the second is body). The fonts matches the ones selected in `theme.typography.header` and `theme.typography.body` from `quartz.config.ts` and will be passed in the format required by [`satori`](https://github.com/vercel/satori). To use them in CSS, use the `.name` property (e.g. `fontFamily: fonts[1].name` to use the "body" font family).
|
|
||||||
|
|
||||||
An example of a component using the header font could look like this:
|
|
||||||
|
|
||||||
```tsx title="socialImage.tsx"
|
|
||||||
export const myImage: SocialImageOptions["imageStructure"] = (...) => {
|
|
||||||
return <p style={{ fontFamily: fonts[0].name }}>Cool Header!</p>
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
> [!example]- Local fonts
|
|
||||||
>
|
|
||||||
> For cases where you use a local fonts under `static` folder, make sure to set the correct `@font-face` in `custom.scss`
|
|
||||||
>
|
|
||||||
> ```scss title="custom.scss"
|
|
||||||
> @font-face {
|
|
||||||
> font-family: "Newsreader";
|
|
||||||
> font-style: normal;
|
|
||||||
> font-weight: normal;
|
|
||||||
> font-display: swap;
|
|
||||||
> src: url("/static/Newsreader.woff2") format("woff2");
|
|
||||||
> }
|
|
||||||
> ```
|
|
||||||
>
|
|
||||||
> Then in `quartz/util/og.tsx`, you can load the satori fonts like so:
|
|
||||||
>
|
|
||||||
> ```tsx title="quartz/util/og.tsx"
|
|
||||||
> const headerFont = joinSegments("static", "Newsreader.woff2")
|
|
||||||
> const bodyFont = joinSegments("static", "Newsreader.woff2")
|
|
||||||
>
|
|
||||||
> export async function getSatoriFont(cfg: GlobalConfiguration): Promise<SatoriOptions["fonts"]> {
|
|
||||||
> const headerWeight: FontWeight = 700
|
|
||||||
> const bodyWeight: FontWeight = 400
|
|
||||||
>
|
|
||||||
> const url = new URL(`https://${cfg.baseUrl ?? "example.com"}`)
|
|
||||||
>
|
|
||||||
> const [header, body] = await Promise.all(
|
|
||||||
> [headerFont, bodyFont].map((font) =>
|
|
||||||
> fetch(`${url.toString()}/${font}`).then((res) => res.arrayBuffer()),
|
|
||||||
> ),
|
|
||||||
> )
|
|
||||||
>
|
|
||||||
> return [
|
|
||||||
> { name: cfg.theme.typography.header, data: header, weight: headerWeight, style: "normal" },
|
|
||||||
> { name: cfg.theme.typography.body, data: body, weight: bodyWeight, style: "normal" },
|
|
||||||
> ]
|
|
||||||
> }
|
|
||||||
> ```
|
|
||||||
>
|
|
||||||
> This font then can be used with your custom structure
|
|
||||||
|
|
||||||
### Local testing
|
|
||||||
|
|
||||||
To test how the full preview of your page is going to look even before deploying, you can forward the port you're serving quartz on. In VSCode, this can easily be achieved following [this guide](https://code.visualstudio.com/docs/editor/port-forwarding) (make sure to set `Visibility` to `public` if testing on external tools like [opengraph.xyz](https://www.opengraph.xyz/)).
|
|
||||||
|
|
||||||
If you have `generateSocialImages` enabled, you can check out all generated images under `public/static/social-images`.
|
|
||||||
|
|
||||||
## Technical info
|
|
||||||
|
|
||||||
Images will be generated as `.webp` files, which helps to keep images small (the average image takes ~`19kB`). They are also compressed further using [sharp](https://sharp.pixelplumbing.com/).
|
|
||||||
|
|
||||||
When using images, the appropriate [Open Graph](https://ogp.me/) and [Twitter](https://developer.twitter.com/en/docs/twitter-for-websites/cards/guides/getting-started) meta tags will be set to ensure they work and look as expected.
|
|
||||||
|
|
||||||
## Examples
|
|
||||||
|
|
||||||
Besides the template for the default image generation (found under `quartz/util/og.tsx`), you can also add your own! To do this, you can either edit the source code of that file (not recommended) or create a new one (e.g. `customSocialImage.tsx`, source shown below).
|
|
||||||
|
|
||||||
After adding that file, you can update `quartz.config.ts` to use your image generation template as follows:
|
|
||||||
|
|
||||||
```ts
|
|
||||||
// Import component at start of file
|
|
||||||
import { customImage } from "./quartz/util/customSocialImage.tsx"
|
|
||||||
|
|
||||||
// In main config
|
|
||||||
const config: QuartzConfig = {
|
|
||||||
...
|
|
||||||
generateSocialImages: {
|
|
||||||
...
|
|
||||||
imageStructure: customImage, // tells quartz to use your component when generating images
|
|
||||||
},
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
The following example will generate images that look as follows:
|
|
||||||
|
|
||||||
| Light | Dark |
|
|
||||||
| ------------------------------------------ | ----------------------------------------- |
|
|
||||||
| ![[custom-social-image-preview-light.png]] | ![[custom-social-image-preview-dark.png]] |
|
|
||||||
|
|
||||||
This example (and the default template) use colors and fonts from your theme specified in the quartz config. Fonts get passed in as a prop, where `fonts[0]` will contain the header font and `fonts[1]` will contain the body font (more info in the [[#fonts]] section).
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
import { SatoriOptions } from "satori/wasm"
|
|
||||||
import { GlobalConfiguration } from "../cfg"
|
|
||||||
import { SocialImageOptions, UserOpts } from "./imageHelper"
|
|
||||||
import { QuartzPluginData } from "../plugins/vfile"
|
|
||||||
|
|
||||||
export const customImage: SocialImageOptions["imageStructure"] = (
|
|
||||||
cfg: GlobalConfiguration,
|
|
||||||
userOpts: UserOpts,
|
|
||||||
title: string,
|
|
||||||
description: string,
|
|
||||||
fonts: SatoriOptions["fonts"],
|
|
||||||
fileData: QuartzPluginData,
|
|
||||||
) => {
|
|
||||||
// How many characters are allowed before switching to smaller font
|
|
||||||
const fontBreakPoint = 22
|
|
||||||
const useSmallerFont = title.length > fontBreakPoint
|
|
||||||
|
|
||||||
const { colorScheme } = userOpts
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
style={{
|
|
||||||
display: "flex",
|
|
||||||
flexDirection: "row",
|
|
||||||
justifyContent: "flex-start",
|
|
||||||
alignItems: "center",
|
|
||||||
height: "100%",
|
|
||||||
width: "100%",
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<div
|
|
||||||
style={{
|
|
||||||
display: "flex",
|
|
||||||
alignItems: "center",
|
|
||||||
justifyContent: "center",
|
|
||||||
height: "100%",
|
|
||||||
width: "100%",
|
|
||||||
backgroundColor: cfg.theme.colors[colorScheme].light,
|
|
||||||
flexDirection: "column",
|
|
||||||
gap: "2.5rem",
|
|
||||||
paddingTop: "2rem",
|
|
||||||
paddingBottom: "2rem",
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<p
|
|
||||||
style={{
|
|
||||||
color: cfg.theme.colors[colorScheme].dark,
|
|
||||||
fontSize: useSmallerFont ? 70 : 82,
|
|
||||||
marginLeft: "4rem",
|
|
||||||
textAlign: "center",
|
|
||||||
marginRight: "4rem",
|
|
||||||
fontFamily: fonts[0].name,
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{title}
|
|
||||||
</p>
|
|
||||||
<p
|
|
||||||
style={{
|
|
||||||
color: cfg.theme.colors[colorScheme].dark,
|
|
||||||
fontSize: 44,
|
|
||||||
marginLeft: "8rem",
|
|
||||||
marginRight: "8rem",
|
|
||||||
lineClamp: 3,
|
|
||||||
fontFamily: fonts[1].name,
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{description}
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
<div
|
|
||||||
style={{
|
|
||||||
height: "100%",
|
|
||||||
width: "2vw",
|
|
||||||
position: "absolute",
|
|
||||||
backgroundColor: cfg.theme.colors[colorScheme].tertiary,
|
|
||||||
opacity: 0.85,
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
> [!example]- Advanced example
|
|
||||||
>
|
|
||||||
> The following example includes a customized social image with a custom background and formatted date.
|
|
||||||
>
|
|
||||||
> ```typescript title="custom-og.tsx"
|
|
||||||
> export const og: SocialImageOptions["Component"] = (
|
|
||||||
> cfg: GlobalConfiguration,
|
|
||||||
> fileData: QuartzPluginData,
|
|
||||||
> { colorScheme }: Options,
|
|
||||||
> title: string,
|
|
||||||
> description: string,
|
|
||||||
> fonts: SatoriOptions["fonts"],
|
|
||||||
> ) => {
|
|
||||||
> let created: string | undefined
|
|
||||||
> let reading: string | undefined
|
|
||||||
> if (fileData.dates) {
|
|
||||||
> created = formatDate(getDate(cfg, fileData)!, cfg.locale)
|
|
||||||
> }
|
|
||||||
> const { minutes, text: _timeTaken, words: _words } = readingTime(fileData.text!)
|
|
||||||
> reading = i18n(cfg.locale).components.contentMeta.readingTime({
|
|
||||||
> minutes: Math.ceil(minutes),
|
|
||||||
> })
|
|
||||||
>
|
|
||||||
> const Li = [created, reading]
|
|
||||||
>
|
|
||||||
> return (
|
|
||||||
> <div
|
|
||||||
> style={{
|
|
||||||
> position: "relative",
|
|
||||||
> display: "flex",
|
|
||||||
> flexDirection: "row",
|
|
||||||
> alignItems: "flex-start",
|
|
||||||
> height: "100%",
|
|
||||||
> width: "100%",
|
|
||||||
> backgroundImage: `url("https://${cfg.baseUrl}/static/og-image.jpeg")`,
|
|
||||||
> backgroundSize: "100% 100%",
|
|
||||||
> }}
|
|
||||||
> >
|
|
||||||
> <div
|
|
||||||
> style={{
|
|
||||||
> position: "absolute",
|
|
||||||
> top: 0,
|
|
||||||
> left: 0,
|
|
||||||
> right: 0,
|
|
||||||
> bottom: 0,
|
|
||||||
> background: "radial-gradient(circle at center, transparent, rgba(0, 0, 0, 0.4) 70%)",
|
|
||||||
> }}
|
|
||||||
> />
|
|
||||||
> <div
|
|
||||||
> style={{
|
|
||||||
> display: "flex",
|
|
||||||
> height: "100%",
|
|
||||||
> width: "100%",
|
|
||||||
> flexDirection: "column",
|
|
||||||
> justifyContent: "flex-start",
|
|
||||||
> alignItems: "flex-start",
|
|
||||||
> gap: "1.5rem",
|
|
||||||
> paddingTop: "4rem",
|
|
||||||
> paddingBottom: "4rem",
|
|
||||||
> marginLeft: "4rem",
|
|
||||||
> }}
|
|
||||||
> >
|
|
||||||
> <img
|
|
||||||
> src={`"https://${cfg.baseUrl}/static/icon.jpeg"`}
|
|
||||||
> style={{
|
|
||||||
> position: "relative",
|
|
||||||
> backgroundClip: "border-box",
|
|
||||||
> borderRadius: "6rem",
|
|
||||||
> }}
|
|
||||||
> width={80}
|
|
||||||
> />
|
|
||||||
> <div
|
|
||||||
> style={{
|
|
||||||
> display: "flex",
|
|
||||||
> flexDirection: "column",
|
|
||||||
> textAlign: "left",
|
|
||||||
> fontFamily: fonts[0].name,
|
|
||||||
> }}
|
|
||||||
> >
|
|
||||||
> <h2
|
|
||||||
> style={{
|
|
||||||
> color: cfg.theme.colors[colorScheme].light,
|
|
||||||
> fontSize: "3rem",
|
|
||||||
> fontWeight: 700,
|
|
||||||
> marginRight: "4rem",
|
|
||||||
> fontFamily: fonts[0].name,
|
|
||||||
> }}
|
|
||||||
> >
|
|
||||||
> {title}
|
|
||||||
> </h2>
|
|
||||||
> <ul
|
|
||||||
> style={{
|
|
||||||
> color: cfg.theme.colors[colorScheme].gray,
|
|
||||||
> gap: "1rem",
|
|
||||||
> fontSize: "1.5rem",
|
|
||||||
> fontFamily: fonts[1].name,
|
|
||||||
> }}
|
|
||||||
> >
|
|
||||||
> {Li.map((item, index) => {
|
|
||||||
> if (item) {
|
|
||||||
> return <li key={index}>{item}</li>
|
|
||||||
> }
|
|
||||||
> })}
|
|
||||||
> </ul>
|
|
||||||
> </div>
|
|
||||||
> <p
|
|
||||||
> style={{
|
|
||||||
> color: cfg.theme.colors[colorScheme].light,
|
|
||||||
> fontSize: "1.5rem",
|
|
||||||
> overflow: "hidden",
|
|
||||||
> marginRight: "8rem",
|
|
||||||
> textOverflow: "ellipsis",
|
|
||||||
> display: "-webkit-box",
|
|
||||||
> WebkitLineClamp: 7,
|
|
||||||
> WebkitBoxOrient: "vertical",
|
|
||||||
> lineClamp: 7,
|
|
||||||
> fontFamily: fonts[1].name,
|
|
||||||
> }}
|
|
||||||
> >
|
|
||||||
> {description}
|
|
||||||
> </p>
|
|
||||||
> </div>
|
|
||||||
> </div>
|
|
||||||
> )
|
|
||||||
> }
|
|
||||||
> ```
|
|
||||||
|
352
docs/plugins/CustomOgImages.md
Normal file
352
docs/plugins/CustomOgImages.md
Normal file
@ -0,0 +1,352 @@
|
|||||||
|
---
|
||||||
|
title: Custom OG Images
|
||||||
|
tags:
|
||||||
|
- feature/emitter
|
||||||
|
---
|
||||||
|
|
||||||
|
The Custom OG Images emitter plugin generates social media preview images for your pages. It uses [satori](https://github.com/vercel/satori) to convert HTML/CSS into images, allowing you to create beautiful and consistent social media preview cards for your content.
|
||||||
|
|
||||||
|
> [!note]
|
||||||
|
> For information on how to add, remove or configure plugins, see the [[configuration#Plugins|Configuration]] page.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- Automatically generates social media preview images for each page
|
||||||
|
- Supports both light and dark mode themes
|
||||||
|
- Customizable through frontmatter properties
|
||||||
|
- Fallback to default image when needed
|
||||||
|
- Full control over image design through custom components
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
> [!info] Info
|
||||||
|
>
|
||||||
|
> The `baseUrl` property in your [[configuration]] must be set properly for social images to work correctly, as they require absolute paths.
|
||||||
|
|
||||||
|
This plugin accepts the following configuration options:
|
||||||
|
|
||||||
|
```typescript title="quartz.config.ts"
|
||||||
|
import { CustomOgImages } from "./quartz/plugins/emitters/ogImage"
|
||||||
|
|
||||||
|
const config: QuartzConfig = {
|
||||||
|
plugins: {
|
||||||
|
emitters: [
|
||||||
|
CustomOgImages({
|
||||||
|
colorScheme: "lightMode", // what colors to use for generating image, same as theme colors from config, valid values are "darkMode" and "lightMode"
|
||||||
|
width: 1200, // width to generate with (in pixels)
|
||||||
|
height: 630, // height to generate with (in pixels)
|
||||||
|
excludeRoot: false, // wether to exclude "/" index path to be excluded from auto generated images (false = use auto, true = use default og image)
|
||||||
|
imageStructure: defaultImage, // custom image component to use
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Configuration Options
|
||||||
|
|
||||||
|
| Option | Type | Default | Description |
|
||||||
|
| ---------------- | --------- | ------------ | ----------------------------------------------------------------- |
|
||||||
|
| `colorScheme` | string | "lightMode" | Theme to use for generating images ("darkMode" or "lightMode") |
|
||||||
|
| `width` | number | 1200 | Width of the generated image in pixels |
|
||||||
|
| `height` | number | 630 | Height of the generated image in pixels |
|
||||||
|
| `excludeRoot` | boolean | false | Whether to exclude the root index page from auto-generated images |
|
||||||
|
| `imageStructure` | component | defaultImage | Custom component to use for image generation |
|
||||||
|
|
||||||
|
## Frontmatter Properties
|
||||||
|
|
||||||
|
The following properties can be used to customize your link previews:
|
||||||
|
|
||||||
|
| Property | Alias | Summary |
|
||||||
|
| ------------------- | ---------------- | ----------------------------------- |
|
||||||
|
| `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"`.
|
||||||
|
|
||||||
|
> [!info] Info
|
||||||
|
>
|
||||||
|
> The priority for what image will be used for the cover image looks like the following: `frontmatter property > generated image (if enabled) > default image`.
|
||||||
|
>
|
||||||
|
> The default image (`quartz/static/og-image.png`) will only be used as a fallback if nothing else is set. If the Custom OG Images emitter plugin is enabled, it will be treated as the new default per page, but can be overwritten by setting the `socialImage` frontmatter property for that page.
|
||||||
|
|
||||||
|
## Customization
|
||||||
|
|
||||||
|
You can fully customize how the images being generated look by passing your own component to `imageStructure`. This component takes JSX + some page metadata/config options and converts it to an image using [satori](https://github.com/vercel/satori). Vercel provides an [online playground](https://og-playground.vercel.app/) that can be used to preview how your JSX looks like as a picture. This is ideal for prototyping your custom design.
|
||||||
|
|
||||||
|
### Fonts
|
||||||
|
|
||||||
|
You will also be passed an array containing a header and a body font (where the first entry is header and the second is body). The fonts matches the ones selected in `theme.typography.header` and `theme.typography.body` from `quartz.config.ts` and will be passed in the format required by [`satori`](https://github.com/vercel/satori). To use them in CSS, use the `.name` property (e.g. `fontFamily: fonts[1].name` to use the "body" font family).
|
||||||
|
|
||||||
|
An example of a component using the header font could look like this:
|
||||||
|
|
||||||
|
```tsx title="socialImage.tsx"
|
||||||
|
export const myImage: SocialImageOptions["imageStructure"] = (...) => {
|
||||||
|
return <p style={{ fontFamily: fonts[0].name }}>Cool Header!</p>
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
> [!example]- Local fonts
|
||||||
|
>
|
||||||
|
> For cases where you use a local fonts under `static` folder, make sure to set the correct `@font-face` in `custom.scss`
|
||||||
|
>
|
||||||
|
> ```scss title="custom.scss"
|
||||||
|
> @font-face {
|
||||||
|
> font-family: "Newsreader";
|
||||||
|
> font-style: normal;
|
||||||
|
> font-weight: normal;
|
||||||
|
> font-display: swap;
|
||||||
|
> src: url("/static/Newsreader.woff2") format("woff2");
|
||||||
|
> }
|
||||||
|
> ```
|
||||||
|
>
|
||||||
|
> Then in `quartz/util/og.tsx`, you can load the Satori fonts like so:
|
||||||
|
>
|
||||||
|
> ```tsx title="quartz/util/og.tsx"
|
||||||
|
> import { joinSegments, QUARTZ } from "../path"
|
||||||
|
> import fs from "fs"
|
||||||
|
> import path from "path"
|
||||||
|
>
|
||||||
|
> const headerFont = joinSegments(QUARTZ, "static", "Newsreader.woff2")
|
||||||
|
> const bodyFont = joinSegments(QUARTZ, "static", "Newsreader.woff2")
|
||||||
|
>
|
||||||
|
> export async function getSatoriFont(cfg: GlobalConfiguration): Promise<SatoriOptions["fonts"]> {
|
||||||
|
> const headerWeight: FontWeight = 700
|
||||||
|
> const bodyWeight: FontWeight = 400
|
||||||
|
>
|
||||||
|
> const [header, body] = await Promise.all(
|
||||||
|
> [headerFont, bodyFont].map((font) =>
|
||||||
|
> fs.promises.readFile(path.resolve(font))
|
||||||
|
> ),
|
||||||
|
> )
|
||||||
|
>
|
||||||
|
> return [
|
||||||
|
> { name: cfg.theme.typography.header, data: header, weight: headerWeight, style: "normal" },
|
||||||
|
> { name: cfg.theme.typography.body, data: body, weight: bodyWeight, style: "normal" },
|
||||||
|
> ]
|
||||||
|
> }
|
||||||
|
> ```
|
||||||
|
>
|
||||||
|
> This font then can be used with your custom structure
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
Here are some example image components you can use as a starting point:
|
||||||
|
|
||||||
|
### Basic Example
|
||||||
|
|
||||||
|
This example will generate images that look as follows:
|
||||||
|
|
||||||
|
| Light | Dark |
|
||||||
|
| ------------------------------------------ | ----------------------------------------- |
|
||||||
|
| ![[custom-social-image-preview-light.png]] | ![[custom-social-image-preview-dark.png]] |
|
||||||
|
|
||||||
|
```tsx
|
||||||
|
import { SatoriOptions } from "satori/wasm"
|
||||||
|
import { GlobalConfiguration } from "../cfg"
|
||||||
|
import { SocialImageOptions, UserOpts } from "./imageHelper"
|
||||||
|
import { QuartzPluginData } from "../plugins/vfile"
|
||||||
|
|
||||||
|
export const customImage: SocialImageOptions["imageStructure"] = (
|
||||||
|
cfg: GlobalConfiguration,
|
||||||
|
userOpts: UserOpts,
|
||||||
|
title: string,
|
||||||
|
description: string,
|
||||||
|
fonts: SatoriOptions["fonts"],
|
||||||
|
fileData: QuartzPluginData,
|
||||||
|
) => {
|
||||||
|
// How many characters are allowed before switching to smaller font
|
||||||
|
const fontBreakPoint = 22
|
||||||
|
const useSmallerFont = title.length > fontBreakPoint
|
||||||
|
|
||||||
|
const { colorScheme } = userOpts
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
display: "flex",
|
||||||
|
flexDirection: "row",
|
||||||
|
justifyContent: "flex-start",
|
||||||
|
alignItems: "center",
|
||||||
|
height: "100%",
|
||||||
|
width: "100%",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
display: "flex",
|
||||||
|
alignItems: "center",
|
||||||
|
justifyContent: "center",
|
||||||
|
height: "100%",
|
||||||
|
width: "100%",
|
||||||
|
backgroundColor: cfg.theme.colors[colorScheme].light,
|
||||||
|
flexDirection: "column",
|
||||||
|
gap: "2.5rem",
|
||||||
|
paddingTop: "2rem",
|
||||||
|
paddingBottom: "2rem",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<p
|
||||||
|
style={{
|
||||||
|
color: cfg.theme.colors[colorScheme].dark,
|
||||||
|
fontSize: useSmallerFont ? 70 : 82,
|
||||||
|
marginLeft: "4rem",
|
||||||
|
textAlign: "center",
|
||||||
|
marginRight: "4rem",
|
||||||
|
fontFamily: fonts[0].name,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{title}
|
||||||
|
</p>
|
||||||
|
<p
|
||||||
|
style={{
|
||||||
|
color: cfg.theme.colors[colorScheme].dark,
|
||||||
|
fontSize: 44,
|
||||||
|
marginLeft: "8rem",
|
||||||
|
marginRight: "8rem",
|
||||||
|
lineClamp: 3,
|
||||||
|
fontFamily: fonts[1].name,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{description}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
height: "100%",
|
||||||
|
width: "2vw",
|
||||||
|
position: "absolute",
|
||||||
|
backgroundColor: cfg.theme.colors[colorScheme].tertiary,
|
||||||
|
opacity: 0.85,
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Advanced Example
|
||||||
|
|
||||||
|
The following example includes a customized social image with a custom background and formatted date:
|
||||||
|
|
||||||
|
```typescript title="custom-og.tsx"
|
||||||
|
export const og: SocialImageOptions["Component"] = (
|
||||||
|
cfg: GlobalConfiguration,
|
||||||
|
fileData: QuartzPluginData,
|
||||||
|
{ colorScheme }: Options,
|
||||||
|
title: string,
|
||||||
|
description: string,
|
||||||
|
fonts: SatoriOptions["fonts"],
|
||||||
|
) => {
|
||||||
|
let created: string | undefined
|
||||||
|
let reading: string | undefined
|
||||||
|
if (fileData.dates) {
|
||||||
|
created = formatDate(getDate(cfg, fileData)!, cfg.locale)
|
||||||
|
}
|
||||||
|
const { minutes, text: _timeTaken, words: _words } = readingTime(fileData.text!)
|
||||||
|
reading = i18n(cfg.locale).components.contentMeta.readingTime({
|
||||||
|
minutes: Math.ceil(minutes),
|
||||||
|
})
|
||||||
|
|
||||||
|
const Li = [created, reading]
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
position: "relative",
|
||||||
|
display: "flex",
|
||||||
|
flexDirection: "row",
|
||||||
|
alignItems: "flex-start",
|
||||||
|
height: "100%",
|
||||||
|
width: "100%",
|
||||||
|
backgroundImage: `url("https://${cfg.baseUrl}/static/og-image.jpeg")`,
|
||||||
|
backgroundSize: "100% 100%",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
position: "absolute",
|
||||||
|
top: 0,
|
||||||
|
left: 0,
|
||||||
|
right: 0,
|
||||||
|
bottom: 0,
|
||||||
|
background: "radial-gradient(circle at center, transparent, rgba(0, 0, 0, 0.4) 70%)",
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
display: "flex",
|
||||||
|
height: "100%",
|
||||||
|
width: "100%",
|
||||||
|
flexDirection: "column",
|
||||||
|
justifyContent: "flex-start",
|
||||||
|
alignItems: "flex-start",
|
||||||
|
gap: "1.5rem",
|
||||||
|
paddingTop: "4rem",
|
||||||
|
paddingBottom: "4rem",
|
||||||
|
marginLeft: "4rem",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<img
|
||||||
|
src={`"https://${cfg.baseUrl}/static/icon.jpeg"`}
|
||||||
|
style={{
|
||||||
|
position: "relative",
|
||||||
|
backgroundClip: "border-box",
|
||||||
|
borderRadius: "6rem",
|
||||||
|
}}
|
||||||
|
width={80}
|
||||||
|
/>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
display: "flex",
|
||||||
|
flexDirection: "column",
|
||||||
|
textAlign: "left",
|
||||||
|
fontFamily: fonts[0].name,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<h2
|
||||||
|
style={{
|
||||||
|
color: cfg.theme.colors[colorScheme].light,
|
||||||
|
fontSize: "3rem",
|
||||||
|
fontWeight: 700,
|
||||||
|
marginRight: "4rem",
|
||||||
|
fontFamily: fonts[0].name,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{title}
|
||||||
|
</h2>
|
||||||
|
<ul
|
||||||
|
style={{
|
||||||
|
color: cfg.theme.colors[colorScheme].gray,
|
||||||
|
gap: "1rem",
|
||||||
|
fontSize: "1.5rem",
|
||||||
|
fontFamily: fonts[1].name,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{Li.map((item, index) => {
|
||||||
|
if (item) {
|
||||||
|
return <li key={index}>{item}</li>
|
||||||
|
}
|
||||||
|
})}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
<p
|
||||||
|
style={{
|
||||||
|
color: cfg.theme.colors[colorScheme].light,
|
||||||
|
fontSize: "1.5rem",
|
||||||
|
overflow: "hidden",
|
||||||
|
marginRight: "8rem",
|
||||||
|
textOverflow: "ellipsis",
|
||||||
|
display: "-webkit-box",
|
||||||
|
WebkitLineClamp: 7,
|
||||||
|
WebkitBoxOrient: "vertical",
|
||||||
|
lineClamp: 7,
|
||||||
|
fontFamily: fonts[1].name,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{description}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
```
|
@ -102,7 +102,7 @@ export default ((opts?: Partial<BreadcrumbOptions>) => {
|
|||||||
|
|
||||||
// Add current slug to full path
|
// Add current slug to full path
|
||||||
currentPath = joinSegments(currentPath, slugParts[i])
|
currentPath = joinSegments(currentPath, slugParts[i])
|
||||||
const includeTrailingSlash = !isTagPath || i < 1
|
const includeTrailingSlash = !isTagPath || i < slugParts.length - 1
|
||||||
|
|
||||||
// Format and add current crumb
|
// Format and add current crumb
|
||||||
const crumb = formatCrumb(
|
const crumb = formatCrumb(
|
||||||
|
@ -31,7 +31,7 @@ export const NotFoundPage: QuartzEmitterPlugin = () => {
|
|||||||
async getDependencyGraph(_ctx, _content, _resources) {
|
async getDependencyGraph(_ctx, _content, _resources) {
|
||||||
return new DepGraph<FilePath>()
|
return new DepGraph<FilePath>()
|
||||||
},
|
},
|
||||||
async emit(ctx, _content, resources): Promise<FilePath[]> {
|
async *emit(ctx, _content, resources) {
|
||||||
const cfg = ctx.cfg.configuration
|
const cfg = ctx.cfg.configuration
|
||||||
const slug = "404" as FullSlug
|
const slug = "404" as FullSlug
|
||||||
|
|
||||||
@ -55,14 +55,12 @@ export const NotFoundPage: QuartzEmitterPlugin = () => {
|
|||||||
allFiles: [],
|
allFiles: [],
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
yield write({
|
||||||
await write({
|
ctx,
|
||||||
ctx,
|
content: renderPage(cfg, slug, componentData, opts, externalResources),
|
||||||
content: renderPage(cfg, slug, componentData, opts, externalResources),
|
slug,
|
||||||
slug,
|
ext: ".html",
|
||||||
ext: ".html",
|
})
|
||||||
}),
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,15 +18,13 @@ export const AliasRedirects: QuartzEmitterPlugin = () => ({
|
|||||||
|
|
||||||
return graph
|
return graph
|
||||||
},
|
},
|
||||||
async emit(ctx, content, _resources): Promise<FilePath[]> {
|
async *emit(ctx, content, _resources) {
|
||||||
const fps: FilePath[] = []
|
|
||||||
|
|
||||||
for (const [_tree, file] of content) {
|
for (const [_tree, file] of content) {
|
||||||
const ogSlug = simplifySlug(file.data.slug!)
|
const ogSlug = simplifySlug(file.data.slug!)
|
||||||
|
|
||||||
for (const slug of file.data.aliases ?? []) {
|
for (const slug of file.data.aliases ?? []) {
|
||||||
const redirUrl = resolveRelative(slug, file.data.slug!)
|
const redirUrl = resolveRelative(slug, file.data.slug!)
|
||||||
const fp = await write({
|
yield write({
|
||||||
ctx,
|
ctx,
|
||||||
content: `
|
content: `
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
@ -43,10 +41,7 @@ export const AliasRedirects: QuartzEmitterPlugin = () => ({
|
|||||||
slug,
|
slug,
|
||||||
ext: ".html",
|
ext: ".html",
|
||||||
})
|
})
|
||||||
|
|
||||||
fps.push(fp)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return fps
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -33,10 +33,9 @@ export const Assets: QuartzEmitterPlugin = () => {
|
|||||||
|
|
||||||
return graph
|
return graph
|
||||||
},
|
},
|
||||||
async emit({ argv, cfg }, _content, _resources): Promise<FilePath[]> {
|
async *emit({ argv, cfg }, _content, _resources) {
|
||||||
const assetsPath = argv.output
|
const assetsPath = argv.output
|
||||||
const fps = await filesToCopy(argv, cfg)
|
const fps = await filesToCopy(argv, cfg)
|
||||||
const res: FilePath[] = []
|
|
||||||
for (const fp of fps) {
|
for (const fp of fps) {
|
||||||
const ext = path.extname(fp)
|
const ext = path.extname(fp)
|
||||||
const src = joinSegments(argv.directory, fp) as FilePath
|
const src = joinSegments(argv.directory, fp) as FilePath
|
||||||
@ -46,10 +45,8 @@ export const Assets: QuartzEmitterPlugin = () => {
|
|||||||
const dir = path.dirname(dest) as FilePath
|
const dir = path.dirname(dest) as FilePath
|
||||||
await fs.promises.mkdir(dir, { recursive: true }) // ensure dir exists
|
await fs.promises.mkdir(dir, { recursive: true }) // ensure dir exists
|
||||||
await fs.promises.copyFile(src, dest)
|
await fs.promises.copyFile(src, dest)
|
||||||
res.push(dest)
|
yield dest
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ export const CNAME: QuartzEmitterPlugin = () => ({
|
|||||||
async getDependencyGraph(_ctx, _content, _resources) {
|
async getDependencyGraph(_ctx, _content, _resources) {
|
||||||
return new DepGraph<FilePath>()
|
return new DepGraph<FilePath>()
|
||||||
},
|
},
|
||||||
async emit({ argv, cfg }, _content, _resources): Promise<FilePath[]> {
|
async emit({ argv, cfg }, _content, _resources) {
|
||||||
if (!cfg.configuration.baseUrl) {
|
if (!cfg.configuration.baseUrl) {
|
||||||
console.warn(chalk.yellow("CNAME emitter requires `baseUrl` to be set in your configuration"))
|
console.warn(chalk.yellow("CNAME emitter requires `baseUrl` to be set in your configuration"))
|
||||||
return []
|
return []
|
||||||
@ -24,7 +24,7 @@ export const CNAME: QuartzEmitterPlugin = () => ({
|
|||||||
if (!content) {
|
if (!content) {
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
fs.writeFileSync(path, content)
|
await fs.promises.writeFile(path, content)
|
||||||
return [path] as FilePath[]
|
return [path] as FilePath[]
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -9,7 +9,7 @@ import styles from "../../styles/custom.scss"
|
|||||||
import popoverStyle from "../../components/styles/popover.scss"
|
import popoverStyle from "../../components/styles/popover.scss"
|
||||||
import { BuildCtx } from "../../util/ctx"
|
import { BuildCtx } from "../../util/ctx"
|
||||||
import { QuartzComponent } from "../../components/types"
|
import { QuartzComponent } from "../../components/types"
|
||||||
import { googleFontHref, joinStyles } from "../../util/theme"
|
import { googleFontHref, joinStyles, processGoogleFonts } from "../../util/theme"
|
||||||
import { Features, transform } from "lightningcss"
|
import { Features, transform } from "lightningcss"
|
||||||
import { transform as transpile } from "esbuild"
|
import { transform as transpile } from "esbuild"
|
||||||
import { write } from "./helpers"
|
import { write } from "./helpers"
|
||||||
@ -207,8 +207,7 @@ export const ComponentResources: QuartzEmitterPlugin = () => {
|
|||||||
async getDependencyGraph(_ctx, _content, _resources) {
|
async getDependencyGraph(_ctx, _content, _resources) {
|
||||||
return new DepGraph<FilePath>()
|
return new DepGraph<FilePath>()
|
||||||
},
|
},
|
||||||
async emit(ctx, _content, _resources): Promise<FilePath[]> {
|
async *emit(ctx, _content, _resources) {
|
||||||
const promises: Promise<FilePath>[] = []
|
|
||||||
const cfg = ctx.cfg.configuration
|
const cfg = ctx.cfg.configuration
|
||||||
// component specific scripts and styles
|
// component specific scripts and styles
|
||||||
const componentResources = getComponentResources(ctx)
|
const componentResources = getComponentResources(ctx)
|
||||||
@ -217,42 +216,35 @@ export const ComponentResources: QuartzEmitterPlugin = () => {
|
|||||||
// let the user do it themselves in css
|
// let the user do it themselves in css
|
||||||
} else if (cfg.theme.fontOrigin === "googleFonts" && !cfg.theme.cdnCaching) {
|
} else if (cfg.theme.fontOrigin === "googleFonts" && !cfg.theme.cdnCaching) {
|
||||||
// when cdnCaching is true, we link to google fonts in Head.tsx
|
// when cdnCaching is true, we link to google fonts in Head.tsx
|
||||||
let match
|
const response = await fetch(googleFontHref(ctx.cfg.configuration.theme))
|
||||||
|
googleFontsStyleSheet = await response.text()
|
||||||
|
|
||||||
const fontSourceRegex = /url\((https:\/\/fonts.gstatic.com\/s\/[^)]+\.(woff2|ttf))\)/g
|
if (!cfg.baseUrl) {
|
||||||
|
throw new Error(
|
||||||
googleFontsStyleSheet = await (
|
"baseUrl must be defined when using Google Fonts without cfg.theme.cdnCaching",
|
||||||
await fetch(googleFontHref(ctx.cfg.configuration.theme))
|
|
||||||
).text()
|
|
||||||
|
|
||||||
while ((match = fontSourceRegex.exec(googleFontsStyleSheet)) !== null) {
|
|
||||||
// match[0] is the `url(path)`, match[1] is the `path`
|
|
||||||
const url = match[1]
|
|
||||||
// the static name of this file.
|
|
||||||
const [filename, ext] = url.split("/").pop()!.split(".")
|
|
||||||
|
|
||||||
googleFontsStyleSheet = googleFontsStyleSheet.replace(
|
|
||||||
url,
|
|
||||||
`https://${cfg.baseUrl}/static/fonts/${filename}.ttf`,
|
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
promises.push(
|
const { processedStylesheet, fontFiles } = await processGoogleFonts(
|
||||||
fetch(url)
|
googleFontsStyleSheet,
|
||||||
.then((res) => {
|
cfg.baseUrl,
|
||||||
if (!res.ok) {
|
)
|
||||||
throw new Error(`Failed to fetch font`)
|
googleFontsStyleSheet = processedStylesheet
|
||||||
}
|
|
||||||
return res.arrayBuffer()
|
// Download and save font files
|
||||||
})
|
for (const fontFile of fontFiles) {
|
||||||
.then((buf) =>
|
const res = await fetch(fontFile.url)
|
||||||
write({
|
if (!res.ok) {
|
||||||
ctx,
|
throw new Error(`failed to fetch font ${fontFile.filename}`)
|
||||||
slug: joinSegments("static", "fonts", filename) as FullSlug,
|
}
|
||||||
ext: `.${ext}`,
|
|
||||||
content: Buffer.from(buf),
|
const buf = await res.arrayBuffer()
|
||||||
}),
|
yield write({
|
||||||
),
|
ctx,
|
||||||
)
|
slug: joinSegments("static", "fonts", fontFile.filename) as FullSlug,
|
||||||
|
ext: `.${fontFile.extension}`,
|
||||||
|
content: Buffer.from(buf),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -267,45 +259,42 @@ export const ComponentResources: QuartzEmitterPlugin = () => {
|
|||||||
...componentResources.css,
|
...componentResources.css,
|
||||||
styles,
|
styles,
|
||||||
)
|
)
|
||||||
|
|
||||||
const [prescript, postscript] = await Promise.all([
|
const [prescript, postscript] = await Promise.all([
|
||||||
joinScripts(componentResources.beforeDOMLoaded),
|
joinScripts(componentResources.beforeDOMLoaded),
|
||||||
joinScripts(componentResources.afterDOMLoaded),
|
joinScripts(componentResources.afterDOMLoaded),
|
||||||
])
|
])
|
||||||
|
|
||||||
promises.push(
|
yield write({
|
||||||
write({
|
ctx,
|
||||||
ctx,
|
slug: "index" as FullSlug,
|
||||||
slug: "index" as FullSlug,
|
ext: ".css",
|
||||||
ext: ".css",
|
content: transform({
|
||||||
content: transform({
|
filename: "index.css",
|
||||||
filename: "index.css",
|
code: Buffer.from(stylesheet),
|
||||||
code: Buffer.from(stylesheet),
|
minify: true,
|
||||||
minify: true,
|
targets: {
|
||||||
targets: {
|
safari: (15 << 16) | (6 << 8), // 15.6
|
||||||
safari: (15 << 16) | (6 << 8), // 15.6
|
ios_saf: (15 << 16) | (6 << 8), // 15.6
|
||||||
ios_saf: (15 << 16) | (6 << 8), // 15.6
|
edge: 115 << 16,
|
||||||
edge: 115 << 16,
|
firefox: 102 << 16,
|
||||||
firefox: 102 << 16,
|
chrome: 109 << 16,
|
||||||
chrome: 109 << 16,
|
},
|
||||||
},
|
include: Features.MediaQueries,
|
||||||
include: Features.MediaQueries,
|
}).code.toString(),
|
||||||
}).code.toString(),
|
}),
|
||||||
}),
|
yield write({
|
||||||
write({
|
|
||||||
ctx,
|
ctx,
|
||||||
slug: "prescript" as FullSlug,
|
slug: "prescript" as FullSlug,
|
||||||
ext: ".js",
|
ext: ".js",
|
||||||
content: prescript,
|
content: prescript,
|
||||||
}),
|
}),
|
||||||
write({
|
yield write({
|
||||||
ctx,
|
ctx,
|
||||||
slug: "postscript" as FullSlug,
|
slug: "postscript" as FullSlug,
|
||||||
ext: ".js",
|
ext: ".js",
|
||||||
content: postscript,
|
content: postscript,
|
||||||
}),
|
})
|
||||||
)
|
|
||||||
|
|
||||||
return await Promise.all(promises)
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -116,9 +116,8 @@ export const ContentIndex: QuartzEmitterPlugin<Partial<Options>> = (opts) => {
|
|||||||
|
|
||||||
return graph
|
return graph
|
||||||
},
|
},
|
||||||
async emit(ctx, content, _resources) {
|
async *emit(ctx, content, _resources) {
|
||||||
const cfg = ctx.cfg.configuration
|
const cfg = ctx.cfg.configuration
|
||||||
const emitted: FilePath[] = []
|
|
||||||
const linkIndex: ContentIndexMap = new Map()
|
const linkIndex: ContentIndexMap = new Map()
|
||||||
for (const [tree, file] of content) {
|
for (const [tree, file] of content) {
|
||||||
const slug = file.data.slug!
|
const slug = file.data.slug!
|
||||||
@ -140,25 +139,21 @@ export const ContentIndex: QuartzEmitterPlugin<Partial<Options>> = (opts) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (opts?.enableSiteMap) {
|
if (opts?.enableSiteMap) {
|
||||||
emitted.push(
|
yield write({
|
||||||
await write({
|
ctx,
|
||||||
ctx,
|
content: generateSiteMap(cfg, linkIndex),
|
||||||
content: generateSiteMap(cfg, linkIndex),
|
slug: "sitemap" as FullSlug,
|
||||||
slug: "sitemap" as FullSlug,
|
ext: ".xml",
|
||||||
ext: ".xml",
|
})
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opts?.enableRSS) {
|
if (opts?.enableRSS) {
|
||||||
emitted.push(
|
yield write({
|
||||||
await write({
|
ctx,
|
||||||
ctx,
|
content: generateRSSFeed(cfg, linkIndex, opts.rssLimit),
|
||||||
content: generateRSSFeed(cfg, linkIndex, opts.rssLimit),
|
slug: (opts?.rssSlug ?? "index") as FullSlug,
|
||||||
slug: (opts?.rssSlug ?? "index") as FullSlug,
|
ext: ".xml",
|
||||||
ext: ".xml",
|
})
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const fp = joinSegments("static", "contentIndex") as FullSlug
|
const fp = joinSegments("static", "contentIndex") as FullSlug
|
||||||
@ -173,16 +168,12 @@ export const ContentIndex: QuartzEmitterPlugin<Partial<Options>> = (opts) => {
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
emitted.push(
|
yield write({
|
||||||
await write({
|
ctx,
|
||||||
ctx,
|
content: JSON.stringify(simplifiedIndex),
|
||||||
content: JSON.stringify(simplifiedIndex),
|
slug: fp,
|
||||||
slug: fp,
|
ext: ".json",
|
||||||
ext: ".json",
|
})
|
||||||
}),
|
|
||||||
)
|
|
||||||
|
|
||||||
return emitted
|
|
||||||
},
|
},
|
||||||
externalResources: (ctx) => {
|
externalResources: (ctx) => {
|
||||||
if (opts?.enableRSS) {
|
if (opts?.enableRSS) {
|
||||||
|
@ -94,9 +94,8 @@ export const ContentPage: QuartzEmitterPlugin<Partial<FullPageLayout>> = (userOp
|
|||||||
|
|
||||||
return graph
|
return graph
|
||||||
},
|
},
|
||||||
async emit(ctx, content, resources): Promise<FilePath[]> {
|
async *emit(ctx, content, resources) {
|
||||||
const cfg = ctx.cfg.configuration
|
const cfg = ctx.cfg.configuration
|
||||||
const fps: FilePath[] = []
|
|
||||||
const allFiles = content.map((c) => c[1].data)
|
const allFiles = content.map((c) => c[1].data)
|
||||||
|
|
||||||
let containsIndex = false
|
let containsIndex = false
|
||||||
@ -118,14 +117,12 @@ export const ContentPage: QuartzEmitterPlugin<Partial<FullPageLayout>> = (userOp
|
|||||||
}
|
}
|
||||||
|
|
||||||
const content = renderPage(cfg, slug, componentData, opts, externalResources)
|
const content = renderPage(cfg, slug, componentData, opts, externalResources)
|
||||||
const fp = await write({
|
yield write({
|
||||||
ctx,
|
ctx,
|
||||||
content,
|
content,
|
||||||
slug,
|
slug,
|
||||||
ext: ".html",
|
ext: ".html",
|
||||||
})
|
})
|
||||||
|
|
||||||
fps.push(fp)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!containsIndex && !ctx.argv.fastRebuild) {
|
if (!containsIndex && !ctx.argv.fastRebuild) {
|
||||||
@ -135,8 +132,6 @@ export const ContentPage: QuartzEmitterPlugin<Partial<FullPageLayout>> = (userOp
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return fps
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -69,8 +69,7 @@ export const FolderPage: QuartzEmitterPlugin<Partial<FolderPageOptions>> = (user
|
|||||||
|
|
||||||
return graph
|
return graph
|
||||||
},
|
},
|
||||||
async emit(ctx, content, resources): Promise<FilePath[]> {
|
async *emit(ctx, content, resources) {
|
||||||
const fps: FilePath[] = []
|
|
||||||
const allFiles = content.map((c) => c[1].data)
|
const allFiles = content.map((c) => c[1].data)
|
||||||
const cfg = ctx.cfg.configuration
|
const cfg = ctx.cfg.configuration
|
||||||
|
|
||||||
@ -119,16 +118,13 @@ export const FolderPage: QuartzEmitterPlugin<Partial<FolderPageOptions>> = (user
|
|||||||
}
|
}
|
||||||
|
|
||||||
const content = renderPage(cfg, slug, componentData, opts, externalResources)
|
const content = renderPage(cfg, slug, componentData, opts, externalResources)
|
||||||
const fp = await write({
|
yield write({
|
||||||
ctx,
|
ctx,
|
||||||
content,
|
content,
|
||||||
slug,
|
slug,
|
||||||
ext: ".html",
|
ext: ".html",
|
||||||
})
|
})
|
||||||
|
|
||||||
fps.push(fp)
|
|
||||||
}
|
}
|
||||||
return fps
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { QuartzEmitterPlugin } from "../types"
|
import { QuartzEmitterPlugin } from "../types"
|
||||||
import { i18n } from "../../i18n"
|
import { i18n } from "../../i18n"
|
||||||
import { unescapeHTML } from "../../util/escape"
|
import { unescapeHTML } from "../../util/escape"
|
||||||
import { FilePath, FullSlug, getFileExtension } from "../../util/path"
|
import { FullSlug, getFileExtension } from "../../util/path"
|
||||||
import { ImageOptions, SocialImageOptions, defaultImage, getSatoriFont } from "../../util/og"
|
import { ImageOptions, SocialImageOptions, defaultImage, getSatoriFont } from "../../util/og"
|
||||||
import { getFontSpecificationName } from "../../util/theme"
|
import { getFontSpecificationName } from "../../util/theme"
|
||||||
import sharp from "sharp"
|
import sharp from "sharp"
|
||||||
@ -52,10 +52,8 @@ export const CustomOgImages: QuartzEmitterPlugin<Partial<SocialImageOptions>> =
|
|||||||
getQuartzComponents() {
|
getQuartzComponents() {
|
||||||
return []
|
return []
|
||||||
},
|
},
|
||||||
async emit(ctx, content, _resources) {
|
async *emit(ctx, content, _resources) {
|
||||||
const cfg = ctx.cfg.configuration
|
const cfg = ctx.cfg.configuration
|
||||||
const emittedFiles: FilePath[] = []
|
|
||||||
|
|
||||||
const headerFont = getFontSpecificationName(cfg.theme.typography.header)
|
const headerFont = getFontSpecificationName(cfg.theme.typography.header)
|
||||||
const bodyFont = getFontSpecificationName(cfg.theme.typography.body)
|
const bodyFont = getFontSpecificationName(cfg.theme.typography.body)
|
||||||
const fonts = await getSatoriFont(headerFont, bodyFont)
|
const fonts = await getSatoriFont(headerFont, bodyFont)
|
||||||
@ -88,17 +86,13 @@ export const CustomOgImages: QuartzEmitterPlugin<Partial<SocialImageOptions>> =
|
|||||||
fullOptions,
|
fullOptions,
|
||||||
)
|
)
|
||||||
|
|
||||||
const fp = await write({
|
yield write({
|
||||||
ctx,
|
ctx,
|
||||||
content: stream,
|
content: stream,
|
||||||
slug: `${slug}-og-image` as FullSlug,
|
slug: `${slug}-og-image` as FullSlug,
|
||||||
ext: ".webp",
|
ext: ".webp",
|
||||||
})
|
})
|
||||||
|
|
||||||
emittedFiles.push(fp)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return emittedFiles
|
|
||||||
},
|
},
|
||||||
externalResources: (ctx) => {
|
externalResources: (ctx) => {
|
||||||
if (!ctx.cfg.configuration.baseUrl) {
|
if (!ctx.cfg.configuration.baseUrl) {
|
||||||
@ -115,7 +109,6 @@ export const CustomOgImages: QuartzEmitterPlugin<Partial<SocialImageOptions>> =
|
|||||||
? `https://${baseUrl}/${pageData.slug!}-og-image.webp`
|
? `https://${baseUrl}/${pageData.slug!}-og-image.webp`
|
||||||
: undefined
|
: undefined
|
||||||
const defaultOgImagePath = `https://${baseUrl}/static/og-image.png`
|
const defaultOgImagePath = `https://${baseUrl}/static/og-image.png`
|
||||||
|
|
||||||
const ogImagePath = userDefinedOgImagePath ?? generatedOgImagePath ?? defaultOgImagePath
|
const ogImagePath = userDefinedOgImagePath ?? generatedOgImagePath ?? defaultOgImagePath
|
||||||
|
|
||||||
const ogImageMimeType = `image/${getFileExtension(ogImagePath) ?? "png"}`
|
const ogImageMimeType = `image/${getFileExtension(ogImagePath) ?? "png"}`
|
||||||
|
@ -3,6 +3,7 @@ import { QuartzEmitterPlugin } from "../types"
|
|||||||
import fs from "fs"
|
import fs from "fs"
|
||||||
import { glob } from "../../util/glob"
|
import { glob } from "../../util/glob"
|
||||||
import DepGraph from "../../depgraph"
|
import DepGraph from "../../depgraph"
|
||||||
|
import { dirname } from "path"
|
||||||
|
|
||||||
export const Static: QuartzEmitterPlugin = () => ({
|
export const Static: QuartzEmitterPlugin = () => ({
|
||||||
name: "Static",
|
name: "Static",
|
||||||
@ -20,13 +21,17 @@ export const Static: QuartzEmitterPlugin = () => ({
|
|||||||
|
|
||||||
return graph
|
return graph
|
||||||
},
|
},
|
||||||
async emit({ argv, cfg }, _content, _resources): Promise<FilePath[]> {
|
async *emit({ argv, cfg }, _content) {
|
||||||
const staticPath = joinSegments(QUARTZ, "static")
|
const staticPath = joinSegments(QUARTZ, "static")
|
||||||
const fps = await glob("**", staticPath, cfg.configuration.ignorePatterns)
|
const fps = await glob("**", staticPath, cfg.configuration.ignorePatterns)
|
||||||
await fs.promises.cp(staticPath, joinSegments(argv.output, "static"), {
|
const outputStaticPath = joinSegments(argv.output, "static")
|
||||||
recursive: true,
|
await fs.promises.mkdir(outputStaticPath, { recursive: true })
|
||||||
dereference: true,
|
for (const fp of fps) {
|
||||||
})
|
const src = joinSegments(staticPath, fp) as FilePath
|
||||||
return fps.map((fp) => joinSegments(argv.output, "static", fp)) as FilePath[]
|
const dest = joinSegments(outputStaticPath, fp) as FilePath
|
||||||
|
await fs.promises.mkdir(dirname(dest), { recursive: true })
|
||||||
|
await fs.promises.copyFile(src, dest)
|
||||||
|
yield dest
|
||||||
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -71,8 +71,7 @@ export const TagPage: QuartzEmitterPlugin<Partial<TagPageOptions>> = (userOpts)
|
|||||||
|
|
||||||
return graph
|
return graph
|
||||||
},
|
},
|
||||||
async emit(ctx, content, resources): Promise<FilePath[]> {
|
async *emit(ctx, content, resources) {
|
||||||
const fps: FilePath[] = []
|
|
||||||
const allFiles = content.map((c) => c[1].data)
|
const allFiles = content.map((c) => c[1].data)
|
||||||
const cfg = ctx.cfg.configuration
|
const cfg = ctx.cfg.configuration
|
||||||
|
|
||||||
@ -127,16 +126,13 @@ export const TagPage: QuartzEmitterPlugin<Partial<TagPageOptions>> = (userOpts)
|
|||||||
}
|
}
|
||||||
|
|
||||||
const content = renderPage(cfg, slug, componentData, opts, externalResources)
|
const content = renderPage(cfg, slug, componentData, opts, externalResources)
|
||||||
const fp = await write({
|
yield write({
|
||||||
ctx,
|
ctx,
|
||||||
content,
|
content,
|
||||||
slug: file.data.slug!,
|
slug: file.data.slug!,
|
||||||
ext: ".html",
|
ext: ".html",
|
||||||
})
|
})
|
||||||
|
|
||||||
fps.push(fp)
|
|
||||||
}
|
}
|
||||||
return fps
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,11 @@ export type QuartzEmitterPlugin<Options extends OptionType = undefined> = (
|
|||||||
) => QuartzEmitterPluginInstance
|
) => QuartzEmitterPluginInstance
|
||||||
export type QuartzEmitterPluginInstance = {
|
export type QuartzEmitterPluginInstance = {
|
||||||
name: string
|
name: string
|
||||||
emit(ctx: BuildCtx, content: ProcessedContent[], resources: StaticResources): Promise<FilePath[]>
|
emit(
|
||||||
|
ctx: BuildCtx,
|
||||||
|
content: ProcessedContent[],
|
||||||
|
resources: StaticResources,
|
||||||
|
): Promise<FilePath[]> | AsyncGenerator<FilePath>
|
||||||
/**
|
/**
|
||||||
* Returns the components (if any) that are used in rendering the page.
|
* Returns the components (if any) that are used in rendering the page.
|
||||||
* This helps Quartz optimize the page by only including necessary resources
|
* This helps Quartz optimize the page by only including necessary resources
|
||||||
|
@ -14,20 +14,34 @@ export async function emitContent(ctx: BuildCtx, content: ProcessedContent[]) {
|
|||||||
|
|
||||||
let emittedFiles = 0
|
let emittedFiles = 0
|
||||||
const staticResources = getStaticResourcesFromPlugins(ctx)
|
const staticResources = getStaticResourcesFromPlugins(ctx)
|
||||||
for (const emitter of cfg.plugins.emitters) {
|
await Promise.all(
|
||||||
try {
|
cfg.plugins.emitters.map(async (emitter) => {
|
||||||
const emitted = await emitter.emit(ctx, content, staticResources)
|
try {
|
||||||
emittedFiles += emitted.length
|
const emitted = await emitter.emit(ctx, content, staticResources)
|
||||||
|
if (Symbol.asyncIterator in emitted) {
|
||||||
if (ctx.argv.verbose) {
|
// Async generator case
|
||||||
for (const file of emitted) {
|
const files: string[] = []
|
||||||
console.log(`[emit:${emitter.name}] ${file}`)
|
for await (const file of emitted) {
|
||||||
|
files.push(file)
|
||||||
|
emittedFiles++
|
||||||
|
if (ctx.argv.verbose) {
|
||||||
|
console.log(`[emit:${emitter.name}] ${file}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Array case
|
||||||
|
emittedFiles += emitted.length
|
||||||
|
if (ctx.argv.verbose) {
|
||||||
|
for (const file of emitted) {
|
||||||
|
console.log(`[emit:${emitter.name}] ${file}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} catch (err) {
|
||||||
|
trace(`Failed to emit from plugin \`${emitter.name}\``, err as Error)
|
||||||
}
|
}
|
||||||
} catch (err) {
|
}),
|
||||||
trace(`Failed to emit from plugin \`${emitter.name}\``, err as Error)
|
)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
log.end(`Emitted ${emittedFiles} files to \`${argv.output}\` in ${perf.timeSince()}`)
|
log.end(`Emitted ${emittedFiles} files to \`${argv.output}\` in ${perf.timeSince()}`)
|
||||||
}
|
}
|
||||||
|
@ -15,8 +15,10 @@ export async function getSatoriFont(headerFontName: string, bodyFontName: string
|
|||||||
const bodyWeight = 400 as FontWeight
|
const bodyWeight = 400 as FontWeight
|
||||||
|
|
||||||
// Fetch fonts
|
// Fetch fonts
|
||||||
const headerFont = await fetchTtf(headerFontName, headerWeight)
|
const [headerFont, bodyFont] = await Promise.all([
|
||||||
const bodyFont = await fetchTtf(bodyFontName, bodyWeight)
|
fetchTtf(headerFontName, headerWeight),
|
||||||
|
fetchTtf(bodyFontName, bodyWeight),
|
||||||
|
])
|
||||||
|
|
||||||
// Convert fonts to satori font format and return
|
// Convert fonts to satori font format and return
|
||||||
const fonts: SatoriOptions["fonts"] = [
|
const fonts: SatoriOptions["fonts"] = [
|
||||||
@ -26,38 +28,48 @@ export async function getSatoriFont(headerFontName: string, bodyFontName: string
|
|||||||
return fonts
|
return fonts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cache for memoizing font data
|
||||||
|
const fontCache = new Map<string, Promise<ArrayBuffer>>()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the `.ttf` file of a google font
|
* Get the `.ttf` file of a google font
|
||||||
* @param fontName name of google font
|
* @param fontName name of google font
|
||||||
* @param weight what font weight to fetch font
|
* @param weight what font weight to fetch font
|
||||||
* @returns `.ttf` file of google font
|
* @returns `.ttf` file of google font
|
||||||
*/
|
*/
|
||||||
async function fetchTtf(fontName: string, weight: FontWeight): Promise<ArrayBuffer> {
|
export async function fetchTtf(fontName: string, weight: FontWeight): Promise<ArrayBuffer> {
|
||||||
try {
|
const cacheKey = `${fontName}-${weight}`
|
||||||
// Get css file from google fonts
|
if (fontCache.has(cacheKey)) {
|
||||||
const cssResponse = await fetch(
|
return fontCache.get(cacheKey)!
|
||||||
`https://fonts.googleapis.com/css2?family=${fontName}:wght@${weight}`,
|
|
||||||
)
|
|
||||||
const css = await cssResponse.text()
|
|
||||||
|
|
||||||
// Extract .ttf url from css file
|
|
||||||
const urlRegex = /url\((https:\/\/fonts.gstatic.com\/s\/.*?.ttf)\)/g
|
|
||||||
const match = urlRegex.exec(css)
|
|
||||||
|
|
||||||
if (!match) {
|
|
||||||
throw new Error("Could not fetch font")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Retrieve font data as ArrayBuffer
|
|
||||||
const fontResponse = await fetch(match[1])
|
|
||||||
|
|
||||||
// fontData is an ArrayBuffer containing the .ttf file data (get match[1] due to google fonts response format, always contains link twice, but second entry is the "raw" link)
|
|
||||||
const fontData = await fontResponse.arrayBuffer()
|
|
||||||
|
|
||||||
return fontData
|
|
||||||
} catch (error) {
|
|
||||||
throw new Error(`Error fetching font: ${error}`)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If not in cache, fetch and store the promise
|
||||||
|
const fontPromise = (async () => {
|
||||||
|
try {
|
||||||
|
// Get css file from google fonts
|
||||||
|
const cssResponse = await fetch(
|
||||||
|
`https://fonts.googleapis.com/css2?family=${fontName}:wght@${weight}`,
|
||||||
|
)
|
||||||
|
const css = await cssResponse.text()
|
||||||
|
|
||||||
|
// Extract .ttf url from css file
|
||||||
|
const urlRegex = /url\((https:\/\/fonts.gstatic.com\/s\/.*?.ttf)\)/g
|
||||||
|
const match = urlRegex.exec(css)
|
||||||
|
|
||||||
|
if (!match) {
|
||||||
|
throw new Error("Could not fetch font")
|
||||||
|
}
|
||||||
|
|
||||||
|
// fontData is an ArrayBuffer containing the .ttf file data (get match[1] due to google fonts response format, always contains link twice, but second entry is the "raw" link)
|
||||||
|
const fontResponse = await fetch(match[1])
|
||||||
|
return await fontResponse.arrayBuffer()
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(`Error fetching font: ${error}`)
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
|
||||||
|
fontCache.set(cacheKey, fontPromise)
|
||||||
|
return fontPromise
|
||||||
}
|
}
|
||||||
|
|
||||||
export type SocialImageOptions = {
|
export type SocialImageOptions = {
|
||||||
|
@ -90,6 +90,36 @@ export function googleFontHref(theme: Theme) {
|
|||||||
return `https://fonts.googleapis.com/css2?family=${bodyFont}&family=${headerFont}&family=${codeFont}&display=swap`
|
return `https://fonts.googleapis.com/css2?family=${bodyFont}&family=${headerFont}&family=${codeFont}&display=swap`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface GoogleFontFile {
|
||||||
|
url: string
|
||||||
|
filename: string
|
||||||
|
extension: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function processGoogleFonts(
|
||||||
|
stylesheet: string,
|
||||||
|
baseUrl: string,
|
||||||
|
): Promise<{
|
||||||
|
processedStylesheet: string
|
||||||
|
fontFiles: GoogleFontFile[]
|
||||||
|
}> {
|
||||||
|
const fontSourceRegex = /url\((https:\/\/fonts.gstatic.com\/s\/[^)]+\.(woff2|ttf))\)/g
|
||||||
|
const fontFiles: GoogleFontFile[] = []
|
||||||
|
let processedStylesheet = stylesheet
|
||||||
|
|
||||||
|
let match
|
||||||
|
while ((match = fontSourceRegex.exec(stylesheet)) !== null) {
|
||||||
|
const url = match[1]
|
||||||
|
const [filename, extension] = url.split("/").pop()!.split(".")
|
||||||
|
const staticUrl = `https://${baseUrl}/static/fonts/${filename}.${extension}`
|
||||||
|
|
||||||
|
processedStylesheet = processedStylesheet.replace(url, staticUrl)
|
||||||
|
fontFiles.push({ url, filename, extension })
|
||||||
|
}
|
||||||
|
|
||||||
|
return { processedStylesheet, fontFiles }
|
||||||
|
}
|
||||||
|
|
||||||
export function joinStyles(theme: Theme, ...stylesheet: string[]) {
|
export function joinStyles(theme: Theme, ...stylesheet: string[]) {
|
||||||
return `
|
return `
|
||||||
${stylesheet.join("\n\n")}
|
${stylesheet.join("\n\n")}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user