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 | import { dirname, resolve } from 'node:path' | 9 | import { dirname, resolve } from 'node:path' |
| 10 | import { fileURLToPath } from 'node:url' | 10 | import { fileURLToPath } from 'node:url' |
| 11 | import { type Font, render } from 'takumi-js' | 11 | import { type Font, render } from 'takumi-js' |
| 12 | import { Renderer } from 'takumi-js/node' | ||
| 12 | import { createContentLoader } from 'vitepress' | 13 | import { createContentLoader } from 'vitepress' |
| 13 | import type { ContentData, SiteConfig } from 'vitepress' | 14 | import type { ContentData, SiteConfig } from 'vitepress' |
| 14 | import { excludedFiles } from '../constants' | 15 | import { excludedFiles } from '../constants' |
| @@ -16,33 +17,39 @@ import { OgImageTemplate } from './OgImageTemplate' | |||
| 16 | 17 | ||
| 17 | const __dirname = dirname(fileURLToPath(import.meta.url)) | 18 | const __dirname = dirname(fileURLToPath(import.meta.url)) |
| 18 | const __fonts = resolve(__dirname, '../fonts') | 19 | const __fonts = resolve(__dirname, '../fonts') |
| 19 | const fonts: Font[] = [ | 20 | const fonts: Font[] = [ |
| 20 | { | 21 | { |
| 21 | name: 'Inter', | 22 | name: 'Inter', |
| 22 | data: await readFile(resolve(__fonts, 'Inter-Regular.otf')), | 23 | data: await readFile(resolve(__fonts, 'Inter-Regular.otf')), |
| 23 | weight: 400, | 24 | weight: 400, |
| 24 | style: 'normal' | 25 | style: 'normal' |
| 25 | }, | 26 | }, |
| 26 | { | 27 | { |
| 27 | name: 'Inter', | 28 | name: 'Inter', |
| 28 | data: await readFile(resolve(__fonts, 'Inter-Medium.otf')), | 29 | data: await readFile(resolve(__fonts, 'Inter-Medium.otf')), |
| 29 | weight: 500, | 30 | weight: 500, |
| 30 | style: 'normal' | 31 | style: 'normal' |
| 31 | }, | 32 | }, |
| 32 | { | 33 | { |
| 33 | name: 'Inter', | 34 | name: 'Inter', |
| 34 | data: await readFile(resolve(__fonts, 'Inter-SemiBold.otf')), | 35 | data: await readFile(resolve(__fonts, 'Inter-SemiBold.otf')), |
| 35 | weight: 600, | 36 | weight: 600, |
| 36 | style: 'normal' | 37 | style: 'normal' |
| 37 | }, | 38 | }, |
| 38 | { | 39 | { |
| 39 | name: 'Inter', | 40 | name: 'Inter', |
| 40 | data: await readFile(resolve(__fonts, 'Inter-Bold.otf')), | 41 | data: await readFile(resolve(__fonts, 'Inter-Bold.otf')), |
| 41 | weight: 700, | 42 | weight: 700, |
| 42 | style: 'normal' | 43 | style: 'normal' |
| 43 | } | 44 | } |
| 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 | export async function generateImages(config: SiteConfig) { | 54 | export async function generateImages(config: SiteConfig) { |
| 48 | const pages = await createContentLoader('**/*.md', { | 55 | const pages = await createContentLoader('**/*.md', { |
| @@ -54,11 +61,12 @@ export async function generateImages(config: SiteConfig) { | |||
| 54 | 61 | ||
| 55 | const filteredPages = pages.filter((p) => p.frontmatter.image === undefined) | 62 | const filteredPages = pages.filter((p) => p.frontmatter.image === undefined) |
| 56 | 63 | ||
| 64 | await loadPersistentImages(filteredPages) | ||
| 65 | |||
| 57 | for (const page of filteredPages) { | 66 | for (const page of filteredPages) { |
| 58 | await generateImage({ | 67 | await generateImage({ |
| 59 | page, | 68 | page, |
| 60 | outDir: config.outDir, | 69 | outDir: config.outDir |
| 61 | fonts | ||
| 62 | }) | 70 | }) |
| 63 | } | 71 | } |
| 64 | } | 72 | } |
| @@ -66,13 +74,11 @@ export async function generateImages(config: SiteConfig) { | |||
| 66 | interface GenerateImagesOptions { | 74 | interface GenerateImagesOptions { |
| 67 | page: ContentData | 75 | page: ContentData |
| 68 | outDir: string | 76 | outDir: string |
| 69 | fonts: Font[] | ||
| 70 | } | 77 | } |
| 71 | 78 | ||
| 72 | async function generateImage({ | 79 | async function generateImage({ |
| 73 | page, | 80 | page, |
| 74 | outDir, | 81 | outDir |
| 75 | fonts | ||
| 76 | }: GenerateImagesOptions) { | 82 | }: GenerateImagesOptions) { |
| 77 | const { frontmatter, url } = page | 83 | const { frontmatter, url } = page |
| 78 | const width = frontmatter.og?.width ?? 1800 | 84 | const width = frontmatter.og?.width ?? 1800 |
| @@ -82,16 +88,17 @@ async function generateImage({ | |||
| 82 | ? (frontmatter.hero.name ?? frontmatter.title) | 88 | ? (frontmatter.hero.name ?? frontmatter.title) |
| 83 | : (frontmatter.customMetaTitle ?? frontmatter.title) | 89 | : (frontmatter.customMetaTitle ?? frontmatter.title) |
| 84 | ) | 90 | ) |
| 85 | const image = String( | 91 | const image = getImage(page) |
| 86 | frontmatter.og?.image ?? 'https://i.wotaku.wiki/f/default.png' | ||
| 87 | ) | ||
| 88 | 92 | ||
| 89 | const png = await render( | 93 | const png = await render( |
| 90 | OgImageTemplate({ title, image }), | 94 | OgImageTemplate({ title, image: persistentImageByUrl.get(image) ?? image }), |
| 91 | { | 95 | { |
| 92 | width, | 96 | width, |
| 93 | height, | 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 | return await writeFile(outputFile, png) | 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 | } | ||