Commit d4142af30f
Unregistered key
Layout: unified · split
docs/.vitepress/configs/hooks/opengraph.ts +73 −36
| @@ -9,6 +9,7 @@ import { mkdir, readFile, writeFile } from 'node:fs/promises' | ||
| 9 | 9 | import { dirname, resolve } from 'node:path' |
| 10 | 10 | import { fileURLToPath } from 'node:url' |
| 11 | 11 | import { type Font, render } from 'takumi-js' |
| 12 | import { Renderer } from 'takumi-js/node' | |
| 12 | 13 | import { createContentLoader } from 'vitepress' |
| 13 | 14 | import type { ContentData, SiteConfig } from 'vitepress' |
| 14 | 15 | import { excludedFiles } from '../constants' |
| @@ -16,33 +17,39 @@ import { OgImageTemplate } from './OgImageTemplate' | ||
| 16 | 17 | |
| 17 | 18 | const __dirname = dirname(fileURLToPath(import.meta.url)) |
| 18 | 19 | const __fonts = resolve(__dirname, '../fonts') |
| 19 | const fonts: Font[] = [ | |
| 20 | { | |
| 21 | name: 'Inter', | |
| 22 | data: await readFile(resolve(__fonts, 'Inter-Regular.otf')), | |
| 23 | weight: 400, | |
| 24 | style: 'normal' | |
| 25 | }, | |
| 26 | { | |
| 27 | name: 'Inter', | |
| 28 | data: await readFile(resolve(__fonts, 'Inter-Medium.otf')), | |
| 29 | weight: 500, | |
| 30 | style: 'normal' | |
| 31 | }, | |
| 32 | { | |
| 33 | name: 'Inter', | |
| 34 | data: await readFile(resolve(__fonts, 'Inter-SemiBold.otf')), | |
| 35 | weight: 600, | |
| 36 | style: 'normal' | |
| 37 | }, | |
| 38 | { | |
| 39 | name: 'Inter', | |
| 40 | data: await readFile(resolve(__fonts, 'Inter-Bold.otf')), | |
| 41 | weight: 700, | |
| 42 | style: 'normal' | |
| 43 | } | |
| 44 | ] | |
| 20 | const fonts: Font[] = [ | |
| 21 | { | |
| 22 | name: 'Inter', | |
| 23 | data: await readFile(resolve(__fonts, 'Inter-Regular.otf')), | |
| 24 | weight: 400, | |
| 25 | style: 'normal' | |
| 26 | }, | |
| 27 | { | |
| 28 | name: 'Inter', | |
| 29 | data: await readFile(resolve(__fonts, 'Inter-Medium.otf')), | |
| 30 | weight: 500, | |
| 31 | style: 'normal' | |
| 32 | }, | |
| 33 | { | |
| 34 | name: 'Inter', | |
| 35 | data: await readFile(resolve(__fonts, 'Inter-SemiBold.otf')), | |
| 36 | weight: 600, | |
| 37 | style: 'normal' | |
| 38 | }, | |
| 39 | { | |
| 40 | name: 'Inter', | |
| 41 | data: await readFile(resolve(__fonts, 'Inter-Bold.otf')), | |
| 42 | weight: 700, | |
| 43 | style: 'normal' | |
| 44 | } | |
| 45 | ] | |
| 46 | ||
| 47 | const renderer = new Renderer() | |
| 48 | await renderer.loadFonts(fonts) | |
| 45 | 49 | |
| 50 | const resourceCache = new Map<string, ArrayBuffer>() | |
| 51 | const persistentImageByUrl = new Map<string, string>() | |
| 52 | const defaultImage = 'https://i.wotaku.wiki/f/default.png' | |
| 46 | 53 | |
| 47 | 54 | export async function generateImages(config: SiteConfig) { |
| 48 | 55 | const pages = await createContentLoader('**/*.md', { |
| @@ -54,11 +61,12 @@ export async function generateImages(config: SiteConfig) { | ||
| 54 | 61 | |
| 55 | 62 | const filteredPages = pages.filter((p) => p.frontmatter.image === undefined) |
| 56 | 63 | |
| 64 | await loadPersistentImages(filteredPages) | |
| 65 | ||
| 57 | 66 | for (const page of filteredPages) { |
| 58 | 67 | await generateImage({ |
| 59 | 68 | page, |
| 60 | outDir: config.outDir, | |
| 61 | fonts | |
| 69 | outDir: config.outDir | |
| 62 | 70 | }) |
| 63 | 71 | } |
| 64 | 72 | } |
| @@ -66,13 +74,11 @@ export async function generateImages(config: SiteConfig) { | ||
| 66 | 74 | interface GenerateImagesOptions { |
| 67 | 75 | page: ContentData |
| 68 | 76 | outDir: string |
| 69 | fonts: Font[] | |
| 70 | 77 | } |
| 71 | 78 | |
| 72 | 79 | async function generateImage({ |
| 73 | 80 | page, |
| 74 | outDir, | |
| 75 | fonts | |
| 81 | outDir | |
| 76 | 82 | }: GenerateImagesOptions) { |
| 77 | 83 | const { frontmatter, url } = page |
| 78 | 84 | const width = frontmatter.og?.width ?? 1800 |
| @@ -82,16 +88,17 @@ async function generateImage({ | ||
| 82 | 88 | ? (frontmatter.hero.name ?? frontmatter.title) |
| 83 | 89 | : (frontmatter.customMetaTitle ?? frontmatter.title) |
| 84 | 90 | ) |
| 85 | const image = String( | |
| 86 | frontmatter.og?.image ?? 'https://i.wotaku.wiki/f/default.png' | |
| 87 | ) | |
| 91 | const image = getImage(page) | |
| 88 | 92 | |
| 89 | 93 | const png = await render( |
| 90 | OgImageTemplate({ title, image }), | |
| 94 | OgImageTemplate({ title, image: persistentImageByUrl.get(image) ?? image }), | |
| 91 | 95 | { |
| 92 | 96 | width, |
| 93 | 97 | height, |
| 94 | fonts | |
| 98 | renderer, | |
| 99 | resourcesOptions: { | |
| 100 | cache: resourceCache | |
| 101 | } | |
| 95 | 102 | } |
| 96 | 103 | ) |
| 97 | 104 | |
| @@ -102,3 +109,33 @@ async function generateImage({ | ||
| 102 | 109 | |
| 103 | 110 | return await writeFile(outputFile, png) |
| 104 | 111 | } |
| 112 | ||
| 113 | async function loadPersistentImages(pages: ContentData[]) { | |
| 114 | const imageUrls = [...new Set(pages.map(getImage))] | |
| 115 | .filter((url) => /^https?:\/\//.test(url)) | |
| 116 | .filter((url) => !persistentImageByUrl.has(url)) | |
| 117 | const offset = persistentImageByUrl.size | |
| 118 | ||
| 119 | await Promise.all( | |
| 120 | imageUrls.map(async (url, index) => { | |
| 121 | const src = `og-background-${offset + index}` | |
| 122 | const response = await fetch(url, { | |
| 123 | signal: AbortSignal.timeout(5000) | |
| 124 | }) | |
| 125 | ||
| 126 | if (!response.ok) { | |
| 127 | throw new Error( | |
| 128 | `Failed to fetch OpenGraph image ${url}: ${response.status} ${response.statusText}` | |
| 129 | ) | |
| 130 | } | |
| 131 | ||
| 132 | const data = await response.arrayBuffer() | |
| 133 | await renderer.putPersistentImage({ src, data }) | |
| 134 | persistentImageByUrl.set(url, src) | |
| 135 | }) | |
| 136 | ) | |
| 137 | } | |
| 138 | ||
| 139 | function getImage(page: ContentData) { | |
| 140 | return String(page.frontmatter.og?.image ?? defaultImage) | |
| 141 | } | |