docs/.vitepress/configs/hooks/opengraph.ts
134 lines · 3738 bytes
1/**
2 * Copyright (c) 2024 taskylizard
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { mkdir, readFile, writeFile } from 'node:fs/promises'
17import { dirname, resolve } from 'node:path'
18import { fileURLToPath } from 'node:url'
19import { createContentLoader } from 'vitepress'
20import type { ContentData, SiteConfig } from 'vitepress'
21import { type SatoriOptions, satoriVue } from 'x-satori/vue'
22import { renderAsync } from '@resvg/resvg-js'
23import { excludedFiles } from '../constants'
24
25const __dirname = dirname(fileURLToPath(import.meta.url))
26const __fonts = resolve(__dirname, '../fonts')
27
28export async function generateImages(config: SiteConfig) {
29 const pages = await createContentLoader('**/*.md', {
30 excerpt: true,
31 globOptions: {
32 ignore: [...excludedFiles, 'node_modules', 'dist']
33 }
34 }).load()
35 const template = await readFile(
36 resolve(__dirname, '../../theme/components/OgImageTemplate.vue'),
37 'utf-8'
38 )
39
40 const fonts: SatoriOptions['fonts'] = [
41 {
42 name: 'Inter',
43 data: await readFile(resolve(__fonts, 'Inter-Regular.otf')),
44 weight: 400,
45 style: 'normal'
46 },
47 {
48 name: 'Inter',
49 data: await readFile(resolve(__fonts, 'Inter-Medium.otf')),
50 weight: 500,
51 style: 'normal'
52 },
53 {
54 name: 'Inter',
55 data: await readFile(resolve(__fonts, 'Inter-SemiBold.otf')),
56 weight: 600,
57 style: 'normal'
58 },
59 {
60 name: 'Inter',
61 data: await readFile(resolve(__fonts, 'Inter-Bold.otf')),
62 weight: 700,
63 style: 'normal'
64 }
65 ]
66
67 const filteredPages = pages.filter((p) => p.frontmatter.image === undefined)
68
69 for (const page of filteredPages) {
70 await generateImage({
71 page,
72 template,
73 outDir: config.outDir,
74 fonts
75 })
76 }
77}
78
79interface GenerateImagesOptions {
80 page: ContentData
81 template: string
82 outDir: string
83 fonts: SatoriOptions['fonts']
84}
85
86function getDir(url: string) {
87 if (url.startsWith('/glossary/')) {
88 return 'Glossary'
89 } else if (url.startsWith('/guides/')) {
90 return 'Guide'
91 }
92
93 // Means we are at root.
94 return undefined
95}
96
97async function generateImage({
98 page,
99 template,
100 outDir,
101 fonts
102}: GenerateImagesOptions) {
103 const { frontmatter, url } = page
104
105 const options: SatoriOptions = {
106 // Use custom dimensions from frontmatter.og if provided
107 width: frontmatter.og?.width ?? 1800,
108 height: frontmatter.og?.height ?? 900,
109 fonts,
110 props: {
111 title:
112 frontmatter.layout === 'home'
113 ? frontmatter.hero.name ?? frontmatter.title
114 : frontmatter.customMetaTitle ?? frontmatter.title,
115 description:
116 frontmatter.layout === 'home'
117 ? frontmatter.hero.tagline ?? frontmatter.description
118 : frontmatter.description,
119 // dir: getDir(url)
120 image: frontmatter.og?.image ?? 'https://i.wotaku.wiki/f/default.png'
121 }
122 }
123
124 const svg = await satoriVue(options, template)
125
126 const render = await renderAsync(svg)
127
128 const outputFolder = resolve(outDir, url.substring(1), '__og_image__')
129 const outputFile = resolve(outputFolder, 'og.png')
130
131 await mkdir(outputFolder, { recursive: true })
132
133 return await writeFile(outputFile, render.asPng())
134}